2020-02-09 19:46:33 +03:00
|
|
|
package zap
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-01-10 15:41:04 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2020-02-09 19:46:33 +03:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
fields map[string]interface{}
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
// Adding seed fields if exist
|
|
|
|
if l.opts.Fields != nil {
|
|
|
|
data := []zap.Field{}
|
|
|
|
for k, v := range l.opts.Fields {
|
|
|
|
data = append(data, zap.Any(k, v))
|
|
|
|
}
|
|
|
|
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
|
2020-03-04 01:04:22 +03:00
|
|
|
l.fields = make(map[string]interface{})
|
2020-02-09 19:46:33 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
func (l *zaplog) Fields(fields map[string]interface{}) logger.Logger {
|
2020-03-04 01:04:22 +03:00
|
|
|
l.Lock()
|
2020-03-04 13:18:04 +03:00
|
|
|
nfields := make(map[string]interface{}, len(l.fields))
|
|
|
|
for k, v := range l.fields {
|
|
|
|
nfields[k] = v
|
2020-02-21 12:31:16 +03:00
|
|
|
}
|
2020-03-04 01:04:22 +03:00
|
|
|
l.Unlock()
|
2020-03-04 13:18:04 +03:00
|
|
|
for k, v := range fields {
|
|
|
|
nfields[k] = v
|
|
|
|
}
|
2020-03-04 01:04:22 +03:00
|
|
|
|
2020-03-04 13:18:04 +03:00
|
|
|
data := make([]zap.Field, 0, len(nfields))
|
|
|
|
for k, v := range fields {
|
|
|
|
data = append(data, zap.Any(k, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
zl := &zaplog{
|
|
|
|
zap: l.zap.With(data...),
|
|
|
|
opts: l.opts,
|
|
|
|
fields: make(map[string]interface{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-04 01:04:22 +03:00
|
|
|
l.RLock()
|
|
|
|
data := make([]zap.Field, 0, len(l.fields))
|
|
|
|
for k, v := range l.fields {
|
|
|
|
data = append(data, zap.Any(k, v))
|
|
|
|
}
|
|
|
|
l.RUnlock()
|
|
|
|
|
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:
|
2020-03-04 01:04:22 +03:00
|
|
|
l.zap.Debug(msg, data...)
|
2020-02-09 19:46:33 +03:00
|
|
|
case zap.InfoLevel:
|
2020-03-04 01:04:22 +03:00
|
|
|
l.zap.Info(msg, data...)
|
2020-02-09 19:46:33 +03:00
|
|
|
case zap.WarnLevel:
|
2020-03-04 01:04:22 +03:00
|
|
|
l.zap.Warn(msg, data...)
|
2020-02-09 19:46:33 +03:00
|
|
|
case zap.ErrorLevel:
|
2020-03-04 01:04:22 +03:00
|
|
|
l.zap.Error(msg, data...)
|
2020-02-09 19:46:33 +03:00
|
|
|
case zap.FatalLevel:
|
2020-03-04 01:04:22 +03:00
|
|
|
l.zap.Fatal(msg, data...)
|
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-03-04 01:04:22 +03:00
|
|
|
l.RLock()
|
|
|
|
data := make([]zap.Field, 0, len(l.fields))
|
|
|
|
for k, v := range l.fields {
|
|
|
|
data = append(data, zap.Any(k, v))
|
|
|
|
}
|
|
|
|
l.RUnlock()
|
|
|
|
|
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:
|
2020-03-04 01:04:22 +03:00
|
|
|
l.zap.Debug(msg, data...)
|
2020-02-09 19:46:33 +03:00
|
|
|
case zap.InfoLevel:
|
2020-03-04 01:04:22 +03:00
|
|
|
l.zap.Info(msg, data...)
|
2020-02-09 19:46:33 +03:00
|
|
|
case zap.WarnLevel:
|
2020-03-04 01:04:22 +03:00
|
|
|
l.zap.Warn(msg, data...)
|
2020-02-09 19:46:33 +03:00
|
|
|
case zap.ErrorLevel:
|
2020-03-04 01:04:22 +03:00
|
|
|
l.zap.Error(msg, data...)
|
2020-02-09 19:46:33 +03:00
|
|
|
case zap.FatalLevel:
|
2020-03-04 01:04:22 +03:00
|
|
|
l.zap.Fatal(msg, data...)
|
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
|
|
|
*/
|