Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let user decide what to do if logger is not in context #177

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,29 @@ log.Ctx(ctx).Info().Msg("hello world")
// Output: {"component":"module","level":"info","message":"hello world"}
```

### Use default logger from context

If one try to get logger from context that has not logger within it the default, disabled logger will be returned
```go
ctx := context.Background()

log.Ctx(ctx).Info().Msg("hello world")

// No output by default!
```
You can easily substitute default logger with your own instance
```go
logger := log.With().Str("component", "module").Logger()

zerolog.DefaultLogger = &logger

ctx := context.Background()

log.Ctx(ctx).Info().Msg("hello world")

// Output: {"component": "module", "level": "info", "message": "hello world"}
```

### Set as standard logger output

```go
Expand Down
9 changes: 6 additions & 3 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import (
"context"
)

var disabledLogger *Logger
// DefaultLogger is used if user tries to get Logger from context.Context that has not Logger within it
// By default it is disabled, meaning it logs nothing at all,
// but it can be easily substituted by custom instance with different behavior.
var DefaultLogger *Logger

func init() {
l := Nop()
disabledLogger = &l
DefaultLogger = &l
}

type ctxKey struct{}
Expand Down Expand Up @@ -43,5 +46,5 @@ func Ctx(ctx context.Context) *Logger {
if l, ok := ctx.Value(ctxKey{}).(*Logger); ok {
return l
}
return disabledLogger
return DefaultLogger
}
12 changes: 10 additions & 2 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestCtx(t *testing.T) {
}

log2 = Ctx(context.Background())
if log2 != disabledLogger {
if log2 != DefaultLogger || log2.level != Disabled {
t.Error("Ctx did not return the expected logger")
}
}
Expand Down Expand Up @@ -58,6 +58,14 @@ func TestCtxDisabled(t *testing.T) {

ctx = dl.WithContext(ctx)
if Ctx(ctx) != &dl {
t.Error("WithContext did not overide logger with a disabled logger")
t.Error("WithContext did not override logger with a disabled logger")
}
}

func TestCtxCustomDefault(t *testing.T) {
logger := New(ioutil.Discard).With().Str("custom_field", "custom_value").Logger()
DefaultLogger = &logger
if Ctx(context.Background()) != &logger {
t.Error("default logger has not been substituted")
}
}
2 changes: 1 addition & 1 deletion log.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (l Logger) With() Context {
//
// Use this method with caution. If unsure, prefer the With method.
func (l *Logger) UpdateContext(update func(c Context) Context) {
if l == disabledLogger {
if l == DefaultLogger {
return
}
if cap(l.context) == 0 {
Expand Down