Skip to content

Instantly share code, notes, and snippets.

@mingalevme
Created March 31, 2021 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mingalevme/81c9df5bb08ac2bab2cde32e3d7e80cf to your computer and use it in GitHub Desktop.
Save mingalevme/81c9df5bb08ac2bab2cde32e3d7e80cf to your computer and use it in GitHub Desktop.
Go (golang) logger interface (logrus)
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)
}
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