Skip to content

Commit

Permalink
enable logger.With().Context(ctx).Logger() pattern (#7)
Browse files Browse the repository at this point in the history
* enable logger.With().Context(ctx).Logger() pattern

* fix failing tests

* fix more failing tests
  • Loading branch information
mhatch-nxcr authored and pablitoc committed Apr 7, 2023
1 parent 12462d9 commit 4f4dda5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions binary_test.go
Expand Up @@ -5,6 +5,7 @@ package zerolog

import (
"bytes"
"context"
"errors"
"fmt"

Expand Down
6 changes: 6 additions & 0 deletions context.go
Expand Up @@ -19,6 +19,12 @@ func (c Context) Logger() Logger {
return c.l
}

// Context adds the context.Context to the logger ctx.
func (c Context) Context(ctx context.Context) Context {
c.l.ctx = ctx
return c
}

// Fields is a helper function to use a map or slice to set fields using type assertion.
// Only map[string]interface{} and []interface{} are accepted. []interface{} must
// alternate string keys and arbitrary values, and extraneous ones are ignored.
Expand Down
11 changes: 6 additions & 5 deletions hook_test.go
Expand Up @@ -2,33 +2,34 @@ package zerolog

import (
"bytes"
"context"
"io/ioutil"
"testing"
)

var (
levelNameHook = HookFunc(func(e *Event, level Level, msg string) {
levelNameHook = HookFunc(func(ctx context.Context, e *Event, level Level, msg string) {
levelName := level.String()
if level == NoLevel {
levelName = "nolevel"
}
e.Str("level_name", levelName)
})
simpleHook = HookFunc(func(e *Event, level Level, msg string) {
simpleHook = HookFunc(func(ctx context.Context, e *Event, level Level, msg string) {
e.Bool("has_level", level != NoLevel)
e.Str("test", "logged")
})
copyHook = HookFunc(func(e *Event, level Level, msg string) {
copyHook = HookFunc(func(ctx context.Context, e *Event, level Level, msg string) {
hasLevel := level != NoLevel
e.Bool("copy_has_level", hasLevel)
if hasLevel {
e.Str("copy_level", level.String())
}
e.Str("copy_msg", msg)
})
nopHook = HookFunc(func(e *Event, level Level, message string) {
nopHook = HookFunc(func(ctx context.Context, e *Event, level Level, message string) {
})
discardHook = HookFunc(func(e *Event, level Level, message string) {
discardHook = HookFunc(func(ctx context.Context, e *Event, level Level, message string) {
e.Discard()
})
)
Expand Down
5 changes: 5 additions & 0 deletions log.go
Expand Up @@ -100,6 +100,7 @@ package zerolog

import (
"errors"
"context"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -216,6 +217,7 @@ type Logger struct {
level Level
sampler Sampler
context []byte
ctx context.Context
hooks []Hook
stack bool
}
Expand Down Expand Up @@ -459,6 +461,9 @@ func (l *Logger) newEvent(level Level, done func(string)) *Event {
if l.context != nil && len(l.context) > 1 {
e.buf = enc.AppendObjectData(e.buf, l.context)
}
if l.ctx != nil {
e.ctx = l.ctx
}
if l.stack {
e.Stack()
}
Expand Down

0 comments on commit 4f4dda5

Please sign in to comment.