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 WithContext #919

Merged
merged 1 commit into from Mar 11, 2019
Merged
Show file tree
Hide file tree
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
14 changes: 12 additions & 2 deletions entry.go
Expand Up @@ -2,6 +2,7 @@ package logrus

import (
"bytes"
"context"
"fmt"
"os"
"reflect"
Expand Down Expand Up @@ -69,6 +70,9 @@ type Entry struct {
// When formatter is called in entry.log(), a Buffer may be set to entry
Buffer *bytes.Buffer

// Contains the context set by the user. Useful for hook processing etc.
Context context.Context

// err may contain a field formatting error
err string
}
Expand Down Expand Up @@ -97,6 +101,12 @@ func (entry *Entry) WithError(err error) *Entry {
return entry.WithField(ErrorKey, err)
}

// Add a context to the Entry.
func (entry *Entry) WithContext(ctx context.Context) *Entry {
entry.Context = ctx
return entry
}

// Add a single field to the Entry.
func (entry *Entry) WithField(key string, value interface{}) *Entry {
return entry.WithFields(Fields{key: value})
Expand Down Expand Up @@ -130,12 +140,12 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
data[k] = v
}
}
return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time, err: fieldErr}
return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time, err: fieldErr, Context: entry.Context}
}

// Overrides the time of the Entry.
func (entry *Entry) WithTime(t time.Time) *Entry {
return &Entry{Logger: entry.Logger, Data: entry.Data, Time: t, err: entry.err}
return &Entry{Logger: entry.Logger, Data: entry.Data, Time: t, err: entry.err, Context: entry.Context}
}

// getPackageName reduces a fully qualified function name to the package name
Expand Down
14 changes: 14 additions & 0 deletions entry_test.go
Expand Up @@ -2,6 +2,7 @@ package logrus

import (
"bytes"
"context"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -33,6 +34,19 @@ func TestEntryWithError(t *testing.T) {

}

func TestEntryWithContext(t *testing.T) {
assert := assert.New(t)
ctx := context.WithValue(context.Background(), "foo", "bar")

assert.Equal(ctx, WithContext(ctx).Context)

logger := New()
logger.Out = &bytes.Buffer{}
entry := NewEntry(logger)

assert.Equal(ctx, entry.WithContext(ctx).Context)
}

func TestEntryPanicln(t *testing.T) {
errBoom := fmt.Errorf("boom time")

Expand Down
6 changes: 6 additions & 0 deletions exported.go
@@ -1,6 +1,7 @@
package logrus

import (
"context"
"io"
"time"
)
Expand Down Expand Up @@ -55,6 +56,11 @@ func WithError(err error) *Entry {
return std.WithField(ErrorKey, err)
}

// WithContext creates an entry from the standard logger and adds a context to it.
func WithContext(ctx context.Context) *Entry {
return std.WithContext(ctx)
}

// WithField creates an entry from the standard logger and adds a field to
// it. If you want multiple fields, use `WithFields`.
//
Expand Down
8 changes: 8 additions & 0 deletions logger.go
@@ -1,6 +1,7 @@
package logrus

import (
"context"
"io"
"os"
"sync"
Expand Down Expand Up @@ -124,6 +125,13 @@ func (logger *Logger) WithError(err error) *Entry {
return entry.WithError(err)
}

// Add a context to the log entry.
func (logger *Logger) WithContext(ctx context.Context) *Entry {
entry := logger.newEntry()
defer logger.releaseEntry(entry)
return entry.WithContext(ctx)
}

// Overrides the time of the log entry.
func (logger *Logger) WithTime(t time.Time) *Entry {
entry := logger.newEntry()
Expand Down