🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Logs don't panic when receiving a `nil` *time.Time
Deluan5abe1567774f
diff --git a/log/log.go b/log/log.goindex c1d058453..3ffbb14b9 100644--- a/log/log.go+++ b/log/log.go@@ -6,6 +6,7 @@ import ("fmt""net/http""os"+ "reflect""runtime""sort""strings"@@ -267,7 +268,12 @@ func addFields(logger *logrus.Entry, keyValuePairs []interface{}) *logrus.Entrycase time.Duration:logger = logger.WithField(name, ShortDur(v))case fmt.Stringer:- logger = logger.WithField(name, v.String())+ vOf := reflect.ValueOf(v)+ if vOf.Kind() == reflect.Pointer && vOf.IsNil() {+ logger = logger.WithField(name, "nil")+ } else {+ logger = logger.WithField(name, v.String())+ }default:logger = logger.WithField(name, v)}diff --git a/log/log_test.go b/log/log_test.goindex e54fa95fe..0462988ef 100644--- a/log/log_test.go+++ b/log/log_test.go@@ -5,6 +5,7 @@ import ("errors""net/http/httptest""testing"+ "time". "github.com/onsi/ginkgo/v2". "github.com/onsi/gomega"@@ -91,9 +92,20 @@ var _ = Describe("Logger", func() {SetLogSourceLine(true)Error("A crash happened")// NOTE: This assertion breaks if the line number above changes- Expect(hook.LastEntry().Data[" source"]).To(ContainSubstring("/log/log_test.go:92"))+ Expect(hook.LastEntry().Data[" source"]).To(ContainSubstring("/log/log_test.go:93"))Expect(hook.LastEntry().Message).To(Equal("A crash happened"))})++ It("logs fmt.Stringer as a string", func() {+ t := time.Now()+ Error("Simple Message", "key1", t)+ Expect(hook.LastEntry().Data["key1"]).To(Equal(t.String()))+ })+ It("logs nil fmt.Stringer as nil", func() {+ var t *time.Time+ Error("Simple Message", "key1", t)+ Expect(hook.LastEntry().Data["key1"]).To(Equal("nil"))+ })})Describe("Levels", func() {