Skip to content

Commit

Permalink
fix: Fix issues identified by golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
thessem committed Aug 22, 2024
1 parent ef056c2 commit 99b521a
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 70 deletions.
15 changes: 4 additions & 11 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ func (e *prettyConsoleEncoder) AppendUint64(u uint64) {
func (e *prettyConsoleEncoder) AppendDuration(duration time.Duration) {
e.addSeparator()
cur := e.buf.Len()
if durationEncoder := e.cfg.EncodeDuration; e != nil {
durationEncoder(duration, e)
}
e.cfg.EncodeDuration(duration, e)
if cur == e.buf.Len() {
// User-supplied EncodeDuration is a no-op. Fall back to nanoseconds to keep
// JSON valid.
Expand All @@ -110,9 +108,7 @@ func (e *prettyConsoleEncoder) AppendDuration(duration time.Duration) {
func (e *prettyConsoleEncoder) AppendTime(t time.Time) {
e.addSeparator()
cur := e.buf.Len()
if timeEncoder := e.cfg.EncodeTime; e != nil {
timeEncoder(t, e)
}
e.cfg.EncodeTime(t, e)
if cur == e.buf.Len() {
// User-supplied EncodeTime is a no-op. Fall back to RFC3339
e.buf.AppendTime(t, time.RFC3339)
Expand Down Expand Up @@ -189,11 +185,8 @@ func (e *prettyConsoleEncoder) AppendReflected(value interface{}) error {
indent: enc.namespaceIndent,
lineEnding: []byte(e.cfg.LineEnding),
}

if reflectedEncoder := e.cfg.NewReflectedEncoder(iw); e != nil {
if err := reflectedEncoder.Encode(value); err != nil {
return err
}
if err := e.cfg.NewReflectedEncoder(iw).Encode(value); err != nil {
return err
}
if l-enc.buf.Len() == 0 {
// User-supplied reflectedEncoder is a no-op. Fall back to dd
Expand Down
2 changes: 1 addition & 1 deletion encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (e *prettyConsoleEncoder) appendSafeByte(s []byte) {
i++
continue
}
e.buf.Write(s[i : i+size])
_, _ = e.buf.Write(s[i : i+size]) // Explicitly ignore errors
i += size
}
}
Expand Down
9 changes: 0 additions & 9 deletions encoder_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,6 @@ func newBenchmarkPrettyZapLogger(lvl zapcore.Level) *zap.Logger {
))
}

func newSampledLogger(lvl zapcore.Level) *zap.Logger {
return zap.New(zapcore.NewSamplerWithOptions(
newZapLogger(zap.DebugLevel).Core(),
100*time.Millisecond,
10, // first
10, // thereafter
))
}

func fakeFields() []zap.Field {
return []zap.Field{
zap.Int("int", _tenInts[0]),
Expand Down
81 changes: 45 additions & 36 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,39 +198,52 @@ func TestEncodeEntry(t *testing.T) {
},
{
desc: "Errors",
// 4:33PM ERR > test message named_stracktrace=<module_name>.TestEncodeEntry\n\t/<some_file>:<line_number>\ntesting.tRunner\n\t/<some_file>:<line_number>
// 4:33PM ERR > test message named_stracktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry\n\t/<some_file>:<line_number>\ntesting.tRunner\n\t/<some_file>:<line_number>
// ↳ error=something \nwent wrong
// ↳ nested.cause=error with stacktrace
// .cause.cause=error with 2 causes
// .cause.cause.0=cause 1
// .stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry
// /<some_file>:<line_number>
// testing.tRunner
// /<some_file>:<line_number>
// runtime.goexit
// /<some_file>:<line_number>
// .cause.1.cause=deeper error with two causes
// .cause.cause.0=deeper cause 1
// .cause.1=deeper cause 2
// .stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry
// /<some_file>:<line_number>
// testing.tRunner
// /<some_file>:<line_number>
// runtime.goexit
// /<some_file>:<line_number>
// .stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry
// /<some_file>:<line_number>
// testing.tRunner
// /<some_file>:<line_number>
// runtime.goexit
// /<some_file>:<line_number>
// .stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry
// /<some_file>:<line_number>
// testing.tRunner
// /<some_file>:<line_number>
// runtime.goexit
// /<some_file>:<line_number>
expected: "\x1b[90m4:33PM\x1b[0m\x1b[31m \x1b[0m\x1b[31mERR\x1b[0m\x1b[31m \x1b[0m\x1b[1m\x1b[31m>\x1b[0m\x1b[0m\x1b[31m \x1b[0mtest message\x1b[31m \x1b[0m\x1b[31mnamed_stracktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\x1b[31m\\n\x1b[0m\x1b[31m\\t\x1b[0m/<some_file>:<line_number>\x1b[31m\\n\x1b[0mtesting.tRunner\x1b[31m\\n\x1b[0m\x1b[31m\\t\x1b[0m/<some_file>:<line_number>\n\x1b[31m ↳ error\x1b[0m\x1b[31m=\x1b[0msomething \x1b[31m\\n\x1b[0mwent wrong\n\x1b[31m ↳ nested\x1b[0m\x1b[31m.cause\x1b[0m\x1b[31m=\x1b[0merror with stacktrace\n \x1b[31m.cause\x1b[0m\x1b[31m.cause\x1b[0m\x1b[31m=\x1b[0merror with 2 causes\n \x1b[31m.cause\x1b[0m\x1b[31m.cause.0\x1b[0m\x1b[31m=\x1b[0mcause 1\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/<some_file>:<line_number>\n testing.tRunner\n \t/<some_file>:<line_number>\n runtime.goexit\n \t/<some_file>:<line_number>\n \x1b[31m.cause.1\x1b[0m\x1b[31m.cause\x1b[0m\x1b[31m=\x1b[0mdeeper error with two causes\n \x1b[31m.cause\x1b[0m\x1b[31m.cause.0\x1b[0m\x1b[31m=\x1b[0mdeeper cause 1\n \x1b[31m.cause.1\x1b[0m\x1b[31m=\x1b[0mdeeper cause 2\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/<some_file>:<line_number>\n testing.tRunner\n \t/<some_file>:<line_number>\n runtime.goexit\n \t/<some_file>:<line_number>\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/<some_file>:<line_number>\n testing.tRunner\n \t/<some_file>:<line_number>\n runtime.goexit\n \t/<some_file>:<line_number>\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/<some_file>:<line_number>\n testing.tRunner\n \t/<some_file>:<line_number>\n runtime.goexit\n \t/<some_file>:<line_number>\n\x1b[31m ↳ nil_panic_PANIC_DISPLAYING_ERROR\x1b[0m\x1b[31m=\x1b[0mPANIC=runtime error: invalid memory address or nil pointer dereference\n\x1b[31m ↳ normal_panic\x1b[0m<nil>\n\x1b[31m ↳ stack\x1b[0m\x1b[31m=\x1b[0man error with a stacktrace has occurred\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/<some_file>:<line_number>\n testing.tRunner\n \t/<some_file>:<line_number>\n runtime.goexit\n \t/<some_file>:<line_number>\n\x1b[31m ↳ \x1b[0m\x1b[31mstacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/<some_file>:<line_number>\n testing.tRunner\n \t/<some_file>:<line_number>\n",
// .cause.cause=error with 2 causes
// .cause.cause.0=cause 1
// .stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry
// /<some_file>:<line_number>
// testing.tRunner
// /<some_file>:<line_number>
// runtime.goexit
// /<some_file>:<line_number>
// .cause.1.cause=deeper error with two causes
// .cause.cause.0=deeper cause 1
// .cause.1=deeper cause 2
// .stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry
// /<some_file>:<line_number>
// testing.tRunner
// /<some_file>:<line_number>
// runtime.goexit
// /<some_file>:<line_number>
// .stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry
// /<some_file>:<line_number>
// testing.tRunner
// /<some_file>:<line_number>
// runtime.goexit
// /<some_file>:<line_number>
// .stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry
// /<some_file>:<line_number>
// testing.tRunner
// /<some_file>:<line_number>
// runtime.goexit
// /<some_file>:<line_number>
// ↳ nil_panic_PANIC_DISPLAYING_ERROR=PANIC=Panic!
// ↳ normal_panic<nil>
// ↳ stack=an error with a stacktrace has occurred
// .stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry
// /<some_file>:<line_number>
// testing.tRunner
// /<some_file>:<line_number>
// runtime.goexit
// /<some_file>:<line_number>
// ↳ stacktrace=github.com/thessem/zap-prettyconsole.TestEncodeEntry
// /<some_file>:<line_number>
// testing.tRunner
// /<some_file>:<line_number>
expected: "\x1b[90m4:33PM\x1b[0m\x1b[31m \x1b[0m\x1b[31mERR\x1b[0m\x1b[31m \x1b[0m\x1b[1m\x1b[31m>\x1b[0m\x1b[0m\x1b[31m \x1b[0mtest message\x1b[31m \x1b[0m\x1b[31mnamed_stracktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\x1b[31m\\n\x1b[0m\x1b[31m\\t\x1b[0m/<some_file>:<line_number>\x1b[31m\\n\x1b[0mtesting.tRunner\x1b[31m\\n\x1b[0m\x1b[31m\\t\x1b[0m/<some_file>:<line_number>\n\x1b[31m ↳ error\x1b[0m\x1b[31m=\x1b[0msomething \x1b[31m\\n\x1b[0mwent wrong\n\x1b[31m ↳ nested\x1b[0m\x1b[31m.cause\x1b[0m\x1b[31m=\x1b[0merror with stacktrace\n \x1b[31m.cause\x1b[0m\x1b[31m.cause\x1b[0m\x1b[31m=\x1b[0merror with 2 causes\n \x1b[31m.cause\x1b[0m\x1b[31m.cause.0\x1b[0m\x1b[31m=\x1b[0mcause 1\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/<some_file>:<line_number>\n testing.tRunner\n \t/<some_file>:<line_number>\n runtime.goexit\n \t/<some_file>:<line_number>\n \x1b[31m.cause.1\x1b[0m\x1b[31m.cause\x1b[0m\x1b[31m=\x1b[0mdeeper error with two causes\n \x1b[31m.cause\x1b[0m\x1b[31m.cause.0\x1b[0m\x1b[31m=\x1b[0mdeeper cause 1\n \x1b[31m.cause.1\x1b[0m\x1b[31m=\x1b[0mdeeper cause 2\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/<some_file>:<line_number>\n testing.tRunner\n \t/<some_file>:<line_number>\n runtime.goexit\n \t/<some_file>:<line_number>\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/<some_file>:<line_number>\n testing.tRunner\n \t/<some_file>:<line_number>\n runtime.goexit\n \t/<some_file>:<line_number>\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/<some_file>:<line_number>\n testing.tRunner\n \t/<some_file>:<line_number>\n runtime.goexit\n \t/<some_file>:<line_number>\n\x1b[31m ↳ nil_panic_PANIC_DISPLAYING_ERROR\x1b[0m\x1b[31m=\x1b[0mPANIC=Panic!\n\x1b[31m ↳ normal_panic\x1b[0m<nil>\n\x1b[31m ↳ stack\x1b[0m\x1b[31m=\x1b[0man error with a stacktrace has occurred\n \x1b[31m.stacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/<some_file>:<line_number>\n testing.tRunner\n \t/<some_file>:<line_number>\n runtime.goexit\n \t/<some_file>:<line_number>\n\x1b[31m ↳ \x1b[0m\x1b[31mstacktrace=\x1b[0mgithub.com/thessem/zap-prettyconsole.TestEncodeEntry\n \t/<some_file>:<line_number>\n testing.tRunner\n \t/<some_file>:<line_number>\n",
ent: zapcore.Entry{
Level: zapcore.ErrorLevel,
Message: "test message",
Expand Down Expand Up @@ -341,11 +354,7 @@ func (t testStableMap) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
type testPanicError string

func (t *testPanicError) Error() string {
if t == nil {
return string(*t)
} else {
panic(*t)
}
panic("Panic!")
}

type nilCauseError struct{}
Expand Down
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (e *prettyConsoleEncoder) encodeError(key string, err error) (retErr error)
}
enc.addSafeString("<nil>")
}
e.buf.Write(enc.buf.Bytes())
_, _ = e.buf.Write(enc.buf.Bytes()) // Explicitly ignore errors
putPrettyConsoleEncoder(enc)

e.inList = true
Expand Down
14 changes: 6 additions & 8 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ func (e *prettyConsoleEncoder) AddReflected(key string, value interface{}) error

switch v := value.(type) {
case formattedString:
iw.Write([]byte(v))
if _, err := iw.Write([]byte(v)); err != nil {
return err
}
default:
if reflectedEncoder := e.cfg.NewReflectedEncoder(iw); e != nil {
if err := reflectedEncoder.Encode(value); err != nil {
return err
}
if err := e.cfg.NewReflectedEncoder(iw).Encode(value); err != nil {
return err
}
if l-enc.buf.Len() == 0 {
// User-supplied reflectedEncoder is a no-op. Fall back to dd
Expand Down Expand Up @@ -183,9 +183,7 @@ func (e *prettyConsoleEncoder) AddDuration(key string, value time.Duration) {
cur := e.buf.Len()
// Both of these append, and we're at the first element of the sublist
e.inList = false
if durationEncoder := e.cfg.EncodeDuration; e != nil {
durationEncoder(value, e)
}
e.cfg.EncodeDuration(value, e)
if cur == e.buf.Len() {
// User-supplied EncodeDuration is a no-op. Fall back to Go format
e.buf.AppendString(value.String())
Expand Down
4 changes: 0 additions & 4 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ func getRecordingEncoder() *recordingEncoder {
return _recorderPool.Get().(*recordingEncoder)
}

func putRecordingEncoder() *recordingEncoder {
return _recorderPool.Get().(*recordingEncoder)
}

func getPrettyConsoleEncoder() *prettyConsoleEncoder {
return _prettyConsolePool.Get().(*prettyConsoleEncoder)
}
Expand Down

0 comments on commit 99b521a

Please sign in to comment.