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

simplify public interface - replace *Entry with Interface in interface.go #60

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 9 additions & 8 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
"time"
)

// assert interface compliance.
var _ Interface = (*Entry)(nil)

// Now returns the current time.
var Now = time.Now

Expand All @@ -32,7 +29,11 @@ func NewEntry(log *Logger) *Entry {
}

// WithFields returns a new entry with `fields` set.
func (e *Entry) WithFields(fields Fielder) *Entry {
func (e *Entry) WithFields(fields Fielder) Interface {
return e.withFields(fields)
}

func (e *Entry) withFields(fields Fielder) *Entry {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget a comment :DDD

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha will do (comment and move interface definitions).

f := []Fields{}
f = append(f, e.fields...)
f = append(f, fields.Fields())
Expand All @@ -43,15 +44,15 @@ func (e *Entry) WithFields(fields Fielder) *Entry {
}

// WithField returns a new entry with the `key` and `value` set.
func (e *Entry) WithField(key string, value interface{}) *Entry {
func (e *Entry) WithField(key string, value interface{}) Interface {
return e.WithFields(Fields{key: value})
}

// WithError returns a new entry with the "error" set to `err`.
//
// The given error may implement .Fielder, if it does the method
// will add all its `.Fields()` into the returned entry.
func (e *Entry) WithError(err error) *Entry {
func (e *Entry) WithError(err error) Interface {
ctx := e.WithField("error", err.Error())

if s, ok := err.(stackTracer); ok {
Expand Down Expand Up @@ -129,9 +130,9 @@ func (e *Entry) Fatalf(msg string, v ...interface{}) {

// Trace returns a new entry with a Stop method to fire off
// a corresponding completion log, useful with defer.
func (e *Entry) Trace(msg string) *Entry {
func (e *Entry) Trace(msg string) Interface {
e.Info(msg)
v := e.WithFields(e.Fields)
v := e.withFields(e.Fields)
v.Message = msg
v.start = time.Now()
return v
Expand Down
10 changes: 5 additions & 5 deletions entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ func TestEntry_WithFields(t *testing.T) {

b := a.WithFields(Fields{"foo": "bar"})
assert.Equal(t, Fields{}, a.mergedFields())
assert.Equal(t, Fields{"foo": "bar"}, b.mergedFields())
assert.Equal(t, Fields{"foo": "bar"}, b.(*Entry).mergedFields())

c := a.WithFields(Fields{"foo": "hello", "bar": "world"})
c := a.WithFields(Fields{"foo": "hello", "bar": "world"}).(*Entry)

e := c.finalize(InfoLevel, "upload")
assert.Equal(t, e.Message, "upload")
Expand All @@ -28,19 +28,19 @@ func TestEntry_WithField(t *testing.T) {
a := NewEntry(nil)
b := a.WithField("foo", "bar")
assert.Equal(t, Fields{}, a.mergedFields())
assert.Equal(t, Fields{"foo": "bar"}, b.mergedFields())
assert.Equal(t, Fields{"foo": "bar"}, b.(*Entry).mergedFields())
}

func TestEntry_WithError(t *testing.T) {
a := NewEntry(nil)
b := a.WithError(fmt.Errorf("boom"))
assert.Equal(t, Fields{}, a.mergedFields())
assert.Equal(t, Fields{"error": "boom"}, b.mergedFields())
assert.Equal(t, Fields{"error": "boom"}, b.(*Entry).mergedFields())
}

func TestEntry_WithErrorFields(t *testing.T) {
a := NewEntry(nil)
b := a.WithError(errFields("boom"))
b := a.WithError(errFields("boom")).(*Entry)
assert.Equal(t, Fields{}, a.mergedFields())
assert.Equal(t, Fields{
"error": "boom",
Expand Down
8 changes: 4 additions & 4 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package log

// Interface represents the API of both Logger and Entry.
type Interface interface {
WithFields(fields Fielder) *Entry
WithField(key string, value interface{}) *Entry
WithError(err error) *Entry
WithFields(fields Fielder) Interface
WithField(key string, value interface{}) Interface
WithError(err error) Interface
Debug(msg string)
Info(msg string)
Warn(msg string)
Expand All @@ -15,5 +15,5 @@ type Interface interface {
Warnf(msg string, v ...interface{})
Errorf(msg string, v ...interface{})
Fatalf(msg string, v ...interface{})
Trace(msg string) *Entry
Trace(msg string) Interface
}
11 changes: 4 additions & 7 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import (
"sort"
)

// assert interface compliance.
var _ Interface = (*Logger)(nil)

// Fielder is an interface for providing fields to custom types.
type Fielder interface {
Fields() Fields
Expand Down Expand Up @@ -62,20 +59,20 @@ type Logger struct {
}

// WithFields returns a new entry with `fields` set.
func (l *Logger) WithFields(fields Fielder) *Entry {
func (l *Logger) WithFields(fields Fielder) Interface {
return NewEntry(l).WithFields(fields.Fields())
}

// WithField returns a new entry with the `key` and `value` set.
//
// Note that the `key` should not have spaces in it - use camel
// case or underscores
func (l *Logger) WithField(key string, value interface{}) *Entry {
func (l *Logger) WithField(key string, value interface{}) Interface {
return NewEntry(l).WithField(key, value)
}

// WithError returns a new entry with the "error" set to `err`.
func (l *Logger) WithError(err error) *Entry {
func (l *Logger) WithError(err error) Interface {
return NewEntry(l).WithError(err)
}

Expand Down Expand Up @@ -131,7 +128,7 @@ func (l *Logger) Fatalf(msg string, v ...interface{}) {

// Trace returns a new entry with a Stop method to fire off
// a corresponding completion log, useful with defer.
func (l *Logger) Trace(msg string) *Entry {
func (l *Logger) Trace(msg string) Interface {
return NewEntry(l).Trace(msg)
}

Expand Down
8 changes: 4 additions & 4 deletions pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ func SetLevelFromString(s string) {
}

// WithFields returns a new entry with `fields` set.
func WithFields(fields Fielder) *Entry {
func WithFields(fields Fielder) Interface {
return Log.WithFields(fields)
}

// WithField returns a new entry with the `key` and `value` set.
func WithField(key string, value interface{}) *Entry {
func WithField(key string, value interface{}) Interface {
return Log.WithField(key, value)
}

// WithError returns a new entry with the "error" set to `err`.
func WithError(err error) *Entry {
func WithError(err error) Interface {
return Log.WithError(err)
}

Expand Down Expand Up @@ -95,6 +95,6 @@ func Fatalf(msg string, v ...interface{}) {

// Trace returns a new entry with a Stop method to fire off
// a corresponding completion log, useful with defer.
func Trace(msg string) *Entry {
func Trace(msg string) Interface {
return Log.Trace(msg)
}