Skip to content

Commit

Permalink
Provide default logger when given is nil
Browse files Browse the repository at this point in the history
If the given inner logger for our log implementation is nil,
provide a new logrus logger by default. This avoids a possible panic
when checking for the log level in the Logf method as well as allows us
to remove the particular logging action using Printf when wrapped logger
was nil.

Resolves: #734 (comment)
  • Loading branch information
ka3de committed Jan 30, 2023
1 parent 364f771 commit 61609ee
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func NewNullLogger() *Logger {

// New creates a new logger.
func New(logger *logrus.Logger, iterID string) *Logger {
if logger == nil {
logger = logrus.New()
}
return &Logger{
Logger: logger,
iterID: iterID,
Expand Down Expand Up @@ -87,10 +90,6 @@ func (l *Logger) Logf(level logrus.Level, category string, msg string, args ...a
if l.categoryFilter != nil && !l.categoryFilter.MatchString(category) {
return
}
if l.Logger == nil {
fmt.Printf("%s [%d]: %s - %d ms\n", category, goRoutineID(), string(msg), elapsed)
return
}
fields := logrus.Fields{
"category": category,
"elapsed": fmt.Sprintf("%d ms", elapsed),
Expand Down

0 comments on commit 61609ee

Please sign in to comment.