parent
6c70049d2f
commit
48851a1ffd
@ -0,0 +1,53 @@ |
||||
package log |
||||
|
||||
import ( |
||||
"fmt" |
||||
"os" |
||||
|
||||
"github.com/go-kratos/kratos/v2/log" |
||||
"go.uber.org/zap" |
||||
"go.uber.org/zap/zapcore" |
||||
) |
||||
|
||||
var _ log.Logger = (*ZapLogger)(nil) |
||||
|
||||
type ZapLogger struct { |
||||
log *zap.Logger |
||||
Sync func() error |
||||
} |
||||
|
||||
// NewZapLogger return ZapLogger
|
||||
func NewZapLogger(encoder zapcore.EncoderConfig, level zap.AtomicLevel, opts ...zap.Option) *ZapLogger { |
||||
core := zapcore.NewCore( |
||||
zapcore.NewConsoleEncoder(encoder), |
||||
zapcore.NewMultiWriteSyncer( |
||||
zapcore.AddSync(os.Stdout), |
||||
), level) |
||||
zapLogger := zap.New(core, opts...) |
||||
return &ZapLogger{log: zapLogger, Sync: zapLogger.Sync} |
||||
} |
||||
|
||||
// Log Implementation of logger interface
|
||||
func (l *ZapLogger) Log(level log.Level, keyvals ...interface{}) error { |
||||
if len(keyvals) == 0 || len(keyvals)%2 != 0 { |
||||
l.log.Warn(fmt.Sprint("Keyvalues must appear in pairs: ", keyvals)) |
||||
return nil |
||||
} |
||||
|
||||
// Zap.Field is used when keyvals pairs appear
|
||||
var data []zap.Field |
||||
for i := 0; i < len(keyvals); i += 2 { |
||||
data = append(data, zap.Any(fmt.Sprint(keyvals[i]), fmt.Sprint(keyvals[i+1]))) |
||||
} |
||||
switch level { |
||||
case log.LevelDebug: |
||||
l.log.Debug("", data...) |
||||
case log.LevelInfo: |
||||
l.log.Info("", data...) |
||||
case log.LevelWarn: |
||||
l.log.Warn("", data...) |
||||
case log.LevelError: |
||||
l.log.Error("", data...) |
||||
} |
||||
return nil |
||||
} |
@ -0,0 +1,39 @@ |
||||
package log |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/go-kratos/kratos/v2/log" |
||||
"go.uber.org/zap" |
||||
"go.uber.org/zap/zapcore" |
||||
) |
||||
|
||||
func TestZapLogger(t *testing.T) { |
||||
encoder := zapcore.EncoderConfig{ |
||||
TimeKey: "t", |
||||
LevelKey: "level", |
||||
NameKey: "logger", |
||||
CallerKey: "caller", |
||||
MessageKey: "msg", |
||||
StacktraceKey: "stack", |
||||
EncodeTime: zapcore.ISO8601TimeEncoder, |
||||
LineEnding: zapcore.DefaultLineEnding, |
||||
EncodeLevel: zapcore.LowercaseLevelEncoder, |
||||
EncodeDuration: zapcore.SecondsDurationEncoder, |
||||
EncodeCaller: zapcore.FullCallerEncoder, |
||||
} |
||||
logger := NewZapLogger( |
||||
encoder, |
||||
zap.NewAtomicLevelAt(zapcore.DebugLevel), |
||||
zap.AddStacktrace( |
||||
zap.NewAtomicLevelAt(zapcore.ErrorLevel)), |
||||
zap.AddCaller(), |
||||
zap.AddCallerSkip(2), |
||||
zap.Development(), |
||||
) |
||||
zlog := log.NewHelper(logger) |
||||
zlog.Infow("name", "kratos", "from", "opensource") |
||||
zlog.Infow("name", "kratos", "from") |
||||
|
||||
defer logger.Sync() |
||||
} |
Loading…
Reference in new issue