Skip to content

Commit

Permalink
Entry WithoutPadding
Browse files Browse the repository at this point in the history
  • Loading branch information
nervo committed Nov 6, 2022
1 parent e773385 commit 2faaaa7
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 9 deletions.
1 change: 1 addition & 0 deletions _examples/main.go
Expand Up @@ -18,6 +18,7 @@ func main() {
log.IncreasePadding()
log.WithField("foo", "bar").Info("info with increased padding")
log.IncreasePadding()
log.WithoutPadding().WithField("foo", "bar").Info("info without padding")
log.WithField("foo", "bar").Info("info with a more increased padding")
log.ResetPadding()
log.WithError(errors.New("some error")).Error("error")
Expand Down
16 changes: 13 additions & 3 deletions entry.go
Expand Up @@ -18,13 +18,15 @@ type Entry struct {
Fields Fields `json:"fields"`
Level Level `json:"level"`
Message string `json:"message"`
Padding int
fields []Fields
}

// NewEntry returns a new entry for `log`.
func NewEntry(log *Logger) *Entry {
return &Entry{
Logger: log,
Logger: log,
Padding: log.Padding,
}
}

Expand All @@ -49,8 +51,9 @@ func (e *Entry) WithFields(fields Fielder) *Entry {
f = append(f, e.fields...)
f = append(f, fields.Fields())
return &Entry{
Logger: e.Logger,
fields: f,
Logger: e.Logger,
Padding: e.Padding,
fields: f,
}
}

Expand All @@ -77,6 +80,12 @@ func (e *Entry) WithError(err error) *Entry {
return ctx
}

// WithoutPadding returns entry without padding set to false.
func (e *Entry) WithoutPadding() *Entry {
e.Padding = defaultPadding

This comment has been minimized.

Copy link
@bep

bep Dec 8, 2022

I suspect people would expect a copy of e here.

This comment has been minimized.

Copy link
@nervo

nervo Dec 8, 2022

Author Owner

@bep sorry, i don't get exactly what you mean :)

This comment has been minimized.

Copy link
@bep

bep Dec 8, 2022

The other With* methods creates a new copy of Entry. This method mutates e which I suspect would be surprising behaviour.

This comment has been minimized.

Copy link
@nervo

nervo Dec 9, 2022

Author Owner

ok, i get it :) See caarlos0#7

return e
}

// Debug level message.
func (e *Entry) Debug(msg string) {
e.Logger.log(DebugLevel, e, msg)
Expand Down Expand Up @@ -146,6 +155,7 @@ func (e *Entry) finalize(level Level, msg string) *Entry {
return &Entry{
Logger: e.Logger,
Fields: e.mergedFields(),
Padding: e.Padding,
Level: level,
Message: msg,
}
Expand Down
26 changes: 21 additions & 5 deletions entry_test.go
Expand Up @@ -2,14 +2,15 @@ package log

import (
"fmt"
"io"
"testing"

"github.com/matryer/is"
)

func TestEntry_WithFields(t *testing.T) {
is := is.New(t)
a := NewEntry(nil)
a := NewEntry(New(io.Discard))
is.Equal(a.Fields, nil)

b := a.WithFields(Fields{"foo": "bar"})
Expand All @@ -26,23 +27,23 @@ func TestEntry_WithFields(t *testing.T) {

func TestEntry_WithField(t *testing.T) {
is := is.New(t)
a := NewEntry(nil)
a := NewEntry(New(io.Discard))
b := a.WithField("foo", "bar")
is.Equal(Fields{}, a.mergedFields())
is.Equal(Fields{"foo": "bar"}, b.mergedFields())
}

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

func TestEntry_WithError_fields(t *testing.T) {
is := is.New(t)
a := NewEntry(nil)
a := NewEntry(New(io.Discard))
b := a.WithError(errFields("boom"))
is.Equal(Fields{}, a.mergedFields())
is.Equal(Fields{
Expand All @@ -53,12 +54,27 @@ func TestEntry_WithError_fields(t *testing.T) {

func TestEntry_WithError_nil(t *testing.T) {
is := is.New(t)
a := NewEntry(nil)
a := NewEntry(New(io.Discard))
b := a.WithError(nil)
is.Equal(Fields{}, a.mergedFields())
is.Equal(Fields{}, b.mergedFields())
}

func TestEntry_WithoutPadding(t *testing.T) {
is := is.New(t)
log := New(io.Discard)

a := NewEntry(log)
is.Equal(defaultPadding, a.Padding)

log.IncreasePadding()
b := NewEntry(log)
is.Equal(defaultPadding+2, b.Padding)

c := b.WithoutPadding()
is.Equal(defaultPadding, c.Padding)
}

type errFields string

func (ef errFields) Error() string {
Expand Down
1 change: 1 addition & 0 deletions interface.go
Expand Up @@ -5,6 +5,7 @@ type Interface interface {
WithFields(Fielder) *Entry
WithField(string, interface{}) *Entry
WithError(error) *Entry
WithoutPadding() *Entry
Debug(string)
Info(string)
Warn(string)
Expand Down
7 changes: 6 additions & 1 deletion logger.go
Expand Up @@ -94,7 +94,7 @@ func (l *Logger) handleLog(e *Entry) {
fmt.Fprintf(
l.Writer,
"%s %-*s",
style.Render(fmt.Sprintf("%*s", 1+l.Padding, level)),
style.Render(fmt.Sprintf("%*s", 1+e.Padding, level)),
l.rightPadding(names),
e.Message,
)
Expand Down Expand Up @@ -131,6 +131,11 @@ func (l *Logger) WithError(err error) *Entry {
return NewEntry(l).WithError(err)
}

// WithoutPadding returns a new entry without padding.
func (l *Logger) WithoutPadding() *Entry {
return NewEntry(l).WithoutPadding()
}

// Debug level message.
func (l *Logger) Debug(msg string) {
NewEntry(l).Debug(msg)
Expand Down
5 changes: 5 additions & 0 deletions pkg.go
Expand Up @@ -61,6 +61,11 @@ func WithError(err error) *Entry {
return Log.WithError(err)
}

// WithoutPadding returns a new entry without padding.
func WithoutPadding() *Entry {
return Log.WithoutPadding()
}

// Debug level message.
func Debug(msg string) {
Log.Debug(msg)
Expand Down
2 changes: 2 additions & 0 deletions pkg_test.go
Expand Up @@ -49,6 +49,8 @@ func TestRootLogOptions(t *testing.T) {
log.WithField("foo", "bar").Info("foo")
log.IncreasePadding()
log.Info("increased")
log.WithoutPadding().Info("without padding")
log.Info("increased")
log.ResetPadding()
pet := &Pet{"Tobi", 3}
log.WithFields(pet).Info("add pet")
Expand Down
2 changes: 2 additions & 0 deletions testdata/TestRootLogOptions.golden
Expand Up @@ -7,4 +7,6 @@
 ⨯ warn 1
 • foo foo=bar
 • increased
 • without padding
 • increased
 • add pet age=3 name=Tobi

0 comments on commit 2faaaa7

Please sign in to comment.