2020-02-09 19:46:33 +03:00
|
|
|
package zap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"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"
|
2020-02-21 12:31:16 +03:00
|
|
|
|
|
|
|
"github.com/micro/go-micro/v2/logger"
|
2020-02-09 19:46:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type zaplog struct {
|
2020-02-21 12:31:16 +03:00
|
|
|
cfg zap.Config
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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.cfg = zapConfig
|
|
|
|
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()
|
|
|
|
l.fields = make(map[string]interface{}, len(fields))
|
2020-02-21 12:31:16 +03:00
|
|
|
for k, v := range fields {
|
2020-03-04 01:04:22 +03:00
|
|
|
l.fields[k] = v
|
2020-02-21 12:31:16 +03:00
|
|
|
}
|
2020-03-04 01:04:22 +03:00
|
|
|
l.Unlock()
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
return l
|
2020-02-09 19:46:33 +03:00
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
func (l *zaplog) Error(err error) logger.Logger {
|
2020-03-04 01:04:22 +03:00
|
|
|
l.Lock()
|
|
|
|
l.fields["error"] = err
|
|
|
|
l.Unlock()
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
return l
|
2020-02-09 19:46:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *zaplog) Log(level logger.Level, args ...interface{}) {
|
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
|
|
|
lvl := loggerToZapLevel(level)
|
2020-02-21 12:31:16 +03:00
|
|
|
msg := fmt.Sprint(args...)
|
2020-02-09 19:46:33 +03:00
|
|
|
switch lvl {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *zaplog) Logf(level logger.Level, format string, args ...interface{}) {
|
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
|
|
|
lvl := loggerToZapLevel(level)
|
|
|
|
msg := fmt.Sprintf(format, args...)
|
|
|
|
switch lvl {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
func NewLogger(opts ...logger.Option) (logger.Logger, error) {
|
2020-02-21 12:31:16 +03:00
|
|
|
// Default options
|
|
|
|
options := logger.Options{
|
|
|
|
Level: logger.InfoLevel,
|
|
|
|
Fields: make(map[string]interface{}),
|
|
|
|
Out: os.Stderr,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
|
|
|
|
l := &zaplog{opts: options}
|
|
|
|
if err := l.Init(opts...); err != nil {
|
2020-02-09 19:46:33 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|