From 61609ee2ccd55f921b99b83ca59228cdadd64d81 Mon Sep 17 00:00:00 2001 From: ka3de Date: Mon, 30 Jan 2023 08:53:51 +0100 Subject: [PATCH] Provide default logger when given is nil 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: https://github.com/grafana/xk6-browser/pull/734#discussion_r1089171642 --- log/logger.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/log/logger.go b/log/logger.go index 955a2abc0..a1c0219f0 100644 --- a/log/logger.go +++ b/log/logger.go @@ -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, @@ -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),