Logs don't panic when receiving a nil *time.Time

This commit is contained in:
Deluan
2024-02-18 13:06:01 -05:00
parent fa72aaa462
commit 5abe156777
2 changed files with 20 additions and 2 deletions
+7 -1
View File
@@ -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.Entry
case 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)
}