Skip to content

Commit

Permalink
feat: optimize for discard (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma authored Mar 16, 2022
1 parent 9d8f564 commit d51f019
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
25 changes: 15 additions & 10 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package logger_test

import (
"io"
"os"
"testing"

Expand All @@ -10,7 +9,7 @@ import (
)

func BenchmarkLogger_Logfmt(b *testing.B) {
log := logger.New(io.Discard, logger.LogfmtFormat(), logger.Debug)
log := logger.New(discard{}, logger.LogfmtFormat(), logger.Debug)

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -22,7 +21,7 @@ func BenchmarkLogger_Logfmt(b *testing.B) {
}

func BenchmarkLogger_Json(b *testing.B) {
log := logger.New(io.Discard, logger.JSONFormat(), logger.Debug)
log := logger.New(discard{}, logger.JSONFormat(), logger.Debug)

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -34,7 +33,7 @@ func BenchmarkLogger_Json(b *testing.B) {
}

func BenchmarkLogger_LogfmtWriter(b *testing.B) {
log := logger.New(io.Discard, logger.LogfmtFormat(), logger.Debug)
log := logger.New(discard{}, logger.LogfmtFormat(), logger.Debug)
w := log.Writer(logger.Info)

p := []byte("some message")
Expand All @@ -49,7 +48,7 @@ func BenchmarkLogger_LogfmtWriter(b *testing.B) {
}

func BenchmarkLogger_LogfmtWithTS(b *testing.B) {
log := logger.New(io.Discard, logger.LogfmtFormat(), logger.Debug)
log := logger.New(discard{}, logger.LogfmtFormat(), logger.Debug)

cancel := log.WithTimestamp()
defer cancel()
Expand All @@ -64,7 +63,7 @@ func BenchmarkLogger_LogfmtWithTS(b *testing.B) {
}

func BenchmarkLogger_JsonWithTS(b *testing.B) {
log := logger.New(io.Discard, logger.JSONFormat(), logger.Debug)
log := logger.New(discard{}, logger.JSONFormat(), logger.Debug)

cancel := log.WithTimestamp()
defer cancel()
Expand All @@ -79,7 +78,7 @@ func BenchmarkLogger_JsonWithTS(b *testing.B) {
}

func BenchmarkLogger_LogfmtCtx(b *testing.B) {
log := logger.New(io.Discard, logger.LogfmtFormat(), logger.Debug).With(ctx.Str("_n", "bench"), ctx.Int("_p", 1))
log := logger.New(discard{}, logger.LogfmtFormat(), logger.Debug).With(ctx.Str("_n", "bench"), ctx.Int("_p", 1))

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -91,7 +90,7 @@ func BenchmarkLogger_LogfmtCtx(b *testing.B) {
}

func BenchmarkLogger_JsonCtx(b *testing.B) {
log := logger.New(io.Discard, logger.JSONFormat(), logger.Debug).With(ctx.Str("_n", "bench"), ctx.Int("_p", 1))
log := logger.New(discard{}, logger.JSONFormat(), logger.Debug).With(ctx.Str("_n", "bench"), ctx.Int("_p", 1))

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -103,7 +102,7 @@ func BenchmarkLogger_JsonCtx(b *testing.B) {
}

func BenchmarkLevelLogger_Logfmt(b *testing.B) {
log := logger.New(io.Discard, logger.LogfmtFormat(), logger.Debug).With(ctx.Str("_n", "bench"), ctx.Int("_p", os.Getpid()))
log := logger.New(discard{}, logger.LogfmtFormat(), logger.Debug).With(ctx.Str("_n", "bench"), ctx.Int("_p", os.Getpid()))

b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
Expand All @@ -117,7 +116,7 @@ func BenchmarkLevelLogger_Logfmt(b *testing.B) {
}

func BenchmarkLevelLogger_Json(b *testing.B) {
log := logger.New(io.Discard, logger.LogfmtFormat(), logger.Debug).With(ctx.Str("_n", "bench"), ctx.Int("_p", os.Getpid()))
log := logger.New(discard{}, logger.LogfmtFormat(), logger.Debug).With(ctx.Str("_n", "bench"), ctx.Int("_p", os.Getpid()))

b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
Expand All @@ -129,3 +128,9 @@ func BenchmarkLevelLogger_Json(b *testing.B) {
}
})
}

type discard struct{}

func (discard) Write(p []byte) (int, error) {
return len(p), nil
}
37 changes: 23 additions & 14 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,23 @@ type Field func(*Event)

// Logger is a logger.
type Logger struct {
w io.Writer
fmtr Formatter
timeFn func() int64
ctx []byte
lvl Level
w io.Writer
isDiscard bool
fmtr Formatter
timeFn func() int64
ctx []byte
lvl Level
}

// New creates a new Logger.
func New(w io.Writer, fmtr Formatter, lvl Level) *Logger {
isDiscard := w == io.Discard

return &Logger{
w: w,
fmtr: fmtr,
lvl: lvl,
w: w,
isDiscard: isDiscard,
fmtr: fmtr,
lvl: lvl,
}
}

Expand Down Expand Up @@ -128,11 +132,12 @@ func (l *Logger) With(ctx ...Field) *Logger {
copy(b, e.buf.Bytes())

return &Logger{
w: l.w,
fmtr: l.fmtr,
timeFn: l.timeFn,
lvl: l.lvl,
ctx: b,
w: l.w,
isDiscard: l.isDiscard,
fmtr: l.fmtr,
timeFn: l.timeFn,
lvl: l.lvl,
ctx: b,
}
}

Expand Down Expand Up @@ -172,6 +177,10 @@ func (fn writerFunc) Write(p []byte) (n int, err error) {
func (l *Logger) Writer(lvl Level) io.Writer {
return writerFunc(func(p []byte) (n int, err error) {
n = len(p)
if l.isDiscard {
return n, nil
}

if n > 0 && p[n-1] == '\n' {
p = p[:n-1]
}
Expand All @@ -182,7 +191,7 @@ func (l *Logger) Writer(lvl Level) io.Writer {
}

func (l *Logger) write(msg string, lvl Level, ctx []Field) {
if lvl > l.lvl {
if l.isDiscard || lvl > l.lvl {
return
}

Expand Down

0 comments on commit d51f019

Please sign in to comment.