logger: add Logger method to access *logrus.Logger

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
Adphi 2022-10-18 18:26:39 +02:00
parent c7096975b1
commit 23f1b78389
Signed by: adphi
GPG Key ID: 46BE4062DB2397FF

View File

@ -64,6 +64,7 @@ type Logger interface {
Logr() logr.Logger Logr() logr.Logger
FieldLogger() logrus.FieldLogger FieldLogger() logrus.FieldLogger
Logger() *logrus.Logger
} }
type logger struct { type logger struct {
@ -167,25 +168,12 @@ func (l *logger) Panicln(args ...interface{}) {
} }
func (l *logger) WriterLevel(level logrus.Level) *io.PipeWriter { func (l *logger) WriterLevel(level logrus.Level) *io.PipeWriter {
switch t := l.fl.(type) { return l.Logger().WriterLevel(level)
case *logrus.Logger:
return t.WriterLevel(level)
case *logrus.Entry:
return t.WriterLevel(level)
}
panic(fmt.Sprintf("unexpected logger type %T", l.fl))
} }
func (l *logger) SetLevel(level logrus.Level) Logger { func (l *logger) SetLevel(level logrus.Level) Logger {
switch t := l.fl.(type) { l.Logger().SetLevel(level)
case *logrus.Logger:
t.SetLevel(level)
return l return l
case *logrus.Entry:
t.Logger.SetLevel(level)
return l
}
panic(fmt.Sprintf("unexpected logger type %T", l.fl))
} }
func (l *logger) WithField(key string, value interface{}) Logger { func (l *logger) WithField(key string, value interface{}) Logger {
@ -212,14 +200,17 @@ func (l *logger) FieldLogger() logrus.FieldLogger {
return l.fl return l.fl
} }
func (l *logger) SetOutput(w io.Writer) Logger { func (l *logger) Logger() *logrus.Logger {
switch t := l.fl.(type) { switch t := l.fl.(type) {
case *logrus.Logger: case *logrus.Logger:
t.SetOutput(w) return t
return l
case *logrus.Entry: case *logrus.Entry:
t.Logger.SetOutput(w) return t.Logger
return l
} }
panic(fmt.Sprintf("unexpected logger type %T", l.fl)) panic(fmt.Sprintf("unexpected logger type %T", l.fl))
} }
func (l *logger) SetOutput(w io.Writer) Logger {
l.Logger().SetOutput(w)
return l
}