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

add context.Context to Event #395

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zerolog

import (
"context"
"fmt"
"net"
"os"
Expand All @@ -27,6 +28,7 @@ type Event struct {
stack bool // enable error stack trace
ch []Hook // hooks from context
skipFrame int // The number of additional frames to skip when printing the caller.
ctx context.Context
}

func putEvent(e *Event) {
Expand Down Expand Up @@ -771,3 +773,22 @@ func (e *Event) MACAddr(key string, ha net.HardwareAddr) *Event {
e.buf = enc.AppendMACAddr(enc.AppendKey(e.buf, key), ha)
return e
}

// WithContext adds a context to the event
func (e *Event) WithContext(ctx context.Context) *Event {
if e == nil {
return e
}
e.ctx = ctx
return e
}

// Context returns the event context
//
// A nil event will return a background context
func (e *Event) Context() context.Context {
if e == nil {
return context.Background()
}
return e.ctx
}