diff --git a/fields.go b/fields.go index 3c5d89d..e4b5c65 100644 --- a/fields.go +++ b/fields.go @@ -86,6 +86,8 @@ func (l *CircularLogger) formatPair(key string, value interface{}) string { case time.Time: ts := value.(time.Time) valueFormatted = ts.Format(l.TimeFmt) + case error: + valueFormatted = value.(error).Error() default: valueFormatted = "[INVALID TYPE]" } diff --git a/fields_test.go b/fields_test.go index f10ca09..29198e7 100644 --- a/fields_test.go +++ b/fields_test.go @@ -1,6 +1,9 @@ package chainsaw import ( + "errors" + is2 "github.com/matryer/is" + "strings" "testing" ) @@ -43,11 +46,19 @@ func TestCircularLogger_Infow(t *testing.T) { } func TestField_usage(t *testing.T) { + is := is2.New(t) l := MakeLogger("test") defer l.Stop() l.SetFields(P{"routerid", 42}, P{"username", "perbu"}) l.AddFields(P{"country", "no"}) l.Infof("Could not open file: %s", "no such file") - l.Infow("Could not open file.", P{"err", "no such file"}) - l.Flush() + l.Infow("Could not open file.", P{"err", errors.New("no such file")}) + err := l.Flush() + is.NoErr(err) + msgs := l.GetMessages(InfoLevel) + is.Equal(len(msgs), 2) + fields0 := msgs[0].Fields + is.True(strings.Contains(fields0, "routerid=42")) + is.True(strings.Contains(fields0, "username=perbu")) + } diff --git a/gen/main.go b/gen/main.go index 2616a72..0097d96 100644 --- a/gen/main.go +++ b/gen/main.go @@ -32,6 +32,7 @@ func main() { logFunctions := make([]LogFunction, 0) for _, level := range myLevels { lvlName := strings.Title(level.String()) + lvlName = strings.TrimSuffix(lvlName, "Level") fmt.Println("Level found", lvlName) lf := LogFunction{Level: lvlName} logFunctions = append(logFunctions, lf) diff --git a/log.go b/log.go index bf03f87..c11583f 100644 --- a/log.go +++ b/log.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -// This file was generated by gen/main.go at 2021-12-19 15:34:47.919655 +0100 CET m=+0.001822834 +// This file was generated by gen/main.go at 2022-09-20 16:00:00.129406 +0200 CEST m=+0.001519376 package chainsaw import ( diff --git a/main.go b/main.go index 11e5aa7..d076131 100644 --- a/main.go +++ b/main.go @@ -8,9 +8,11 @@ import ( "time" ) +// Code generation; the order matters, make sure to run the stringers first. +// +//go:generate stringer -type=controlType +//go:generate stringer -type=LogLevel //go:generate go run gen/main.go -//go:generate stringer -type=controlType -//go:generate string -type=LogLevel const channelTimeout = time.Second var defaultLogger *CircularLogger @@ -115,14 +117,19 @@ type CircularLogger struct { func (l *CircularLogger) log(level LogLevel, message string, fields string) { t := time.Now() + msgFields := l.fields + if len(fields) > 0 { + msgFields += " " + fields + } logM := LogMessage{ Message: message, - Fields: fields, + Fields: msgFields, LogLevel: level, TimeStamp: t, } l.logCh <- logM - // trigger backtrace if level is high enough to triger it. + // trigger backtrace if level is high enough to trigger it. + // if backTraceLevel is set to TraceLevel, this is disabled. if l.backTraceLevel > TraceLevel && level >= l.backTraceLevel { err := l.Flush() if err != nil { @@ -303,6 +310,7 @@ func (l *CircularLogger) getMessageOverCh(level LogLevel) []LogMessage { buf := make([]LogMessage, 0) for i := l.current; i < l.current+l.logBufferSize; i++ { msg := l.messages[i%l.logBufferSize] + // Add the fields from the logger: if msg.LogLevel < level { continue }