Skip to content

Commit

Permalink
global logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Arun Gopalpuri committed Dec 8, 2020
1 parent 1910d85 commit 2b30b94
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 17 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ func main() {
logrusLog.SetOutput(os.Stdout)
logrusLog.SetLevel(logrus.DebugLevel)
log, _ := logger.NewLogrusLogger(logrusLog)
log.WithFields(logger.Fields{
logger.ReplaceGlobals(log)
//anywhere in your code you can now use logger.L() as its globally set
logger.L().WithFields(logger.Fields{
"foo": "bar",
}).Info("direct")
}
Expand All @@ -61,9 +63,10 @@ func main() {
zapcore.DebugLevel)
zapLogger := zap.New(core)
log, _ := logger.NewZapLogger(zapLogger)
log.WithFields(logger.Fields{
logger.ReplaceGlobals(log)
//anywhere in your code you can now use logger.L() as its globally set
logger.L().WithFields(logger.Fields{
"foo": "bar",
}).Info("direct")
}

```
29 changes: 29 additions & 0 deletions pkg/logger/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package logger

import (
"sync"
)

var (
_globalMU sync.RWMutex
_globalL Logger
)

// ReplaceGlobals replaces the global Logger and returns a
// function to restore the original values. It's safe for concurrent use.
func ReplaceGlobals(logger Logger) func() {
_globalMU.Lock()
prev := _globalL
_globalL = logger
_globalMU.Unlock()
return func() { ReplaceGlobals(prev) }
}

// L returns the global Logger, which can be reconfigured with ReplaceGlobals.
// It's safe for concurrent use.
func L() Logger {
_globalMU.RLock()
l := _globalL
_globalMU.RUnlock()
return l
}
2 changes: 2 additions & 0 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package logger
//Fields Type to pass when we want to call WithFields for structured logging
type Fields map[string]interface{}

var logger Logger

//Logger is our contract for the logger
type Logger interface {
// Debug uses fmt.Sprint to construct and log a message.
Expand Down
21 changes: 14 additions & 7 deletions pkg/logger/logrus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func TestLogrusInfoLogger(t *testing.T) {
logrus.SetOutput(&buffer)
logrus.SetLevel(log.DebugLevel)
absLogger, _ := NewLogrusLogger(logrus)
absLogger.WithFields(Fields{
ReplaceGlobals(absLogger)
L().WithFields(Fields{
"foo": "bar",
}).Info("direct")

Expand All @@ -36,7 +37,8 @@ func TestLogrusInfofLogger(t *testing.T) {
logrus.SetOutput(&buffer)
logrus.SetLevel(log.DebugLevel)
absLogger, _ := NewLogrusLogger(logrus)
absLogger.WithFields(Fields{
ReplaceGlobals(absLogger)
L().WithFields(Fields{
"ping": "pong",
}).Infof("received %s balls", "ping pong")

Expand All @@ -55,7 +57,8 @@ func TestLogrusWarnLogger(t *testing.T) {
logrus.SetOutput(&buffer)
logrus.SetLevel(log.DebugLevel)
absLogger, _ := NewLogrusLogger(logrus)
absLogger.WithFields(Fields{
ReplaceGlobals(absLogger)
L().WithFields(Fields{
"foo": "bar",
"log": "logrus",
}).Warn("direct")
Expand All @@ -76,7 +79,8 @@ func TestLogrusWarnfLogger(t *testing.T) {
logrus.SetOutput(&buffer)
logrus.SetLevel(log.DebugLevel)
absLogger, _ := NewLogrusLogger(logrus)
absLogger.WithFields(Fields{
ReplaceGlobals(absLogger)
L().WithFields(Fields{
"ping": "pong",
"log": "logrus",
}).Warnf("received %s balls", "table tennis")
Expand All @@ -97,6 +101,7 @@ func TestLogrusPanicLogger(t *testing.T) {
logrus.SetOutput(&buffer)
logrus.SetLevel(log.ErrorLevel)
absLogger, _ := NewLogrusLogger(logrus)
ReplaceGlobals(absLogger)
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
Expand All @@ -108,7 +113,7 @@ func TestLogrusPanicLogger(t *testing.T) {
assert.Equal(t, "dataDB", fields["db"])
assert.Equal(t, "logrus", fields["log"])
}()
absLogger.WithFields(Fields{
L().WithFields(Fields{
"db": "dataDB",
"log": "logrus",
}).Panic("db not found")
Expand All @@ -122,7 +127,8 @@ func TestLogursErrorLogger(t *testing.T) {
logrus.SetOutput(&buffer)
logrus.SetLevel(log.DebugLevel)
absLogger, _ := NewLogrusLogger(logrus)
absLogger.WithFields(Fields{
ReplaceGlobals(absLogger)
L().WithFields(Fields{
"acctNumber": 7899,
"log": "logrus",
}).Errorf("Error creating account %s", "testAccount")
Expand All @@ -143,7 +149,8 @@ func TestLogrusNoOutputLogger(t *testing.T) {
logrus.SetOutput(&buffer)
logrus.SetLevel(log.InfoLevel)
absLogger, _ := NewLogrusLogger(logrus)
absLogger.WithFields(Fields{
ReplaceGlobals(absLogger)
L().WithFields(Fields{
"foo": "bar",
}).Debugf("direct")

Expand Down
21 changes: 14 additions & 7 deletions pkg/logger/zap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ func TestZapInfoLogger(t *testing.T) {
)
z := zap.New(core)
absLogger, _ := NewZapLogger(z)
absLogger.WithFields(Fields{
ReplaceGlobals(absLogger)
L().WithFields(Fields{
"foo": "bar",
}).Info("direct")

Expand All @@ -45,7 +46,8 @@ func TestZapInfofLogger(t *testing.T) {
)
z := zap.New(core)
absLogger, _ := NewZapLogger(z)
absLogger.WithFields(Fields{
ReplaceGlobals(absLogger)
L().WithFields(Fields{
"ping": "pong",
}).Infof("received %s balls", "ping pong")

Expand All @@ -68,7 +70,8 @@ func TestZapWarnLogger(t *testing.T) {
)
z := zap.New(core)
absLogger, _ := NewZapLogger(z)
absLogger.WithFields(Fields{
ReplaceGlobals(absLogger)
L().WithFields(Fields{
"foo": "bar",
"log": "zap",
}).Warn("direct")
Expand All @@ -93,7 +96,8 @@ func TestZapWarnfLogger(t *testing.T) {
)
z := zap.New(core)
absLogger, _ := NewZapLogger(z)
absLogger.WithFields(Fields{
ReplaceGlobals(absLogger)
L().WithFields(Fields{
"ping": "pong",
"log": "zap",
}).Warnf("received %s balls", "table tennis")
Expand Down Expand Up @@ -129,7 +133,8 @@ func TestZapPanicLogger(t *testing.T) {
assert.Equal(t, "dataDB", fields["db"])
assert.Equal(t, "zap", fields["log"])
}()
absLogger.WithFields(Fields{
ReplaceGlobals(absLogger)
L().WithFields(Fields{
"db": "dataDB",
"log": "zap",
}).Panic("db not found")
Expand All @@ -147,7 +152,8 @@ func TestZapErrorLogger(t *testing.T) {
)
z := zap.New(core)
absLogger, _ := NewZapLogger(z)
absLogger.WithFields(Fields{
ReplaceGlobals(absLogger)
L().WithFields(Fields{
"acctNumber": 7899,
"log": "zap",
}).Errorf("Error creating account %s", "testAccount")
Expand All @@ -172,7 +178,8 @@ func TestZapNoOutputLogger(t *testing.T) {
)
z := zap.New(core)
absLogger, _ := NewZapLogger(z)
absLogger.WithFields(Fields{
ReplaceGlobals(absLogger)
L().WithFields(Fields{
"foo": "bar",
}).Debugf("direct")
assert.Equal(t, "", string(buffer.Bytes()))
Expand Down

0 comments on commit 2b30b94

Please sign in to comment.