Skip to content

Commit

Permalink
Merge pull request #15 from uber-common/ajs-nop
Browse files Browse the repository at this point in the history
Add constructor for a no-op logger
  • Loading branch information
akshayjshah authored Sep 19, 2017
2 parents dfa0983 + 5a41c97 commit 11c68f2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
12 changes: 11 additions & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package bark

import (
"io/ioutil"
"time"

"github.com/cactus/go-statsd-client/statsd"
Expand Down Expand Up @@ -84,7 +85,7 @@ type Logger interface {
Fields() Fields
}

// Logfields is an interface for dictionaries passed to Logger's WithFields logging method.
// LogFields is an interface for dictionaries passed to Logger's WithFields logging method.
// It exists to provide a layer of indirection so code already using other
// "Fields" types can be changed to use bark.Logger instances without
// any refactoring (sidestepping the fact that if we used a concrete
Expand All @@ -101,6 +102,15 @@ func (f Fields) Fields() map[string]interface{} {
return f
}

// NewNopLogger creates a no-op logger.
func NewNopLogger() Logger {
return NewLoggerFromLogrus(&logrus.Logger{
Out: ioutil.Discard,
Formatter: new(logrus.JSONFormatter),
Level: logrus.DebugLevel,
})
}

// NewLoggerFromLogrus creates a bark-compliant wrapper for a logrus-brand logger.
func NewLoggerFromLogrus(logger *logrus.Logger) Logger {
return newBarkLogrusLogger(logger)
Expand Down
13 changes: 9 additions & 4 deletions logrus_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (

// Create a logrus logger that writes its out output to a buffer for inspection
func getLogrusLogger() (*logrus.Logger, *bytes.Buffer) {
var logrusLogger *logrus.Logger = logrus.New()
logrusLogger := logrus.New()

buffer := &bytes.Buffer{}
logrusLogger.Out = buffer
Expand Down Expand Up @@ -155,10 +155,8 @@ func TestWithError(t *testing.T) {
}

func TestGetFields(t *testing.T) {
var logger bark.Logger

// Plain logger
logger = bark.NewLoggerFromLogrus(logrus.New())
logger := bark.NewLoggerFromLogrus(logrus.New())
require.Equal(t, logger.Fields(), bark.Fields(nil))

// Add nil, don't crash
Expand Down Expand Up @@ -286,3 +284,10 @@ func TestFatalf(t *testing.T) {
barkStderr := execFatalTool(t, "bark.Fatalf")
validateOutput(t, barkStderr, logrusStderr)
}

func TestNopLogger(t *testing.T) {
assert.NotPanics(t, func() {
logger := bark.NewNopLogger()
logger.Info("hello")
})
}

0 comments on commit 11c68f2

Please sign in to comment.