完善日志级别 (#625)

Co-authored-by: simons <liyulinga@qq.com>
pull/651/head
vincenzo 4 years ago committed by GitHub
parent ecd7533e7f
commit 0f4fa0ad7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      pkg/log/file.go
  2. 104
      pkg/log/log.go
  3. 10
      pkg/log/log_test.go

@ -11,16 +11,20 @@ import (
// level idx
const (
_infoIdx = iota
_debugIdx = iota
_infoIdx
_warnIdx
_errorIdx
_fatalIdx
_totalIdx
)
var _fileNames = map[int]string{
_debugIdx: "debug.log",
_infoIdx: "info.log",
_warnIdx: "warning.log",
_errorIdx: "error.log",
_fatalIdx: "fatal.log",
}
// FileHandler .
@ -63,10 +67,14 @@ func (h *FileHandler) Log(ctx context.Context, lv Level, args ...D) {
d[_time] = time.Now().Format(_timeFormat)
var w io.Writer
switch lv {
case _debugLevel:
w = h.fws[_debugIdx]
case _warnLevel:
w = h.fws[_warnIdx]
case _errorLevel:
w = h.fws[_errorIdx]
case _fatalLevel:
w = h.fws[_fatalIdx]
default:
w = h.fws[_infoIdx]
}

@ -138,49 +138,109 @@ func Init(conf *Config) {
c = conf
}
// Debug logs a message at the debug log level.
func Debug(format string, args ...interface{}) {
if int32(_debugLevel) >= c.V {
h.Log(context.Background(), _debugLevel, KVString(_log, fmt.Sprintf(format, args...)))
}
}
// Info logs a message at the info log level.
func Info(format string, args ...interface{}) {
h.Log(context.Background(), _infoLevel, KVString(_log, fmt.Sprintf(format, args...)))
if int32(_infoLevel) >= c.V {
h.Log(context.Background(), _infoLevel, KVString(_log, fmt.Sprintf(format, args...)))
}
}
// Warn logs a message at the warning log level.
func Warn(format string, args ...interface{}) {
h.Log(context.Background(), _warnLevel, KVString(_log, fmt.Sprintf(format, args...)))
if int32(_warnLevel) >= c.V {
h.Log(context.Background(), _warnLevel, KVString(_log, fmt.Sprintf(format, args...)))
}
}
// Error logs a message at the error log level.
func Error(format string, args ...interface{}) {
h.Log(context.Background(), _errorLevel, KVString(_log, fmt.Sprintf(format, args...)))
if int32(_errorLevel) >= c.V {
h.Log(context.Background(), _errorLevel, KVString(_log, fmt.Sprintf(format, args...)))
}
}
// Fatal logs a message at the fatal log level.
func Fatal(format string, args ...interface{}) {
if int32(_fatalLevel) >= c.V {
h.Log(context.Background(), _fatalLevel, KVString(_log, fmt.Sprintf(format, args...)))
}
}
// Debugc logs a message at the debug log level.
func Debugc(ctx context.Context, format string, args ...interface{}) {
if int32(_debugLevel) >= c.V {
h.Log(ctx, _debugLevel, KVString(_log, fmt.Sprintf(format, args...)))
}
}
// Infoc logs a message at the info log level.
func Infoc(ctx context.Context, format string, args ...interface{}) {
h.Log(ctx, _infoLevel, KVString(_log, fmt.Sprintf(format, args...)))
if int32(_infoLevel) >= c.V {
h.Log(ctx, _infoLevel, KVString(_log, fmt.Sprintf(format, args...)))
}
}
// Errorc logs a message at the error log level.
func Errorc(ctx context.Context, format string, args ...interface{}) {
h.Log(ctx, _errorLevel, KVString(_log, fmt.Sprintf(format, args...)))
if int32(_errorLevel) >= c.V {
h.Log(ctx, _errorLevel, KVString(_log, fmt.Sprintf(format, args...)))
}
}
// Warnc logs a message at the warning log level.
func Warnc(ctx context.Context, format string, args ...interface{}) {
h.Log(ctx, _warnLevel, KVString(_log, fmt.Sprintf(format, args...)))
if int32(_warnLevel) >= c.V {
h.Log(ctx, _warnLevel, KVString(_log, fmt.Sprintf(format, args...)))
}
}
// Fatalc logs a message at the fatal log level.
func Fatalc(ctx context.Context, format string, args ...interface{}) {
if int32(_fatalLevel) >= c.V {
h.Log(ctx, _fatalLevel, KVString(_log, fmt.Sprintf(format, args...)))
}
}
// Debugv logs a message at the debug log level.
func Debugv(ctx context.Context, args ...D) {
if int32(_debugLevel) >= c.V {
h.Log(ctx, _debugLevel, args...)
}
}
// Infov logs a message at the info log level.
func Infov(ctx context.Context, args ...D) {
h.Log(ctx, _infoLevel, args...)
if int32(_infoLevel) >= c.V {
h.Log(ctx, _infoLevel, args...)
}
}
// Warnv logs a message at the warning log level.
func Warnv(ctx context.Context, args ...D) {
h.Log(ctx, _warnLevel, args...)
if int32(_warnLevel) >= c.V {
h.Log(ctx, _warnLevel, args...)
}
}
// Errorv logs a message at the error log level.
func Errorv(ctx context.Context, args ...D) {
h.Log(ctx, _errorLevel, args...)
if int32(_errorLevel) >= c.V {
h.Log(ctx, _errorLevel, args...)
}
}
// Fatalv logs a message at the error log level.
func Fatalv(ctx context.Context, args ...D) {
if int32(_fatalLevel) >= c.V {
h.Log(ctx, _fatalLevel, args...)
}
}
func logw(args []interface{}) []D {
@ -198,19 +258,39 @@ func logw(args []interface{}) []D {
return ds
}
// Debugw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func Debugw(ctx context.Context, args ...interface{}) {
if int32(_debugLevel) >= c.V {
h.Log(ctx, _debugLevel, logw(args)...)
}
}
// Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func Infow(ctx context.Context, args ...interface{}) {
h.Log(ctx, _infoLevel, logw(args)...)
if int32(_infoLevel) >= c.V {
h.Log(ctx, _infoLevel, logw(args)...)
}
}
// Warnw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func Warnw(ctx context.Context, args ...interface{}) {
h.Log(ctx, _warnLevel, logw(args)...)
if int32(_warnLevel) >= c.V {
h.Log(ctx, _warnLevel, logw(args)...)
}
}
// Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func Errorw(ctx context.Context, args ...interface{}) {
h.Log(ctx, _errorLevel, logw(args)...)
if int32(_errorLevel) >= c.V {
h.Log(ctx, _errorLevel, logw(args)...)
}
}
// Fatalw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func Fatalw(ctx context.Context, args ...interface{}) {
if int32(_fatalLevel) >= c.V {
h.Log(ctx, _fatalLevel, logw(args)...)
}
}
// SetFormat only effective on stdout and file handler

@ -33,6 +33,11 @@ type TestLog struct {
}
func testLog(t *testing.T) {
t.Run("Fatal", func(t *testing.T) {
Fatal("hello %s", "world")
Fatalv(context.Background(), KV("key", 2222222), KV("test2", "test"))
Fatalc(context.Background(), "keys: %s %s...", "key1", "key2")
})
t.Run("Error", func(t *testing.T) {
Error("hello %s", "world")
Errorv(context.Background(), KV("key", 2222222), KV("test2", "test"))
@ -48,6 +53,11 @@ func testLog(t *testing.T) {
Infov(context.Background(), KV("key", 2222222), KV("test2", "test"))
Infoc(context.Background(), "keys: %s %s...", "key1", "key2")
})
t.Run("Debug", func(t *testing.T) {
Debug("hello %s", "world")
Debugv(context.Background(), KV("key", 2222222), KV("test2", "test"))
Debugc(context.Background(), "keys: %s %s...", "key1", "key2")
})
}
func TestFile(t *testing.T) {

Loading…
Cancel
Save