-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removes hard dependency on logrus for logging
Provides backwards compatibility with existing logrus via SetLogger() method Introduces a Logger interface that others can implement for other structured loggers such as zerolog. Add SetLoggerFromLogger method that allows caller to pass in an implementation of the new Logger interface. Bumps the golang.org/x/sys dependency since tests fail to run on go 1.18 with the old version. adding a test case for LogrusLogger adding benchmark, add WithFields method because it's a lot faster apparently
- Loading branch information
1 parent
87609ae
commit 8c6aae6
Showing
5 changed files
with
191 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package groupcache | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// Logger is a minimal interface that will allow us to use structured loggers, | ||
// including (but not limited to) logrus. | ||
type Logger interface { | ||
// Error logging level | ||
Error() Logger | ||
|
||
// Warn logging level | ||
Warn() Logger | ||
|
||
// Info logging level | ||
Info() Logger | ||
|
||
// Debug logging level | ||
Debug() Logger | ||
|
||
// ErrorField is a field with an error value | ||
ErrorField(label string, err error) Logger | ||
|
||
// StringField is a field with a string value | ||
StringField(label string, val string) Logger | ||
|
||
// WithFields is willy-nilly key value pairs. | ||
WithFields(fields map[string]interface{}) Logger | ||
|
||
// Printf is called last to emit the log at the given | ||
// level. | ||
Printf(format string, args ...interface{}) | ||
} | ||
|
||
// LogrusLogger is an implementation of Logger that wraps logrus... who knew? | ||
type LogrusLogger struct { | ||
Entry *logrus.Entry | ||
level logrus.Level | ||
} | ||
|
||
func (l LogrusLogger) Info() Logger { | ||
return LogrusLogger{ | ||
Entry: l.Entry, | ||
level: logrus.InfoLevel, | ||
} | ||
} | ||
|
||
func (l LogrusLogger) Debug() Logger { | ||
return LogrusLogger{ | ||
Entry: l.Entry, | ||
level: logrus.DebugLevel, | ||
} | ||
} | ||
|
||
func (l LogrusLogger) Warn() Logger { | ||
return LogrusLogger{ | ||
Entry: l.Entry, | ||
level: logrus.WarnLevel, | ||
} | ||
} | ||
|
||
func (l LogrusLogger) Error() Logger { | ||
return LogrusLogger{ | ||
Entry: l.Entry, | ||
level: logrus.ErrorLevel, | ||
} | ||
} | ||
|
||
func (l LogrusLogger) WithFields(fields map[string]interface{}) Logger { | ||
return LogrusLogger{ | ||
Entry: l.Entry.WithFields(fields), | ||
level: l.level, | ||
} | ||
} | ||
|
||
// ErrorField - create a field for an error | ||
func (l LogrusLogger) ErrorField(label string, err error) Logger { | ||
return LogrusLogger{ | ||
Entry: l.Entry.WithField(label, err), | ||
level: l.level, | ||
} | ||
|
||
} | ||
|
||
// StringField - create a field for a string. | ||
func (l LogrusLogger) StringField(label string, val string) Logger { | ||
return LogrusLogger{ | ||
Entry: l.Entry.WithField(label, val), | ||
level: l.level, | ||
} | ||
} | ||
|
||
func (l LogrusLogger) Printf(format string, args ...interface{}) { | ||
l.Entry.Logf(l.level, format, args...) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package groupcache | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"github.com/sirupsen/logrus" | ||
"testing" | ||
) | ||
|
||
// This tests the compatibility of the LogrusLogger with the previous behavior. | ||
func TestLogrusLogger(t *testing.T) { | ||
var buf bytes.Buffer | ||
l := logrus.New() | ||
l.SetFormatter(&logrus.TextFormatter{ | ||
DisableTimestamp: true, | ||
}) | ||
l.Out = &buf | ||
e := logrus.NewEntry(l) | ||
e = e.WithField("ContextKey", "ContextVal") | ||
SetLogger(e) | ||
logger.Error(). | ||
WithFields(map[string]interface{}{ | ||
"err": errors.New("test error"), | ||
"key": "keyValue", | ||
"category": "groupcache", | ||
}).Printf("error retrieving key from peer %s", "http://127.0.0.1:8080") | ||
|
||
interfaceOut := buf.String() | ||
buf.Reset() | ||
e.WithFields(logrus.Fields{ | ||
"err": errors.New("test error"), | ||
"key": "keyValue", | ||
"category": "groupcache", | ||
}).Errorf("error retrieving key from peer %s", "http://127.0.0.1:8080") | ||
logrusOut := buf.String() | ||
if interfaceOut != logrusOut { | ||
t.Errorf("output is not the same.\ngot:\n%s\nwant:\n%s", interfaceOut, logrusOut) | ||
} | ||
} | ||
|
||
func BenchmarkLogrusLogger(b *testing.B) { | ||
var buf bytes.Buffer | ||
l := logrus.New() | ||
l.SetFormatter(&logrus.TextFormatter{ | ||
DisableTimestamp: true, | ||
}) | ||
l.Out = &buf | ||
e := logrus.NewEntry(l) | ||
SetLogger(e) | ||
for i := 0; i < b.N; i++ { | ||
logger.Error(). | ||
WithFields(map[string]interface{}{ | ||
"err": errors.New("test error"), | ||
"key": "keyValue", | ||
"category": "groupcache", | ||
}).Printf("error retrieving key from peer %s", "http://127.0.0.1:8080") | ||
buf.Reset() | ||
} | ||
} | ||
|
||
func BenchmarkLogrus(b *testing.B) { | ||
var buf bytes.Buffer | ||
l := logrus.New() | ||
l.SetFormatter(&logrus.TextFormatter{ | ||
DisableTimestamp: true, | ||
}) | ||
l.Out = &buf | ||
e := logrus.NewEntry(l) | ||
for i := 0; i < b.N; i++ { | ||
e.WithFields(logrus.Fields{ | ||
"err": errors.New("test error"), | ||
"key": "keyValue", | ||
"category": "groupcache", | ||
}).Errorf("error retrieving key from peer %s", "http://127.0.0.1:8080") | ||
buf.Reset() | ||
} | ||
} |