Skip to content

Commit

Permalink
fix: handle logging calls when the logger is nil.
Browse files Browse the repository at this point in the history
  • Loading branch information
perbu committed Nov 18, 2022
1 parent 358f049 commit b524fab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ type CircularLogger struct {
}

func (l *CircularLogger) log(level LogLevel, message string, fields string) {
// check if l is nil. This can happen if the user has not initialized a logger.
if l == nil {
fmt.Println("Chainsaw: This logger isn't initialized. Use default logger or initialize a new one.")
return
}
t := time.Now()
msgFields := l.fields
if len(fields) > 0 {
Expand Down
10 changes: 10 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ func TestDemo(t *testing.T) {
logger.Stop()
}

func TestNilLogger(t *testing.T) {
var logger *CircularLogger
logger.Trace("This should not panic")
logger.Debugf("This should not panic either: %d", 1)
logger.Infow("This should not panic either", P{"test", 1})
logger.Warn("This should not panic either")
logger.Errorf("This should not panic either: %d", 1)
logger.Errorw("This should not panic either", P{"test", 1})
}

func TestLoggingPerformance(t *testing.T) {
is := is2.New(t)

Expand Down

0 comments on commit b524fab

Please sign in to comment.