2021-10-28 00:52:30 +03:00
|
|
|
package zerolog // import "go.unistack.org/micro-logger-zerolog/v3"
|
2020-02-11 13:49:33 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-02-14 08:04:50 +03:00
|
|
|
"runtime/debug"
|
2020-02-21 12:31:16 +03:00
|
|
|
"time"
|
2020-02-11 13:49:33 +03:00
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
2020-02-14 08:04:50 +03:00
|
|
|
zlog "github.com/rs/zerolog/log"
|
|
|
|
"github.com/rs/zerolog/pkgerrors"
|
2021-10-28 00:52:30 +03:00
|
|
|
"go.unistack.org/micro/v3/logger"
|
2020-02-14 08:04:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Mode uint8
|
|
|
|
|
|
|
|
const (
|
2020-02-21 12:31:16 +03:00
|
|
|
Production Mode = iota
|
2020-02-14 08:04:50 +03:00
|
|
|
Development
|
2020-02-11 13:49:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type zeroLogger struct {
|
2020-02-21 12:31:16 +03:00
|
|
|
zLog zerolog.Logger
|
|
|
|
opts Options
|
2020-02-14 08:04:50 +03:00
|
|
|
}
|
|
|
|
|
2021-10-28 00:52:30 +03:00
|
|
|
func (l *zeroLogger) Level(lvl logger.Level) {
|
|
|
|
}
|
|
|
|
|
2021-09-16 11:06:41 +03:00
|
|
|
func (l *zeroLogger) Clone(opts ...logger.Option) logger.Logger {
|
2021-10-28 00:52:30 +03:00
|
|
|
nl := &zeroLogger{zLog: l.zLog, opts: l.opts}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&nl.opts.Options)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = nl.Init()
|
|
|
|
return nl
|
2021-09-16 11:06:41 +03:00
|
|
|
}
|
|
|
|
|
2020-02-11 13:49:33 +03:00
|
|
|
func (l *zeroLogger) Init(opts ...logger.Option) error {
|
|
|
|
for _, o := range opts {
|
2020-02-21 12:31:16 +03:00
|
|
|
o(&l.opts.Options)
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 15:34:37 +03:00
|
|
|
if zl, ok := l.opts.Context.Value(loggerKey{}).(zerolog.Logger); ok {
|
|
|
|
l.zLog = zl
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
if hs, ok := l.opts.Context.Value(hooksKey{}).([]zerolog.Hook); ok {
|
|
|
|
l.opts.Hooks = hs
|
2020-02-14 08:04:50 +03:00
|
|
|
}
|
2020-02-21 12:31:16 +03:00
|
|
|
if tf, ok := l.opts.Context.Value(timeFormatKey{}).(string); ok {
|
|
|
|
l.opts.TimeFormat = tf
|
2020-02-14 08:04:50 +03:00
|
|
|
}
|
2020-02-21 12:31:16 +03:00
|
|
|
if exitFunction, ok := l.opts.Context.Value(exitKey{}).(func(int)); ok {
|
|
|
|
l.opts.ExitFunc = exitFunction
|
2020-02-14 08:04:50 +03:00
|
|
|
}
|
2020-02-21 12:31:16 +03:00
|
|
|
if caller, ok := l.opts.Context.Value(reportCallerKey{}).(bool); ok && caller {
|
|
|
|
l.opts.ReportCaller = caller
|
2020-02-14 08:04:50 +03:00
|
|
|
}
|
2020-02-21 12:31:16 +03:00
|
|
|
if useDefault, ok := l.opts.Context.Value(useAsDefaultKey{}).(bool); ok && useDefault {
|
|
|
|
l.opts.UseAsDefault = useDefault
|
2020-02-14 08:04:50 +03:00
|
|
|
}
|
2020-02-21 12:31:16 +03:00
|
|
|
if devMode, ok := l.opts.Context.Value(developmentModeKey{}).(bool); ok && devMode {
|
|
|
|
l.opts.Mode = Development
|
2020-02-14 08:04:50 +03:00
|
|
|
}
|
2020-02-21 12:31:16 +03:00
|
|
|
if prodMode, ok := l.opts.Context.Value(productionModeKey{}).(bool); ok && prodMode {
|
|
|
|
l.opts.Mode = Production
|
2020-02-14 08:04:50 +03:00
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
// RESET
|
|
|
|
zerolog.TimeFieldFormat = time.RFC3339
|
|
|
|
zerolog.ErrorStackMarshaler = nil
|
2020-07-06 15:59:57 +03:00
|
|
|
zerolog.CallerSkipFrameCount = 4
|
2020-02-21 12:31:16 +03:00
|
|
|
|
|
|
|
switch l.opts.Mode {
|
2020-02-14 08:04:50 +03:00
|
|
|
case Development:
|
|
|
|
zerolog.ErrorStackMarshaler = func(err error) interface{} {
|
|
|
|
fmt.Println(string(debug.Stack()))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
consOut := zerolog.NewConsoleWriter(
|
2020-02-11 13:49:33 +03:00
|
|
|
func(w *zerolog.ConsoleWriter) {
|
2020-02-21 12:31:16 +03:00
|
|
|
if len(l.opts.TimeFormat) > 0 {
|
|
|
|
w.TimeFormat = l.opts.TimeFormat
|
2020-02-15 09:56:46 +03:00
|
|
|
}
|
2020-02-21 12:31:16 +03:00
|
|
|
w.Out = l.opts.Out
|
2020-02-14 08:04:50 +03:00
|
|
|
w.NoColor = false
|
2020-02-11 13:49:33 +03:00
|
|
|
},
|
|
|
|
)
|
2021-09-16 11:06:41 +03:00
|
|
|
// level = logger.DebugLevel
|
2020-02-21 12:31:16 +03:00
|
|
|
l.zLog = zerolog.New(consOut).
|
2020-02-14 08:04:50 +03:00
|
|
|
Level(zerolog.DebugLevel).
|
|
|
|
With().Timestamp().Stack().Logger()
|
|
|
|
default: // Production
|
|
|
|
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
|
2020-02-21 12:31:16 +03:00
|
|
|
l.zLog = zerolog.New(l.opts.Out).
|
2020-02-14 08:04:50 +03:00
|
|
|
Level(zerolog.InfoLevel).
|
|
|
|
With().Timestamp().Stack().Logger()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set log Level if not default
|
2020-02-21 12:31:16 +03:00
|
|
|
if l.opts.Level != 100 {
|
|
|
|
zerolog.SetGlobalLevel(loggerToZerologLevel(l.opts.Level))
|
|
|
|
l.zLog = l.zLog.Level(loggerToZerologLevel(l.opts.Level))
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
|
|
|
|
2020-07-06 15:59:57 +03:00
|
|
|
// Reporting caller
|
2020-02-21 12:31:16 +03:00
|
|
|
if l.opts.ReportCaller {
|
|
|
|
l.zLog = l.zLog.With().Caller().Logger()
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
2020-07-06 15:59:57 +03:00
|
|
|
|
|
|
|
// Adding hooks if exist
|
2020-02-21 12:31:16 +03:00
|
|
|
for _, hook := range l.opts.Hooks {
|
|
|
|
l.zLog = l.zLog.Hook(hook)
|
2020-02-14 08:04:50 +03:00
|
|
|
}
|
2020-02-11 13:49:33 +03:00
|
|
|
|
2020-02-14 08:04:50 +03:00
|
|
|
// Setting timeFormat
|
2020-02-21 12:31:16 +03:00
|
|
|
if len(l.opts.TimeFormat) > 0 {
|
|
|
|
zerolog.TimeFieldFormat = l.opts.TimeFormat
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
|
|
|
|
2020-02-14 08:04:50 +03:00
|
|
|
// Adding seed fields if exist
|
2020-02-21 12:31:16 +03:00
|
|
|
if l.opts.Fields != nil {
|
2021-08-06 02:50:15 +03:00
|
|
|
data := make(map[string]interface{}, len(l.opts.Fields)/2)
|
|
|
|
for i := 0; i < len(l.opts.Fields); i += 2 {
|
|
|
|
data[l.opts.Fields[i].(string)] = l.opts.Fields[i+1]
|
|
|
|
}
|
|
|
|
l.zLog = l.zLog.With().Fields(data).Logger()
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 09:56:46 +03:00
|
|
|
// Also set it as zerolog's Default logger
|
2020-02-21 12:31:16 +03:00
|
|
|
if l.opts.UseAsDefault {
|
|
|
|
zlog.Logger = l.zLog
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
2020-02-14 08:04:50 +03:00
|
|
|
|
2020-02-11 13:49:33 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-06 02:50:15 +03:00
|
|
|
func (l *zeroLogger) Fields(fields ...interface{}) logger.Logger {
|
|
|
|
data := make(map[string]interface{}, len(fields)/2)
|
|
|
|
for i := 0; i < len(fields); i += 2 {
|
|
|
|
data[fields[i].(string)] = fields[i+1]
|
|
|
|
}
|
|
|
|
l.zLog = l.zLog.With().Fields(data).Logger()
|
2020-02-21 12:31:16 +03:00
|
|
|
return l
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 15:34:37 +03:00
|
|
|
func (l *zeroLogger) V(level logger.Level) bool {
|
2022-01-20 01:02:58 +03:00
|
|
|
return l.zLog.GetLevel() <= loggerToZerologLevel(level)
|
2021-01-10 15:34:37 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:53:50 +03:00
|
|
|
func (l *zeroLogger) Info(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, logger.InfoLevel, args...)
|
2021-01-10 15:34:37 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:53:50 +03:00
|
|
|
func (l *zeroLogger) Error(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, logger.ErrorLevel, args...)
|
2021-01-10 15:34:37 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:53:50 +03:00
|
|
|
func (l *zeroLogger) Warn(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, logger.WarnLevel, args...)
|
2021-01-10 15:34:37 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:53:50 +03:00
|
|
|
func (l *zeroLogger) Debug(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, logger.DebugLevel, args...)
|
2021-01-10 15:34:37 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:53:50 +03:00
|
|
|
func (l *zeroLogger) Trace(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, logger.TraceLevel, args...)
|
2021-01-10 15:34:37 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:53:50 +03:00
|
|
|
func (l *zeroLogger) Fatal(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, logger.FatalLevel, args...)
|
2021-01-10 15:34:37 +03:00
|
|
|
// Invoke os.Exit because unlike zerolog.Logger.Fatal zerolog.Logger.WithLevel won't stop the execution.
|
|
|
|
l.opts.ExitFunc(1)
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:53:50 +03:00
|
|
|
func (l *zeroLogger) Infof(ctx context.Context, msg string, args ...interface{}) {
|
|
|
|
l.Logf(ctx, logger.InfoLevel, msg, args...)
|
2021-01-10 15:34:37 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:53:50 +03:00
|
|
|
func (l *zeroLogger) Errorf(ctx context.Context, msg string, args ...interface{}) {
|
|
|
|
l.Logf(ctx, logger.ErrorLevel, msg, args...)
|
2021-01-10 15:34:37 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:53:50 +03:00
|
|
|
func (l *zeroLogger) Warnf(ctx context.Context, msg string, args ...interface{}) {
|
|
|
|
l.Logf(ctx, logger.WarnLevel, msg, args...)
|
2021-01-10 15:34:37 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:53:50 +03:00
|
|
|
func (l *zeroLogger) Debugf(ctx context.Context, msg string, args ...interface{}) {
|
|
|
|
l.Logf(ctx, logger.DebugLevel, msg, args...)
|
2021-01-10 15:34:37 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:53:50 +03:00
|
|
|
func (l *zeroLogger) Tracef(ctx context.Context, msg string, args ...interface{}) {
|
|
|
|
l.Logf(ctx, logger.TraceLevel, msg, args...)
|
2021-01-10 15:34:37 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:53:50 +03:00
|
|
|
func (l *zeroLogger) Fatalf(ctx context.Context, msg string, args ...interface{}) {
|
|
|
|
l.Logf(ctx, logger.FatalLevel, msg, args...)
|
2021-01-10 15:34:37 +03:00
|
|
|
// Invoke os.Exit because unlike zerolog.Logger.Fatal zerolog.Logger.WithLevel won't stop the execution.
|
|
|
|
l.opts.ExitFunc(1)
|
|
|
|
}
|
|
|
|
|
2021-01-12 09:53:50 +03:00
|
|
|
func (l *zeroLogger) Log(ctx context.Context, level logger.Level, args ...interface{}) {
|
|
|
|
if !l.V(level) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
msg := fmt.Sprint(args...)
|
|
|
|
l.zLog.WithLevel(loggerToZerologLevel(level)).Msg(msg)
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:53:50 +03:00
|
|
|
func (l *zeroLogger) Logf(ctx context.Context, level logger.Level, format string, args ...interface{}) {
|
|
|
|
if !l.V(level) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
l.zLog.WithLevel(loggerToZerologLevel(level)).Msgf(format, args...)
|
2020-02-11 13:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *zeroLogger) String() string {
|
|
|
|
return "zerolog"
|
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
func (l *zeroLogger) Options() logger.Options {
|
|
|
|
return l.opts.Options
|
|
|
|
}
|
|
|
|
|
2020-02-11 13:49:33 +03:00
|
|
|
// NewLogger builds a new logger based on options
|
|
|
|
func NewLogger(opts ...logger.Option) logger.Logger {
|
2020-02-21 12:31:16 +03:00
|
|
|
// Default options
|
|
|
|
options := Options{
|
2021-01-12 10:23:17 +03:00
|
|
|
Options: logger.NewOptions(opts...),
|
2020-02-21 12:31:16 +03:00
|
|
|
ReportCaller: false,
|
|
|
|
UseAsDefault: false,
|
|
|
|
Mode: Production,
|
|
|
|
ExitFunc: os.Exit,
|
|
|
|
}
|
|
|
|
|
|
|
|
l := &zeroLogger{opts: options}
|
2020-02-11 13:49:33 +03:00
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func loggerToZerologLevel(level logger.Level) zerolog.Level {
|
|
|
|
switch level {
|
|
|
|
case logger.TraceLevel:
|
|
|
|
return zerolog.TraceLevel
|
|
|
|
case logger.DebugLevel:
|
|
|
|
return zerolog.DebugLevel
|
|
|
|
case logger.InfoLevel:
|
|
|
|
return zerolog.InfoLevel
|
|
|
|
case logger.WarnLevel:
|
|
|
|
return zerolog.WarnLevel
|
|
|
|
case logger.ErrorLevel:
|
|
|
|
return zerolog.ErrorLevel
|
|
|
|
case logger.FatalLevel:
|
|
|
|
return zerolog.FatalLevel
|
|
|
|
default:
|
|
|
|
return zerolog.InfoLevel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ZerologToLoggerLevel(level zerolog.Level) logger.Level {
|
|
|
|
switch level {
|
|
|
|
case zerolog.TraceLevel:
|
|
|
|
return logger.TraceLevel
|
|
|
|
case zerolog.DebugLevel:
|
|
|
|
return logger.DebugLevel
|
|
|
|
case zerolog.InfoLevel:
|
|
|
|
return logger.InfoLevel
|
|
|
|
case zerolog.WarnLevel:
|
|
|
|
return logger.WarnLevel
|
|
|
|
case zerolog.ErrorLevel:
|
|
|
|
return logger.ErrorLevel
|
|
|
|
case zerolog.FatalLevel:
|
|
|
|
return logger.FatalLevel
|
|
|
|
default:
|
|
|
|
return logger.InfoLevel
|
|
|
|
}
|
|
|
|
}
|