2020-11-27 13:00:45 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
2021-10-13 15:05:59 +00:00
|
|
|
"fmt"
|
2021-11-18 19:21:30 +00:00
|
|
|
"io"
|
2021-10-13 15:05:59 +00:00
|
|
|
|
2022-03-24 12:48:24 +00:00
|
|
|
"github.com/bombsimon/logrusr/v2"
|
|
|
|
"github.com/go-logr/logr"
|
2020-11-27 13:00:45 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-11-18 19:21:30 +00:00
|
|
|
standardLogger Logger = &logger{fl: logrus.StandardLogger()}
|
2020-11-27 13:00:45 +00:00
|
|
|
)
|
|
|
|
|
2021-11-18 19:21:30 +00:00
|
|
|
func StandardLogger() Logger {
|
|
|
|
return standardLogger
|
|
|
|
}
|
|
|
|
|
2020-11-27 13:00:45 +00:00
|
|
|
func New() Logger {
|
2021-11-18 19:21:30 +00:00
|
|
|
return &logger{fl: logrus.New()}
|
2020-11-27 13:00:45 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 10:39:52 +00:00
|
|
|
func FromLogrus(fl logrus.FieldLogger) Logger {
|
|
|
|
return &logger{fl: fl}
|
|
|
|
}
|
|
|
|
|
2020-11-27 13:00:45 +00:00
|
|
|
type Logger interface {
|
|
|
|
WithField(key string, value interface{}) Logger
|
2021-10-13 15:05:59 +00:00
|
|
|
WithFields(kv ...interface{}) Logger
|
2020-11-27 13:00:45 +00:00
|
|
|
WithError(err error) Logger
|
|
|
|
|
2022-03-24 12:48:24 +00:00
|
|
|
SetLevel(level logrus.Level) Logger
|
2021-11-18 19:21:30 +00:00
|
|
|
WriterLevel(level logrus.Level) *io.PipeWriter
|
|
|
|
|
2022-07-01 20:09:20 +00:00
|
|
|
SetOutput(w io.Writer) Logger
|
|
|
|
|
2020-11-27 13:00:45 +00:00
|
|
|
Debugf(format string, args ...interface{})
|
|
|
|
Infof(format string, args ...interface{})
|
|
|
|
Printf(format string, args ...interface{})
|
|
|
|
Warnf(format string, args ...interface{})
|
|
|
|
Warningf(format string, args ...interface{})
|
|
|
|
Errorf(format string, args ...interface{})
|
|
|
|
Fatalf(format string, args ...interface{})
|
|
|
|
Panicf(format string, args ...interface{})
|
|
|
|
|
|
|
|
Debug(args ...interface{})
|
|
|
|
Info(args ...interface{})
|
|
|
|
Print(args ...interface{})
|
|
|
|
Warn(args ...interface{})
|
|
|
|
Warning(args ...interface{})
|
|
|
|
Error(args ...interface{})
|
|
|
|
Fatal(args ...interface{})
|
|
|
|
Panic(args ...interface{})
|
|
|
|
|
|
|
|
Debugln(args ...interface{})
|
|
|
|
Infoln(args ...interface{})
|
|
|
|
Println(args ...interface{})
|
|
|
|
Warnln(args ...interface{})
|
|
|
|
Warningln(args ...interface{})
|
|
|
|
Errorln(args ...interface{})
|
|
|
|
Fatalln(args ...interface{})
|
|
|
|
Panicln(args ...interface{})
|
2022-03-24 12:48:24 +00:00
|
|
|
|
|
|
|
Logr() logr.Logger
|
2022-06-02 21:35:38 +00:00
|
|
|
FieldLogger() logrus.FieldLogger
|
2020-11-27 13:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type logger struct {
|
2021-11-18 19:21:30 +00:00
|
|
|
fl logrus.FieldLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Debugf(format string, args ...interface{}) {
|
|
|
|
l.fl.Debugf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Infof(format string, args ...interface{}) {
|
|
|
|
l.fl.Infof(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Printf(format string, args ...interface{}) {
|
|
|
|
l.fl.Printf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Warnf(format string, args ...interface{}) {
|
|
|
|
l.fl.Warnf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Warningf(format string, args ...interface{}) {
|
|
|
|
l.fl.Warningf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Errorf(format string, args ...interface{}) {
|
|
|
|
l.fl.Errorf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Fatalf(format string, args ...interface{}) {
|
|
|
|
l.fl.Fatalf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Panicf(format string, args ...interface{}) {
|
|
|
|
l.fl.Panicf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Debug(args ...interface{}) {
|
|
|
|
l.fl.Debug(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Info(args ...interface{}) {
|
|
|
|
l.fl.Info(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Print(args ...interface{}) {
|
|
|
|
l.fl.Print(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Warn(args ...interface{}) {
|
|
|
|
l.fl.Warn(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Warning(args ...interface{}) {
|
|
|
|
l.fl.Warning(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Error(args ...interface{}) {
|
|
|
|
l.fl.Error(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Fatal(args ...interface{}) {
|
|
|
|
l.fl.Fatal(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Panic(args ...interface{}) {
|
|
|
|
l.fl.Panic(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Debugln(args ...interface{}) {
|
|
|
|
l.fl.Debugln(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Infoln(args ...interface{}) {
|
|
|
|
l.fl.Infoln(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Println(args ...interface{}) {
|
|
|
|
l.fl.Println(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Warnln(args ...interface{}) {
|
|
|
|
l.fl.Warnln(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Warningln(args ...interface{}) {
|
|
|
|
l.fl.Warningln(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Errorln(args ...interface{}) {
|
|
|
|
l.fl.Errorln(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Fatalln(args ...interface{}) {
|
|
|
|
l.fl.Fatalln(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Panicln(args ...interface{}) {
|
|
|
|
l.fl.Panicln(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) WriterLevel(level logrus.Level) *io.PipeWriter {
|
|
|
|
switch t := l.fl.(type) {
|
|
|
|
case *logrus.Logger:
|
|
|
|
return t.WriterLevel(level)
|
|
|
|
case *logrus.Entry:
|
|
|
|
return t.WriterLevel(level)
|
|
|
|
}
|
2022-03-24 12:48:24 +00:00
|
|
|
panic(fmt.Sprintf("unexpected logger type %T", l.fl))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) SetLevel(level logrus.Level) Logger {
|
|
|
|
switch t := l.fl.(type) {
|
|
|
|
case *logrus.Logger:
|
|
|
|
t.SetLevel(level)
|
|
|
|
return l
|
|
|
|
case *logrus.Entry:
|
|
|
|
t.Logger.SetLevel(level)
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("unexpected logger type %T", l.fl))
|
2020-11-27 13:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) WithField(key string, value interface{}) Logger {
|
2021-11-18 19:21:30 +00:00
|
|
|
return &logger{fl: l.fl.WithField(key, value)}
|
2020-11-27 13:00:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 15:05:59 +00:00
|
|
|
func (l *logger) WithFields(kv ...interface{}) Logger {
|
2021-11-18 19:21:30 +00:00
|
|
|
log := &logger{fl: l.fl}
|
2021-09-17 23:39:15 +00:00
|
|
|
for i := 0; i < len(kv); i += 2 {
|
2021-11-18 19:21:30 +00:00
|
|
|
log = &logger{fl: log.fl.WithField(fmt.Sprintf("%v", kv[i]), kv[i+1])}
|
2020-11-27 13:00:45 +00:00
|
|
|
}
|
2021-11-18 19:21:30 +00:00
|
|
|
return log
|
2020-11-27 13:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) WithError(err error) Logger {
|
2021-11-18 19:21:30 +00:00
|
|
|
return &logger{fl: l.fl.WithError(err)}
|
2020-11-27 13:00:45 +00:00
|
|
|
}
|
2022-03-24 12:48:24 +00:00
|
|
|
|
|
|
|
func (l *logger) Logr() logr.Logger {
|
|
|
|
return logrusr.New(l.fl)
|
|
|
|
}
|
2022-06-02 21:35:38 +00:00
|
|
|
|
|
|
|
func (l *logger) FieldLogger() logrus.FieldLogger {
|
|
|
|
return l.fl
|
|
|
|
}
|
2022-07-01 20:09:20 +00:00
|
|
|
|
|
|
|
func (l *logger) SetOutput(w io.Writer) Logger {
|
|
|
|
switch t := l.fl.(type) {
|
|
|
|
case *logrus.Logger:
|
|
|
|
t.SetOutput(w)
|
|
|
|
return l
|
|
|
|
case *logrus.Entry:
|
|
|
|
t.Logger.SetOutput(w)
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("unexpected logger type %T", l.fl))
|
|
|
|
}
|