Created
March 31, 2021 10:02
-
-
Save mingalevme/81c9df5bb08ac2bab2cde32e3d7e80cf to your computer and use it in GitHub Desktop.
Go (golang) logger interface (logrus)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package log | |
import ( | |
"context" | |
"github.com/sirupsen/logrus" | |
"time" | |
) | |
type Fields logrus.Fields | |
type Logger interface { | |
WithField(key string, value interface{}) Entry | |
WithFields(fields Fields) Entry | |
WithError(err error) Entry | |
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{}) | |
// IsDebugEnabled() bool | |
// IsInfoEnabled() bool | |
// IsWarnEnabled() bool | |
// IsErrorEnabled() bool | |
// IsFatalEnabled() bool | |
// IsPanicEnabled() bool | |
} | |
type Entry interface { | |
Logger | |
WithContext(ctx context.Context) Entry | |
WithTime(t time.Time) Entry | |
String() (string, error) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package log | |
import ( | |
"context" | |
"github.com/sirupsen/logrus" | |
"io/ioutil" | |
"time" | |
) | |
func NewLogrusLogger(logrus logrus.FieldLogger) Logger { | |
return &logrusLogger{ | |
FieldLogger: logrus, | |
} | |
} | |
type logrusLogger struct { | |
logrus.FieldLogger | |
} | |
func (s * logrusLogger) WithField(key string, value interface{}) Entry { | |
return &entry{ | |
s.FieldLogger.WithField(key, value), | |
} | |
} | |
func (s * logrusLogger) WithFields(fields Fields) Entry { | |
return &entry{ | |
s.FieldLogger.WithFields(logrus.Fields(fields)), | |
} | |
} | |
func (s * logrusLogger) WithError(err error) Entry { | |
return &entry{ | |
s.FieldLogger.WithError(err), | |
} | |
} | |
// --------------------------------------------------------------------------------------------------------------------- | |
type entry struct { | |
*logrus.Entry | |
} | |
func (s *entry) WithField(key string, value interface{}) Entry { | |
return &entry{ | |
s.Entry.WithField(key, value), | |
} | |
} | |
func (s *entry) WithFields(fields Fields) Entry { | |
return &entry{ | |
s.Entry.WithFields(logrus.Fields(fields)), | |
} | |
} | |
func (s *entry) WithError(err error) Entry { | |
return &entry{ | |
s.Entry.WithError(err), | |
} | |
} | |
func (s *entry) WithContext(ctx context.Context) Entry { | |
return &entry{ | |
s.Entry.WithContext(ctx), | |
} | |
} | |
func (s *entry) WithTime(t time.Time) Entry { | |
return &entry{ | |
s.Entry.WithTime(t), | |
} | |
} | |
// --------------------------------------------------------------------------------------------------------------------- | |
func NewNullLogger() Logger { | |
logger := logrus.New() | |
logger.Out = ioutil.Discard | |
return NewLogrusLogger(logger) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment