2021-10-28 00:47:01 +03:00
|
|
|
package zap // import "go.unistack.org/micro-logger-zap/v3"
|
2020-02-09 19:46:33 +03:00
|
|
|
|
|
|
|
import (
|
2021-01-12 09:45:13 +03:00
|
|
|
"context"
|
2020-02-09 19:46:33 +03:00
|
|
|
"fmt"
|
2020-02-21 12:31:16 +03:00
|
|
|
"os"
|
2020-03-04 01:04:22 +03:00
|
|
|
"sync"
|
2020-02-09 19:46:33 +03:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
2021-10-28 00:47:01 +03:00
|
|
|
"go.unistack.org/micro/v3/logger"
|
2020-02-09 19:46:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type zaplog struct {
|
2020-02-21 12:31:16 +03:00
|
|
|
zap *zap.Logger
|
|
|
|
opts logger.Options
|
2020-03-04 01:04:22 +03:00
|
|
|
sync.RWMutex
|
2021-10-28 00:47:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *zaplog) Level(lvl logger.Level) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *zaplog) Clone(opts ...logger.Option) logger.Logger {
|
|
|
|
nl := &zaplog{zap: l.zap, opts: l.opts}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&nl.opts)
|
|
|
|
}
|
|
|
|
return nl
|
2020-02-15 09:56:46 +03:00
|
|
|
}
|
|
|
|
|
2020-02-09 19:46:33 +03:00
|
|
|
func (l *zaplog) Init(opts ...logger.Option) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
for _, o := range opts {
|
2020-02-21 12:31:16 +03:00
|
|
|
o(&l.opts)
|
2020-02-09 19:46:33 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 15:41:04 +03:00
|
|
|
if zlog, ok := l.opts.Context.Value(loggerKey{}).(*zap.Logger); ok {
|
|
|
|
l.zap = zlog
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-09 19:46:33 +03:00
|
|
|
zapConfig := zap.NewProductionConfig()
|
2020-02-21 12:31:16 +03:00
|
|
|
if zconfig, ok := l.opts.Context.Value(configKey{}).(zap.Config); ok {
|
2020-02-09 19:46:33 +03:00
|
|
|
zapConfig = zconfig
|
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
if zcconfig, ok := l.opts.Context.Value(encoderConfigKey{}).(zapcore.EncoderConfig); ok {
|
2020-02-09 19:46:33 +03:00
|
|
|
zapConfig.EncoderConfig = zcconfig
|
|
|
|
}
|
|
|
|
|
2020-03-04 01:04:22 +03:00
|
|
|
skip, ok := l.opts.Context.Value(callerSkipKey{}).(int)
|
|
|
|
if !ok || skip < 1 {
|
|
|
|
skip = 1
|
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
// Set log Level if not default
|
2020-02-09 19:46:33 +03:00
|
|
|
zapConfig.Level = zap.NewAtomicLevel()
|
2020-02-21 12:31:16 +03:00
|
|
|
if l.opts.Level != logger.InfoLevel {
|
|
|
|
zapConfig.Level.SetLevel(loggerToZapLevel(l.opts.Level))
|
2020-02-09 19:46:33 +03:00
|
|
|
}
|
|
|
|
|
2020-03-04 01:04:22 +03:00
|
|
|
log, err := zapConfig.Build(zap.AddCallerSkip(skip))
|
2020-02-09 19:46:33 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-10 02:22:48 +03:00
|
|
|
log = log.WithOptions(zap.WrapCore(func(c zapcore.Core) zapcore.Core {
|
|
|
|
return zapcore.NewCore(
|
|
|
|
zapcore.NewJSONEncoder(zapConfig.EncoderConfig),
|
|
|
|
zapcore.Lock(zapcore.AddSync(l.opts.Out)),
|
|
|
|
loggerToZapLevel(l.opts.Level),
|
|
|
|
)
|
|
|
|
}))
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
// Adding seed fields if exist
|
|
|
|
if l.opts.Fields != nil {
|
2021-08-06 02:41:38 +03:00
|
|
|
data := make([]zap.Field, 0, len(l.opts.Fields)/2)
|
|
|
|
for i := 0; i < len(l.opts.Fields); i += 2 {
|
|
|
|
data = append(data, zap.Any(l.opts.Fields[i].(string), l.opts.Fields[i+1]))
|
2020-02-21 12:31:16 +03:00
|
|
|
}
|
|
|
|
log = log.With(data...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adding namespace
|
|
|
|
if namespace, ok := l.opts.Context.Value(namespaceKey{}).(string); ok {
|
|
|
|
log = log.With(zap.Namespace(namespace))
|
|
|
|
}
|
|
|
|
|
|
|
|
// defer log.Sync() ??
|
|
|
|
|
2020-02-09 19:46:33 +03:00
|
|
|
l.zap = log
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-06 02:41:38 +03:00
|
|
|
func (l *zaplog) Fields(fields ...interface{}) logger.Logger {
|
|
|
|
data := make([]zap.Field, 0, len(fields)/2)
|
|
|
|
for i := 0; i < len(fields); i += 2 {
|
|
|
|
data = append(data, zap.Any(fields[i].(string), fields[i+1]))
|
2020-03-04 13:18:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
zl := &zaplog{
|
2021-08-06 02:41:38 +03:00
|
|
|
zap: l.zap.With(data...),
|
|
|
|
opts: l.opts,
|
2020-03-04 13:18:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return zl
|
2020-02-09 19:46:33 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:45:13 +03:00
|
|
|
func (l *zaplog) Errorf(ctx context.Context, msg string, args ...interface{}) {
|
|
|
|
l.Logf(ctx, logger.ErrorLevel, msg, args...)
|
2021-01-10 15:41:04 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:45:13 +03:00
|
|
|
func (l *zaplog) Debugf(ctx context.Context, msg string, args ...interface{}) {
|
|
|
|
l.Logf(ctx, logger.DebugLevel, msg, args...)
|
2021-01-10 15:41:04 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:45:13 +03:00
|
|
|
func (l *zaplog) Infof(ctx context.Context, msg string, args ...interface{}) {
|
|
|
|
l.Logf(ctx, logger.InfoLevel, msg, args...)
|
2021-01-10 15:41:04 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:45:13 +03:00
|
|
|
func (l *zaplog) Fatalf(ctx context.Context, msg string, args ...interface{}) {
|
|
|
|
l.Logf(ctx, logger.FatalLevel, msg, args...)
|
2021-01-10 15:41:04 +03:00
|
|
|
os.Exit(1)
|
2020-02-09 19:46:33 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:45:13 +03:00
|
|
|
func (l *zaplog) Tracef(ctx context.Context, msg string, args ...interface{}) {
|
|
|
|
l.Logf(ctx, logger.TraceLevel, msg, args...)
|
2021-01-10 15:41:04 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:45:13 +03:00
|
|
|
func (l *zaplog) Warnf(ctx context.Context, msg string, args ...interface{}) {
|
|
|
|
l.Logf(ctx, logger.WarnLevel, msg, args...)
|
2021-01-10 15:41:04 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:45:13 +03:00
|
|
|
func (l *zaplog) Error(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, logger.ErrorLevel, args...)
|
2021-01-10 15:41:04 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:45:13 +03:00
|
|
|
func (l *zaplog) Debug(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, logger.DebugLevel, args...)
|
2021-01-10 15:41:04 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:45:13 +03:00
|
|
|
func (l *zaplog) Info(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, logger.InfoLevel, args...)
|
2021-01-10 15:41:04 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:45:13 +03:00
|
|
|
func (l *zaplog) Fatal(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, logger.FatalLevel, args...)
|
2021-01-10 15:41:04 +03:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-01-12 09:45:13 +03:00
|
|
|
func (l *zaplog) Trace(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, logger.TraceLevel, args...)
|
2021-01-10 15:41:04 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:45:13 +03:00
|
|
|
func (l *zaplog) Warn(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, logger.WarnLevel, args...)
|
2021-01-10 15:41:04 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:45:13 +03:00
|
|
|
func (l *zaplog) Log(ctx context.Context, level logger.Level, args ...interface{}) {
|
2021-01-10 15:41:04 +03:00
|
|
|
if !l.V(level) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
msg := fmt.Sprint(args...)
|
2021-01-10 15:41:04 +03:00
|
|
|
switch loggerToZapLevel(level) {
|
2020-02-09 19:46:33 +03:00
|
|
|
case zap.DebugLevel:
|
2021-10-28 00:47:01 +03:00
|
|
|
l.zap.Debug(msg)
|
2020-02-09 19:46:33 +03:00
|
|
|
case zap.InfoLevel:
|
2021-10-28 00:47:01 +03:00
|
|
|
l.zap.Info(msg)
|
2020-02-09 19:46:33 +03:00
|
|
|
case zap.WarnLevel:
|
2021-10-28 00:47:01 +03:00
|
|
|
l.zap.Warn(msg)
|
2020-02-09 19:46:33 +03:00
|
|
|
case zap.ErrorLevel:
|
2021-10-28 00:47:01 +03:00
|
|
|
l.zap.Error(msg)
|
2020-02-09 19:46:33 +03:00
|
|
|
case zap.FatalLevel:
|
2021-10-28 00:47:01 +03:00
|
|
|
l.zap.Fatal(msg)
|
2020-02-09 19:46:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 09:45:13 +03:00
|
|
|
func (l *zaplog) Logf(ctx context.Context, level logger.Level, format string, args ...interface{}) {
|
2021-01-10 15:41:04 +03:00
|
|
|
if !l.V(level) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-09 19:46:33 +03:00
|
|
|
msg := fmt.Sprintf(format, args...)
|
2021-01-10 15:41:04 +03:00
|
|
|
switch loggerToZapLevel(level) {
|
2020-02-09 19:46:33 +03:00
|
|
|
case zap.DebugLevel:
|
2021-10-28 00:47:01 +03:00
|
|
|
l.zap.Debug(msg)
|
2020-02-09 19:46:33 +03:00
|
|
|
case zap.InfoLevel:
|
2021-10-28 00:47:01 +03:00
|
|
|
l.zap.Info(msg)
|
2020-02-09 19:46:33 +03:00
|
|
|
case zap.WarnLevel:
|
2021-10-28 00:47:01 +03:00
|
|
|
l.zap.Warn(msg)
|
2020-02-09 19:46:33 +03:00
|
|
|
case zap.ErrorLevel:
|
2021-10-28 00:47:01 +03:00
|
|
|
l.zap.Error(msg)
|
2020-02-09 19:46:33 +03:00
|
|
|
case zap.FatalLevel:
|
2021-10-28 00:47:01 +03:00
|
|
|
l.zap.Fatal(msg)
|
2020-02-09 19:46:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 15:41:04 +03:00
|
|
|
func (l *zaplog) V(level logger.Level) bool {
|
|
|
|
return l.zap.Core().Enabled(loggerToZapLevel(level))
|
|
|
|
}
|
|
|
|
|
2020-02-09 19:46:33 +03:00
|
|
|
func (l *zaplog) String() string {
|
|
|
|
return "zap"
|
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
func (l *zaplog) Options() logger.Options {
|
|
|
|
return l.opts
|
|
|
|
}
|
|
|
|
|
2020-02-09 19:46:33 +03:00
|
|
|
// New builds a new logger based on options
|
2021-01-10 15:41:04 +03:00
|
|
|
func NewLogger(opts ...logger.Option) logger.Logger {
|
|
|
|
l := &zaplog{opts: logger.NewOptions(opts...)}
|
|
|
|
return l
|
2020-02-09 19:46:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func loggerToZapLevel(level logger.Level) zapcore.Level {
|
|
|
|
switch level {
|
|
|
|
case logger.TraceLevel, logger.DebugLevel:
|
|
|
|
return zap.DebugLevel
|
|
|
|
case logger.InfoLevel:
|
|
|
|
return zap.InfoLevel
|
|
|
|
case logger.WarnLevel:
|
|
|
|
return zap.WarnLevel
|
|
|
|
case logger.ErrorLevel:
|
|
|
|
return zap.ErrorLevel
|
|
|
|
case logger.FatalLevel:
|
|
|
|
return zap.FatalLevel
|
|
|
|
default:
|
|
|
|
return zap.InfoLevel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 15:41:04 +03:00
|
|
|
/*
|
2020-02-09 19:46:33 +03:00
|
|
|
func zapToLoggerLevel(level zapcore.Level) logger.Level {
|
|
|
|
switch level {
|
|
|
|
case zap.DebugLevel:
|
|
|
|
return logger.DebugLevel
|
|
|
|
case zap.InfoLevel:
|
|
|
|
return logger.InfoLevel
|
|
|
|
case zap.WarnLevel:
|
|
|
|
return logger.WarnLevel
|
|
|
|
case zap.ErrorLevel:
|
|
|
|
return logger.ErrorLevel
|
|
|
|
case zap.FatalLevel:
|
|
|
|
return logger.FatalLevel
|
|
|
|
default:
|
|
|
|
return logger.InfoLevel
|
|
|
|
}
|
|
|
|
}
|
2021-01-10 15:41:04 +03:00
|
|
|
*/
|