Mention fields duplication caveat in documentation (#41)

This commit is contained in:
Kai Ren 2018-03-08 17:41:28 +02:00 committed by Olivier Poitrey
parent 8c1c6a0cd7
commit b62d797a8d
4 changed files with 34 additions and 0 deletions

View File

@ -476,3 +476,18 @@ Log a static string, without any context or `printf`-style templating:
| logrus | 1244 ns/op | 1505 B/op | 27 allocs/op | | logrus | 1244 ns/op | 1505 B/op | 27 allocs/op |
| apex/log | 2751 ns/op | 584 B/op | 11 allocs/op | | apex/log | 2751 ns/op | 584 B/op | 11 allocs/op |
| log15 | 5181 ns/op | 1592 B/op | 26 allocs/op | | log15 | 5181 ns/op | 1592 B/op | 26 allocs/op |
## Caveats
There is no fields deduplication out-of-the-box.
Using the same key multiple times creates new key in final JSON each time.
```go
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
logger.Info().
Timestamp().
Msg("dup")
// Output: {"level":"info","time":1494567715,"time":1494567715,"message":"dup"}
```
However, its not a big deal though as JSON accepts dup keys, the last one prevails.

View File

@ -277,6 +277,8 @@ var th = timestampHook{}
// Timestamp adds the current local time as UNIX timestamp to the logger context with the "time" key. // 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. // To customize the key name, change zerolog.TimestampFieldName.
//
// NOTE: It won't dedupe the "time" key if the *Context has one already.
func (c Context) Timestamp() Context { func (c Context) Timestamp() Context {
c.l = c.l.Hook(th) c.l = c.l.Hook(th)
return c return c

View File

@ -499,6 +499,9 @@ func (e *Event) Floats64(key string, f []float64) *Event {
// Timestamp adds the current local time as UNIX timestamp to the *Event context with the "time" key. // Timestamp adds the current local time as UNIX timestamp to the *Event context with the "time" key.
// To customize the key name, change zerolog.TimestampFieldName. // To customize the key name, change zerolog.TimestampFieldName.
//
// NOTE: It won't dedupe the "time" key if the *Event (or *Context) has one
// already.
func (e *Event) Timestamp() *Event { func (e *Event) Timestamp() *Event {
if e == nil { if e == nil {
return e return e

14
log.go
View File

@ -82,6 +82,20 @@
// log.Warn().Msg("") // log.Warn().Msg("")
// // Output: {"level":"warn","severity":"warn"} // // Output: {"level":"warn","severity":"warn"}
// //
//
// Caveats
//
// There is no fields deduplication out-of-the-box.
// Using the same key multiple times creates new key in final JSON each time.
//
// logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
// logger.Info().
// Timestamp().
// Msg("dup")
// // Output: {"level":"info","time":1494567715,"time":1494567715,"message":"dup"}
//
// However, its not a big deal though as JSON accepts dup keys,
// the last one prevails.
package zerolog package zerolog
import ( import (