2020-02-09 19:45:23 +03:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
2020-02-21 12:31:16 +03:00
|
|
|
"context"
|
2020-03-07 13:31:25 +03:00
|
|
|
"fmt"
|
2020-02-09 19:45:23 +03:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-01-10 15:21:29 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2020-02-09 19:45:23 +03:00
|
|
|
)
|
|
|
|
|
2021-01-10 15:21:29 +03:00
|
|
|
type Logger interface {
|
2020-03-07 13:31:25 +03:00
|
|
|
WithFields(fields logrus.Fields) *logrus.Entry
|
2021-01-10 15:21:29 +03:00
|
|
|
Debug(args ...interface{})
|
|
|
|
Debugf(format string, args ...interface{})
|
|
|
|
Warn(args ...interface{})
|
|
|
|
Warnf(format string, args ...interface{})
|
|
|
|
Info(args ...interface{})
|
|
|
|
Infof(format string, args ...interface{})
|
|
|
|
Fatal(args ...interface{})
|
|
|
|
Fatalf(format string, args ...interface{})
|
|
|
|
Trace(args ...interface{})
|
|
|
|
Tracef(format string, args ...interface{})
|
|
|
|
Error(args ...interface{})
|
|
|
|
Errorf(format string, args ...interface{})
|
2021-01-12 09:30:54 +03:00
|
|
|
Log(level logrus.Level, args ...interface{})
|
|
|
|
Logf(level logrus.Level, format string, args ...interface{})
|
2020-03-07 13:31:25 +03:00
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
type logrusLogger struct {
|
2021-01-10 15:21:29 +03:00
|
|
|
Logger Logger
|
2020-03-07 13:31:25 +03:00
|
|
|
opts Options
|
2020-02-09 19:45:23 +03:00
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
func (l *logrusLogger) Init(opts ...logger.Option) error {
|
2020-02-09 19:45:23 +03:00
|
|
|
for _, o := range opts {
|
2020-02-21 12:31:16 +03:00
|
|
|
o(&l.opts.Options)
|
2020-02-09 19:45:23 +03:00
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
if formatter, ok := l.opts.Context.Value(formatterKey{}).(logrus.Formatter); ok {
|
|
|
|
l.opts.Formatter = formatter
|
|
|
|
}
|
|
|
|
if hs, ok := l.opts.Context.Value(hooksKey{}).(logrus.LevelHooks); ok {
|
|
|
|
l.opts.Hooks = hs
|
|
|
|
}
|
|
|
|
if caller, ok := l.opts.Context.Value(reportCallerKey{}).(bool); ok && caller {
|
|
|
|
l.opts.ReportCaller = caller
|
|
|
|
}
|
|
|
|
if exitFunction, ok := l.opts.Context.Value(exitKey{}).(func(int)); ok {
|
|
|
|
l.opts.ExitFunc = exitFunction
|
2020-02-09 19:45:23 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 15:21:29 +03:00
|
|
|
switch ll := l.opts.Context.Value(loggerKey{}).(type) {
|
2020-03-07 13:31:25 +03:00
|
|
|
case *logrus.Logger:
|
|
|
|
// overwrite default options
|
|
|
|
l.opts.Level = logrusToLoggerLevel(ll.GetLevel())
|
|
|
|
l.opts.Out = ll.Out
|
|
|
|
l.opts.Formatter = ll.Formatter
|
|
|
|
l.opts.Hooks = ll.Hooks
|
|
|
|
l.opts.ReportCaller = ll.ReportCaller
|
|
|
|
l.opts.ExitFunc = ll.ExitFunc
|
|
|
|
l.Logger = ll
|
|
|
|
case *logrus.Entry:
|
|
|
|
// overwrite default options
|
|
|
|
el := ll.Logger
|
|
|
|
l.opts.Level = logrusToLoggerLevel(el.GetLevel())
|
|
|
|
l.opts.Out = el.Out
|
|
|
|
l.opts.Formatter = el.Formatter
|
|
|
|
l.opts.Hooks = el.Hooks
|
|
|
|
l.opts.ReportCaller = el.ReportCaller
|
|
|
|
l.opts.ExitFunc = el.ExitFunc
|
|
|
|
l.Logger = ll
|
|
|
|
case nil:
|
|
|
|
log := logrus.New() // defaults
|
|
|
|
log.SetLevel(loggerToLogrusLevel(l.opts.Level))
|
|
|
|
log.SetOutput(l.opts.Out)
|
|
|
|
log.SetFormatter(l.opts.Formatter)
|
|
|
|
log.ReplaceHooks(l.opts.Hooks)
|
|
|
|
log.SetReportCaller(l.opts.ReportCaller)
|
|
|
|
log.ExitFunc = l.opts.ExitFunc
|
|
|
|
l.Logger = log
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid logrus type: %T", ll)
|
2020-02-09 19:45:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-10 15:21:29 +03:00
|
|
|
func (l *logrusLogger) V(level logger.Level) bool {
|
|
|
|
switch ll := l.Logger.(type) {
|
|
|
|
case *logrus.Logger:
|
|
|
|
return ll.IsLevelEnabled(loggerToLogrusLevel(level))
|
|
|
|
case *logrus.Entry:
|
|
|
|
return ll.Logger.IsLevelEnabled(loggerToLogrusLevel(level))
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
func (l *logrusLogger) String() string {
|
|
|
|
return "logrus"
|
|
|
|
}
|
|
|
|
|
2021-08-06 02:31:49 +03:00
|
|
|
func (l *logrusLogger) Fields(fields ...interface{}) logger.Logger {
|
|
|
|
flds := make(map[string]interface{}, len(fields)/2)
|
|
|
|
for i := 0; i < len(fields); i += 2 {
|
|
|
|
flds[fields[i].(string)] = fields[i+1]
|
|
|
|
}
|
|
|
|
return &logrusLogger{l.Logger.WithFields(flds), l.opts}
|
2020-02-09 19:45:23 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:30:54 +03:00
|
|
|
func (l *logrusLogger) Trace(ctx context.Context, args ...interface{}) {
|
2021-01-12 09:43:17 +03:00
|
|
|
l.Log(ctx, logger.TraceLevel, args...)
|
2021-01-10 15:21:29 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:30:54 +03:00
|
|
|
func (l *logrusLogger) Tracef(ctx context.Context, format string, args ...interface{}) {
|
2021-01-12 09:43:17 +03:00
|
|
|
l.Logf(ctx, logger.TraceLevel, format, args...)
|
2021-01-10 15:21:29 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:30:54 +03:00
|
|
|
func (l *logrusLogger) Warn(ctx context.Context, args ...interface{}) {
|
2021-01-12 09:43:17 +03:00
|
|
|
l.Log(ctx, logger.WarnLevel, args...)
|
2021-01-10 15:21:29 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:30:54 +03:00
|
|
|
func (l *logrusLogger) Warnf(ctx context.Context, format string, args ...interface{}) {
|
2021-01-12 09:43:17 +03:00
|
|
|
l.Logf(ctx, logger.WarnLevel, format, args...)
|
2021-01-10 15:21:29 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:30:54 +03:00
|
|
|
func (l *logrusLogger) Info(ctx context.Context, args ...interface{}) {
|
2021-01-12 09:43:17 +03:00
|
|
|
l.Log(ctx, logger.InfoLevel, args...)
|
2021-01-10 15:21:29 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:30:54 +03:00
|
|
|
func (l *logrusLogger) Infof(ctx context.Context, format string, args ...interface{}) {
|
2021-01-12 09:43:17 +03:00
|
|
|
l.Logf(ctx, logger.InfoLevel, format, args...)
|
2021-01-10 15:21:29 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:30:54 +03:00
|
|
|
func (l *logrusLogger) Error(ctx context.Context, args ...interface{}) {
|
2021-01-12 09:43:17 +03:00
|
|
|
l.Log(ctx, logger.ErrorLevel, args...)
|
2021-01-10 15:21:29 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:30:54 +03:00
|
|
|
func (l *logrusLogger) Errorf(ctx context.Context, format string, args ...interface{}) {
|
2021-01-12 09:43:17 +03:00
|
|
|
l.Logf(ctx, logger.ErrorLevel, format, args...)
|
2021-01-10 15:21:29 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:30:54 +03:00
|
|
|
func (l *logrusLogger) Fatal(ctx context.Context, args ...interface{}) {
|
2021-01-12 09:43:17 +03:00
|
|
|
l.Log(ctx, logger.FatalLevel, args...)
|
2021-01-10 15:21:29 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:43:17 +03:00
|
|
|
func (l *logrusLogger) Fatalf(ctx context.Context, format string, args ...interface{}) {
|
|
|
|
l.Logf(ctx, logger.FatalLevel, format, args...)
|
2021-01-12 09:30:54 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:43:17 +03:00
|
|
|
func (l *logrusLogger) Debug(ctx context.Context, args ...interface{}) {
|
|
|
|
l.Log(ctx, logger.DebugLevel, args...)
|
2021-01-12 09:30:54 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:43:17 +03:00
|
|
|
func (l *logrusLogger) Debugf(ctx context.Context, format string, args ...interface{}) {
|
|
|
|
l.Logf(ctx, logger.DebugLevel, format, args...)
|
2021-01-10 15:21:29 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:43:17 +03:00
|
|
|
func (l *logrusLogger) Log(ctx context.Context, level logger.Level, args ...interface{}) {
|
|
|
|
if !l.V(level) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Logger.Log(loggerToLogrusLevel(level), args...)
|
2020-02-09 19:45:23 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:43:17 +03:00
|
|
|
func (l *logrusLogger) Logf(ctx context.Context, level logger.Level, format string, args ...interface{}) {
|
|
|
|
if !l.V(level) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Logger.Logf(loggerToLogrusLevel(level), format, args...)
|
2020-02-09 19:45:23 +03:00
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
func (l *logrusLogger) Options() logger.Options {
|
|
|
|
// FIXME: How to return full opts?
|
|
|
|
return l.opts.Options
|
2020-02-09 19:45:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// New builds a new logger based on options
|
2020-02-21 12:31:16 +03:00
|
|
|
func NewLogger(opts ...logger.Option) logger.Logger {
|
|
|
|
options := Options{
|
2021-01-19 23:24:13 +03:00
|
|
|
Options: logger.NewOptions(opts...),
|
2020-02-21 12:31:16 +03:00
|
|
|
Formatter: new(logrus.TextFormatter),
|
|
|
|
Hooks: make(logrus.LevelHooks),
|
|
|
|
ReportCaller: false,
|
|
|
|
ExitFunc: os.Exit,
|
|
|
|
}
|
|
|
|
l := &logrusLogger{opts: options}
|
2020-02-09 19:45:23 +03:00
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
func loggerToLogrusLevel(level logger.Level) logrus.Level {
|
2020-02-09 19:45:23 +03:00
|
|
|
switch level {
|
2020-02-21 12:31:16 +03:00
|
|
|
case logger.TraceLevel:
|
2020-02-09 19:45:23 +03:00
|
|
|
return logrus.TraceLevel
|
2020-02-21 12:31:16 +03:00
|
|
|
case logger.DebugLevel:
|
2020-02-09 19:45:23 +03:00
|
|
|
return logrus.DebugLevel
|
2020-02-21 12:31:16 +03:00
|
|
|
case logger.InfoLevel:
|
2020-02-09 19:45:23 +03:00
|
|
|
return logrus.InfoLevel
|
2020-02-21 12:31:16 +03:00
|
|
|
case logger.WarnLevel:
|
2020-02-09 19:45:23 +03:00
|
|
|
return logrus.WarnLevel
|
2020-02-21 12:31:16 +03:00
|
|
|
case logger.ErrorLevel:
|
2020-02-09 19:45:23 +03:00
|
|
|
return logrus.ErrorLevel
|
2020-02-21 12:31:16 +03:00
|
|
|
case logger.FatalLevel:
|
2020-02-09 19:45:23 +03:00
|
|
|
return logrus.FatalLevel
|
|
|
|
default:
|
|
|
|
return logrus.InfoLevel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 12:31:16 +03:00
|
|
|
func logrusToLoggerLevel(level logrus.Level) logger.Level {
|
2020-02-09 19:45:23 +03:00
|
|
|
switch level {
|
|
|
|
case logrus.TraceLevel:
|
2020-02-21 12:31:16 +03:00
|
|
|
return logger.TraceLevel
|
2020-02-09 19:45:23 +03:00
|
|
|
case logrus.DebugLevel:
|
2020-02-21 12:31:16 +03:00
|
|
|
return logger.DebugLevel
|
2020-02-09 19:45:23 +03:00
|
|
|
case logrus.InfoLevel:
|
2020-02-21 12:31:16 +03:00
|
|
|
return logger.InfoLevel
|
2020-02-09 19:45:23 +03:00
|
|
|
case logrus.WarnLevel:
|
2020-02-21 12:31:16 +03:00
|
|
|
return logger.WarnLevel
|
2020-02-09 19:45:23 +03:00
|
|
|
case logrus.ErrorLevel:
|
2020-02-21 12:31:16 +03:00
|
|
|
return logger.ErrorLevel
|
2020-02-09 19:45:23 +03:00
|
|
|
case logrus.FatalLevel:
|
2020-02-21 12:31:16 +03:00
|
|
|
return logger.FatalLevel
|
2020-02-09 19:45:23 +03:00
|
|
|
default:
|
2020-02-21 12:31:16 +03:00
|
|
|
return logger.InfoLevel
|
2020-02-09 19:45:23 +03:00
|
|
|
}
|
2020-02-15 09:55:25 +03:00
|
|
|
}
|