Skip to content

Commit

Permalink
Merge pull request #20 from BrendanGalloway/logrus-interface-base
Browse files Browse the repository at this point in the history
Use logrus Interface instead of Type
  • Loading branch information
moshloop authored Oct 15, 2020
2 parents 6f70b4e + c65acee commit afee3b1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 49 deletions.
2 changes: 1 addition & 1 deletion console/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (p *terminalProgress) Status(status string) {

func NewTextProgress(name string, localTest *TestResults) Progress {
return &textProgress{
Logger: logger.NewLogger("test", name),
Logger: logger.WithValues("test", name),
test: localTest,
}
}
Expand Down
12 changes: 3 additions & 9 deletions logger/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (
var currentLogger Logger

func init() {
currentLogger = logrusLogger{
Logger: logsrusapi.StandardLogger(),
}
currentLogger = NewLogrusLogger(logsrusapi.StandardLogger())
}

func BindFlags(flags *pflag.FlagSet) {
Expand Down Expand Up @@ -71,12 +69,8 @@ func IsDebugEnabled() bool {
return currentLogger.IsDebugEnabled()
}

func NewLogger(key string, value interface{}) Logger {
return currentLogger.NewLogger(key, value)
}

func NewLoggerWithFields(fields map[string]interface{}) Logger {
return currentLogger.NewLoggerWithFields(fields)
func WithValues(keysAndValues ...interface{}) Logger {
return currentLogger.WithValues(keysAndValues)
}

func StandardLogger() Logger {
Expand Down
3 changes: 1 addition & 2 deletions logger/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ type Logger interface {
Debugf(format string, args ...interface{})
Tracef(format string, args ...interface{})
Fatalf(format string, args ...interface{})
NewLogger(key string, value interface{}) Logger
NewLoggerWithFields(fields map[string]interface{}) Logger
WithValues(keysAndValues ...interface{}) Logger
IsTraceEnabled() bool
IsDebugEnabled() bool
SetLogLevel(level int)
Expand Down
53 changes: 30 additions & 23 deletions logger/logrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,31 @@ import (
)

type logrusLogger struct {
*logrusapi.Logger
*logrusapi.Entry
}

func NewLogrusLogger(existing logrusapi.Ext1FieldLogger) Logger {
switch existing.(type) {
case *logrusapi.Entry:
return logrusLogger{Entry: existing.(*logrusapi.Entry)}
case *logrusapi.Logger:
return logrusLogger{Entry: logrusapi.NewEntry(existing.(*logrusapi.Logger))}
default:
return logrusLogger{Entry: logrusapi.NewEntry(logrusapi.StandardLogger())}
}
}

func (logrus logrusLogger) Warnf(format string, args ...interface{}) {
logrus.Logger.Warnf(format, args...)
logrus.Entry.Warnf(format, args...)
}

func (logrus logrusLogger) Infof(format string, args ...interface{}) {
logrus.Logger.Infof(format, args...)
logrus.Entry.Infof(format, args...)
}

//Secretf is like Tracef, but attempts to strip any secrets from the text
func (logrus logrusLogger) Secretf(format string, args ...interface{}) {
logrus.Logger.Tracef(stripSecrets(fmt.Sprintf(format, args...)))
logrus.Entry.Tracef(stripSecrets(fmt.Sprintf(format, args...)))
}

//Prettyf is like Tracef, but pretty prints the entire struct
Expand All @@ -30,48 +41,44 @@ func (logrus logrusLogger) Prettyf(msg string, obj interface{}) {
}

func (logrus logrusLogger) Errorf(format string, args ...interface{}) {
logrus.Logger.Errorf(format, args...)
logrus.Entry.Errorf(format, args...)
}

func (logrus logrusLogger) Debugf(format string, args ...interface{}) {
logrus.Logger.Debugf(format, args...)
logrus.Entry.Debugf(format, args...)
}

func (logrus logrusLogger) Tracef(format string, args ...interface{}) {
logrus.Logger.Tracef(format, args...)
logrus.Entry.Tracef(format, args...)
}

func (logrus logrusLogger) Fatalf(format string, args ...interface{}) {
logrus.Logger.Fatalf(format, args...)
logrus.Entry.Fatalf(format, args...)
}

func (logrus logrusLogger) NewLogger(key string, value interface{}) Logger {
return logrusLogger{Logger: logrusapi.New().WithField(key, value).Logger}
}

func (logrus logrusLogger) NewLoggerWithFields(fields map[string]interface{}) Logger {
return logrusLogger{Logger: logrusapi.New().WithFields(logrusapi.Fields(fields)).Logger}
}

func NewLogrusLogger(existing logrusapi.Logger) Logger {
return logrusLogger{Logger: &existing}
func (logrus logrusLogger) WithValues(keysAndValues ...interface{}) Logger {
fieldMap := make(map[string]interface{})
for i := 0; i < len(keysAndValues); i += 2 {
fieldMap[fmt.Sprintf("%v", keysAndValues[i])] = keysAndValues[i+1]
}
return logrusLogger{Entry: logrus.Entry.WithFields(logrusapi.Fields(fieldMap))}
}

func (logrus logrusLogger) SetLogLevel(level int) {
switch {
case level > 1:
logrus.Logger.SetLevel(logrusapi.TraceLevel)
logrus.Entry.Logger.SetLevel(logrusapi.TraceLevel)
case level > 0:
logrus.Logger.SetLevel(logrusapi.DebugLevel)
logrus.Entry.Logger.SetLevel(logrusapi.DebugLevel)
default:
logrus.Logger.SetLevel(logrusapi.InfoLevel)
logrus.Entry.Logger.SetLevel(logrusapi.InfoLevel)
}
}

func (logrus logrusLogger) IsTraceEnabled() bool {
return logrus.Logger.IsLevelEnabled(logrusapi.TraceLevel)
return logrus.Entry.Logger.IsLevelEnabled(logrusapi.TraceLevel)
}

func (logrus logrusLogger) IsDebugEnabled() bool {
return logrus.Logger.IsLevelEnabled(logrusapi.DebugLevel)
return logrus.Entry.Logger.IsLevelEnabled(logrusapi.DebugLevel)
}
16 changes: 2 additions & 14 deletions logger/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,8 @@ func (zap ZapLogger) SetLogLevel(level int) {
zap.Level.SetLevel(atom.Level())
}

func (zap ZapLogger) NewLogger(key string, value interface{}) Logger {
logger := zap.Logger.With(key, value)
return ZapLogger{
Level: zap.Level,
Base: logger.Desugar(),
Logger: logger,
}
}

func (zap ZapLogger) NewLoggerWithFields(fields map[string]interface{}) Logger {
logger := zap.Logger
for k, v := range fields {
logger = logger.With(k, v)
}
func (zap ZapLogger) WithValues(keysAndValues ...interface{}) Logger {
logger := zap.Logger.With(keysAndValues...)
return ZapLogger{
Level: zap.Level,
Base: logger.Desugar(),
Expand Down

0 comments on commit afee3b1

Please sign in to comment.