Skip to content

Commit

Permalink
fix: fields from the logger were not added to messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
perbu committed Sep 20, 2022
1 parent 6da0f45 commit 8611f0f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
2 changes: 2 additions & 0 deletions fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
}
Expand Down
15 changes: 13 additions & 2 deletions fields_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package chainsaw

import (
"errors"
is2 "github.com/matryer/is"
"strings"
"testing"
)

Expand Down Expand Up @@ -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"))

}
1 change: 1 addition & 0 deletions gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion log.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 8611f0f

Please sign in to comment.