Skip to content

Commit

Permalink
Merge pull request #14 from kempjeng/ioutil-discard-default-output
Browse files Browse the repository at this point in the history
default output ioutil.Discard
  • Loading branch information
francoispqt authored Aug 28, 2018
2 parents ab670ab + d22a1fb commit 78cfad0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package onelog

import (
"io"
"io/ioutil"
"runtime"
"strconv"

Expand Down Expand Up @@ -45,6 +46,10 @@ type Logger struct {

// New returns a fresh onelog Logger with default values.
func New(w io.Writer, levels uint8) *Logger {
if w == nil {
w = ioutil.Discard
}

return &Logger{
levels: levels,
w: w,
Expand Down
7 changes: 7 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,10 @@ func TestOnelogContext(t *testing.T) {
assert.Equal(t, json, string(w.b), "bytes written to the writer dont equal expected result")
})
}

func TestNilOutput(t *testing.T) {
logger := New(nil, 0)
assert.NotNil(t, logger.w, "Logger output should not be nil")
logger.Error("Test output")
t.Log("Successfully write to ioutil.Discard output")
}

0 comments on commit 78cfad0

Please sign in to comment.