Skip to content

Commit

Permalink
fix an issue for the field size limiting (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
isacikgoz committed Aug 11, 2023
1 parent a5b7201 commit 918322d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
// LimitByteSlice discards the bytes from a slice that exceeds the limit
func LimitByteSlice(b []byte, limit int) []byte {
if limit > 0 && limit < len(b) {
lb := make([]byte, 0, limit+3)
lb := make([]byte, limit, limit+3)
copy(lb, b[:limit])
return append(lb, []byte("...")...)
}
Expand Down
40 changes: 40 additions & 0 deletions logr_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package logr_test

import (
"bufio"
"bytes"
"context"
"encoding/json"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -157,3 +159,41 @@ func TestRemoveTarget(t *testing.T) {
t.Error(err)
}
}

func TestLimitLogFields(t *testing.T) {
formatter := &formatters.JSON{DisableTimestamp: true}
filter := &logr.StdFilter{Lvl: logr.Info, Stacktrace: logr.Error}
lgr, _ := logr.New(logr.MaxFieldLen(8))

buf := &bytes.Buffer{}
target := test.NewSlowTarget(buf, 2)

err := lgr.AddTarget(target, "t", filter, formatter, 3000)
require.NoError(t, err)

cfg := test.DoSomeLoggingCfg{
Lgr: lgr,
Goroutines: 1,
Loops: 1,
Lvl: logr.Info,
}
test.DoSomeLogging(cfg)
err = lgr.Flush()
require.NoError(t, err)

scanner := bufio.NewScanner(buf)
entry := struct {
Message string `json:"msg"`
}{}

var numLines int
for scanner.Scan() {
err = json.Unmarshal(scanner.Bytes(), &entry)
require.NoError(t, err)

require.Equal(t, "This is ...", entry.Message)
numLines++
}

require.Equal(t, 1, numLines)
}

0 comments on commit 918322d

Please sign in to comment.