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

Restore padding #4

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions _examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ func main() {
log.WithField("foo", "bar").Info("info with a more increased padding")
log.ResetPadding()
log.WithError(errors.New("some error")).Error("error")
log.RestorePadding()
log.WithField("foo", "bar").Info("info with a restored padding")
log.WithError(errors.New("some fatal error")).Fatal("fatal")
}
5 changes: 5 additions & 0 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func (e *Entry) ResetPadding() {
e.Logger.ResetPadding()
}

// RestorePadding restore the padding previously resetted.
func (e *Entry) RestorePadding() {
e.Logger.RestorePadding()
}

// IncreasePadding increases the padding 1 times.
func (e *Entry) IncreasePadding() {
e.Logger.IncreasePadding()
Expand Down
1 change: 1 addition & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Interface interface {
Errorf(string, ...interface{})
Fatalf(string, ...interface{})
ResetPadding()
RestorePadding()
IncreasePadding()
DecreasePadding()
}
15 changes: 11 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,24 @@ func (f Fields) Names() (v []string) {

// Logger represents a logger with configurable Level and Handler.
type Logger struct {
mu sync.Mutex
Writer io.Writer
Level Level
Padding int
mu sync.Mutex
Writer io.Writer
Level Level
Padding int
resettedPadding int
}

// ResetPadding resets the padding to default.
func (l *Logger) ResetPadding() {
l.resettedPadding = l.Padding
l.Padding = defaultPadding
}

// RestorePadding restore the padding previously resetted.
func (l *Logger) RestorePadding() {
l.Padding = l.resettedPadding
}

// IncreasePadding increases the padding 1 times.
func (l *Logger) IncreasePadding() {
l.Padding += defaultPadding
Expand Down
5 changes: 5 additions & 0 deletions pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func ResetPadding() {
Log.ResetPadding()
}

// RestorePadding restore the padding previously resetted.
func RestorePadding() {
Log.RestorePadding()
}

// IncreasePadding increases the padding 1 times.
func IncreasePadding() {
Log.IncreasePadding()
Expand Down
3 changes: 3 additions & 0 deletions pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func TestRootLogOptions(t *testing.T) {
log.IncreasePadding()
log.Info("increased")
log.ResetPadding()
log.Info("resetted")
log.RestorePadding()
log.Info("restored")
pet := &Pet{"Tobi", 3}
log.WithFields(pet).Info("add pet")
requireEqualOutput(t, out.Bytes())
Expand Down
4 changes: 3 additions & 1 deletion testdata/TestRootLogOptions.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
 ⨯ warn 1
 • foo foo=bar
 • increased
 • add pet age=3 name=Tobi
 • resetted
 • restored
 • add pet age=3 name=Tobi