mirror of
https://github.com/go-kratos/kratos.git
synced 2025-03-17 21:07:54 +02:00
parent
ecd7533e7f
commit
0f4fa0ad7c
@ -11,16 +11,20 @@ import (
|
|||||||
|
|
||||||
// level idx
|
// level idx
|
||||||
const (
|
const (
|
||||||
_infoIdx = iota
|
_debugIdx = iota
|
||||||
|
_infoIdx
|
||||||
_warnIdx
|
_warnIdx
|
||||||
_errorIdx
|
_errorIdx
|
||||||
|
_fatalIdx
|
||||||
_totalIdx
|
_totalIdx
|
||||||
)
|
)
|
||||||
|
|
||||||
var _fileNames = map[int]string{
|
var _fileNames = map[int]string{
|
||||||
|
_debugIdx: "debug.log",
|
||||||
_infoIdx: "info.log",
|
_infoIdx: "info.log",
|
||||||
_warnIdx: "warning.log",
|
_warnIdx: "warning.log",
|
||||||
_errorIdx: "error.log",
|
_errorIdx: "error.log",
|
||||||
|
_fatalIdx: "fatal.log",
|
||||||
}
|
}
|
||||||
|
|
||||||
// FileHandler .
|
// FileHandler .
|
||||||
@ -63,10 +67,14 @@ func (h *FileHandler) Log(ctx context.Context, lv Level, args ...D) {
|
|||||||
d[_time] = time.Now().Format(_timeFormat)
|
d[_time] = time.Now().Format(_timeFormat)
|
||||||
var w io.Writer
|
var w io.Writer
|
||||||
switch lv {
|
switch lv {
|
||||||
|
case _debugLevel:
|
||||||
|
w = h.fws[_debugIdx]
|
||||||
case _warnLevel:
|
case _warnLevel:
|
||||||
w = h.fws[_warnIdx]
|
w = h.fws[_warnIdx]
|
||||||
case _errorLevel:
|
case _errorLevel:
|
||||||
w = h.fws[_errorIdx]
|
w = h.fws[_errorIdx]
|
||||||
|
case _fatalLevel:
|
||||||
|
w = h.fws[_fatalIdx]
|
||||||
default:
|
default:
|
||||||
w = h.fws[_infoIdx]
|
w = h.fws[_infoIdx]
|
||||||
}
|
}
|
||||||
|
104
pkg/log/log.go
104
pkg/log/log.go
@ -138,49 +138,109 @@ func Init(conf *Config) {
|
|||||||
c = conf
|
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.
|
// Info logs a message at the info log level.
|
||||||
func Info(format string, args ...interface{}) {
|
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.
|
// Warn logs a message at the warning log level.
|
||||||
func Warn(format string, args ...interface{}) {
|
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.
|
// Error logs a message at the error log level.
|
||||||
func Error(format string, args ...interface{}) {
|
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.
|
// Infoc logs a message at the info log level.
|
||||||
func Infoc(ctx context.Context, format string, args ...interface{}) {
|
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.
|
// Errorc logs a message at the error log level.
|
||||||
func Errorc(ctx context.Context, format string, args ...interface{}) {
|
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.
|
// Warnc logs a message at the warning log level.
|
||||||
func Warnc(ctx context.Context, format string, args ...interface{}) {
|
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.
|
// Infov logs a message at the info log level.
|
||||||
func Infov(ctx context.Context, args ...D) {
|
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.
|
// Warnv logs a message at the warning log level.
|
||||||
func Warnv(ctx context.Context, args ...D) {
|
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.
|
// Errorv logs a message at the error log level.
|
||||||
func Errorv(ctx context.Context, args ...D) {
|
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 {
|
func logw(args []interface{}) []D {
|
||||||
@ -198,19 +258,39 @@ func logw(args []interface{}) []D {
|
|||||||
return ds
|
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.
|
// 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{}) {
|
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.
|
// 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{}) {
|
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.
|
// 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{}) {
|
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
|
// SetFormat only effective on stdout and file handler
|
||||||
|
@ -33,6 +33,11 @@ type TestLog struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testLog(t *testing.T) {
|
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) {
|
t.Run("Error", func(t *testing.T) {
|
||||||
Error("hello %s", "world")
|
Error("hello %s", "world")
|
||||||
Errorv(context.Background(), KV("key", 2222222), KV("test2", "test"))
|
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"))
|
Infov(context.Background(), KV("key", 2222222), KV("test2", "test"))
|
||||||
Infoc(context.Background(), "keys: %s %s...", "key1", "key2")
|
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) {
|
func TestFile(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user