Skip to content

Commit

Permalink
Write directly into a single shared buffer per event for even better …
Browse files Browse the repository at this point in the history
…perf
  • Loading branch information
rs committed May 20, 2017
1 parent 19dfb55 commit d0cfcbb
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 307 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ The zerolog package provides a fast and simple logger dedicated to JSON output.
All operations are allocation free (those numbers *include* JSON encoding):

```
BenchmarkLogEmpty-8 50000000 22 ns/op 0 B/op 0 allocs/op
BenchmarkDisabled-8 100000000 9 ns/op 0 B/op 0 allocs/op
BenchmarkInfo-8 10000000 210 ns/op 0 B/op 0 allocs/op
BenchmarkContextFields-8 10000000 254 ns/op 0 B/op 0 allocs/op
BenchmarkLogFields-8 5000000 377 ns/op 0 B/op 0 allocs/op
BenchmarkLogEmpty-8 50000000 19.8 ns/op 0 B/op 0 allocs/op
BenchmarkDisabled-8 100000000 4.73 ns/op 0 B/op 0 allocs/op
BenchmarkInfo-8 10000000 85.1 ns/op 0 B/op 0 allocs/op
BenchmarkContextFields-8 10000000 81.9 ns/op 0 B/op 0 allocs/op
BenchmarkLogFields-8 5000000 247 ns/op 0 B/op 0 allocs/op
```

## Usage
Expand Down
13 changes: 7 additions & 6 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"io/ioutil"
"testing"
"time"
)

var (
Expand Down Expand Up @@ -44,9 +45,9 @@ func BenchmarkInfo(b *testing.B) {
func BenchmarkContextFields(b *testing.B) {
logger := New(ioutil.Discard).With().
Str("string", "four!").
Str("time", "now"). // XXX
Str("duration", "123"). //XXX
Str("another string", "done!").
Time("time", time.Time{}).
Int("int", 123).
Float32("float", -2.203230293249593).
Logger()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
Expand All @@ -63,9 +64,9 @@ func BenchmarkLogFields(b *testing.B) {
for pb.Next() {
logger.Info().
Str("string", "four!").
Str("time", "now"). // XXX
Str("duration", "123"). //XXX
Str("another string", "done!").
Time("time", time.Time{}).
Int("int", 123).
Float32("float", -2.203230293249593).
Msg(fakeMessage)
}
})
Expand Down
76 changes: 43 additions & 33 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,108 +12,118 @@ func (c Context) Logger() Logger {
return c.l
}

func (c Context) append(f field) Context {
return Context{
l: Logger{
parent: c.l,
w: c.l.w,
field: f.compileJSON(),
level: c.l.level,
sample: c.l.sample,
counter: c.l.counter,
},
}
}

// Dict adds the field key with the dict to the logger context.
func (c Context) Dict(key string, dict Event) Context {
dict.buf.WriteByte('}')
return c.append(fRaw(key, dict.buf.String()))
func (c Context) Dict(key string, dict *Event) Context {
dict.buf = append(dict.buf, '}')
c.l.context = append(appendKey(c.l.context, key), dict.buf...)
eventPool.Put(dict)
return c
}

// Str adds the field key with val as a string to the logger context.
func (c Context) Str(key, val string) Context {
return c.append(fStr(key, val))
c.l.context = appendString(c.l.context, key, val)
return c
}

// Err adds the field "error" with err as a string to the logger context.
// To customize the key name, change zerolog.ErrorFieldName.
func (c Context) Err(err error) Context {
return c.append(fErr(err))
c.l.context = appendError(c.l.context, err)
return c
}

// Bool adds the field key with val as a Boolean to the logger context.
func (c Context) Bool(key string, b bool) Context {
return c.append(fBool(key, b))
c.l.context = appendBool(c.l.context, key, b)
return c
}

// Int adds the field key with i as a int to the logger context.
func (c Context) Int(key string, i int) Context {
return c.append(fInt(key, i))
c.l.context = appendInt(c.l.context, key, i)
return c
}

// Int8 adds the field key with i as a int8 to the logger context.
func (c Context) Int8(key string, i int8) Context {
return c.append(fInt8(key, i))
c.l.context = appendInt8(c.l.context, key, i)
return c
}

// Int16 adds the field key with i as a int16 to the logger context.
func (c Context) Int16(key string, i int16) Context {
return c.append(fInt16(key, i))
c.l.context = appendInt16(c.l.context, key, i)
return c
}

// Int32 adds the field key with i as a int32 to the logger context.
func (c Context) Int32(key string, i int32) Context {
return c.append(fInt32(key, i))
c.l.context = appendInt32(c.l.context, key, i)
return c
}

// Int64 adds the field key with i as a int64 to the logger context.
func (c Context) Int64(key string, i int64) Context {
return c.append(fInt64(key, i))
c.l.context = appendInt64(c.l.context, key, i)
return c
}

// Uint adds the field key with i as a uint to the logger context.
func (c Context) Uint(key string, i uint) Context {
return c.append(fUint(key, i))
c.l.context = appendUint(c.l.context, key, i)
return c
}

// Uint8 adds the field key with i as a uint8 to the logger context.
func (c Context) Uint8(key string, i uint8) Context {
return c.append(fUint8(key, i))
c.l.context = appendUint8(c.l.context, key, i)
return c
}

// Uint16 adds the field key with i as a uint16 to the logger context.
func (c Context) Uint16(key string, i uint16) Context {
return c.append(fUint16(key, i))
c.l.context = appendUint16(c.l.context, key, i)
return c
}

// Uint32 adds the field key with i as a uint32 to the logger context.
func (c Context) Uint32(key string, i uint32) Context {
return c.append(fUint32(key, i))
c.l.context = appendUint32(c.l.context, key, i)
return c
}

// Uint64 adds the field key with i as a uint64 to the logger context.
func (c Context) Uint64(key string, i uint64) Context {
return c.append(fUint64(key, i))
c.l.context = appendUint64(c.l.context, key, i)
return c
}

// Float32 adds the field key with f as a float32 to the logger context.
func (c Context) Float32(key string, f float32) Context {
return c.append(fFloat32(key, f))
c.l.context = appendFloat32(c.l.context, key, f)
return c
}

// Float64 adds the field key with f as a float64 to the logger context.
func (c Context) Float64(key string, f float64) Context {
return c.append(fFloat64(key, f))
c.l.context = appendFloat64(c.l.context, key, f)
return c
}

// Timestamp adds the current local time as UNIX timestamp to the logger context with the "time" key.
// To customize the key name, change zerolog.TimestampFieldName.
func (c Context) Timestamp() Context {
return c.append(fTimestamp())
if len(c.l.context) > 0 {
c.l.context[0] = 1
} else {
c.l.context = append(c.l.context, 1)
}
return c
}

// Time adds the field key with t formated as string using zerolog.TimeFieldFormat.
func (c Context) Time(key string, t time.Time) Context {
return c.append(fTime(key, t))
c.l.context = appendTime(c.l.context, key, t)
return c
}
Loading

0 comments on commit d0cfcbb

Please sign in to comment.