Skip to content

Commit

Permalink
Add Logger.SetLevelsOutput() and Logger.EnableLevelsCaller() to suppo…
Browse files Browse the repository at this point in the history
…rt batch setup.
  • Loading branch information
edoger committed Jun 17, 2021
1 parent 0adb6c6 commit b7aa77b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
28 changes: 27 additions & 1 deletion logger.go
Expand Up @@ -39,7 +39,12 @@ type Logger interface {
// SetLevelOutput sets the current logger level output writer.
// The level output writer is used to write log data of a given level.
// If the given writer is nil, the level writer will be disabled.
SetLevelOutput(level Level, w io.Writer) Logger
SetLevelOutput(Level, io.Writer) Logger

// SetLevelsOutput sets the current logger levels output writer.
// The level output writer is used to write log data of a given level.
// If the given writer is nil, the levels writer will be disabled.
SetLevelsOutput([]Level, io.Writer) Logger

// SetOutputInterceptor sets the output interceptor for the current logger.
// If the given interceptor is nil, the log data is written to the output writer.
Expand Down Expand Up @@ -75,6 +80,9 @@ type Logger interface {
// EnableLevelCaller enables caller reporting on logs of a given level.
EnableLevelCaller(Level, ...int) Logger

// EnableLevelsCaller enables caller reporting on logs of the given levels.
EnableLevelsCaller([]Level, ...int) Logger

// AddHook adds the given log hook to the current logger.
AddHook(Hook) Logger

Expand Down Expand Up @@ -123,6 +131,16 @@ func (o *logger) SetLevelOutput(level Level, w io.Writer) Logger {
return o
}

// SetLevelsOutput sets the current logger levels output writer.
// The level output writer is used to write log data of a given level.
// If the given writer is nil, the levels writer will be disabled.
func (o *logger) SetLevelsOutput(levels []Level, w io.Writer) Logger {
for i, j := 0, len(levels); i < j; i++ {
o.SetLevelOutput(levels[i], w)
}
return o
}

// SetOutputInterceptor sets the output interceptor for the current logger.
// If the given interceptor is nil, the log data is written to the output writer.
func (o *logger) SetOutputInterceptor(f func(Summary, io.Writer) (int, error)) Logger {
Expand Down Expand Up @@ -209,6 +227,14 @@ func (o *logger) EnableLevelCaller(level Level, skip ...int) Logger {
return o
}

// EnableLevelsCaller enables caller reporting on logs of the given levels.
func (o *logger) EnableLevelsCaller(levels []Level, skip ...int) Logger {
for i, j := 0, len(levels); i < j; i++ {
o.EnableLevelCaller(levels[i], skip...)
}
return o
}

// AddHook adds the given log hook to the current logger.
func (o *logger) AddHook(hook Hook) Logger {
o.core.hooks.Add(hook)
Expand Down
8 changes: 4 additions & 4 deletions logger_caller_test.go
Expand Up @@ -48,12 +48,12 @@ func TestLogger_EnableCaller(t *testing.T) {
}
}

func TestLogger_EnableLevelCaller(t *testing.T) {
func TestLogger_EnableLevelsCaller(t *testing.T) {
w := new(bytes.Buffer)
o := New("test")
o.SetOutput(w)
o.SetLevel(TraceLevel)
o.EnableLevelCaller(DebugLevel)
o.EnableLevelsCaller([]Level{DebugLevel})
o.Debug("bar") // LINE 57
o.Info("foo") // LINE 58

Expand All @@ -66,7 +66,7 @@ func TestLogger_EnableLevelCaller(t *testing.T) {
}

w.Reset()
o.EnableLevelCaller(DebugLevel, 1)
o.EnableLevelsCaller([]Level{DebugLevel}, 1)
testLogCaller(o) // LINE 70

got = w.String()
Expand All @@ -81,7 +81,7 @@ func TestLogger_WithCaller(t *testing.T) {
o.SetOutput(w)
o.SetLevel(TraceLevel)

o.WithCaller().Info("test") // LINE 84
o.WithCaller().Info("test") // LINE 84
o.WithCaller(1).WithCaller().WithCaller().Info("test") // LINE 85

got := w.String()
Expand Down
14 changes: 13 additions & 1 deletion logger_test.go
Expand Up @@ -79,7 +79,19 @@ func TestLogger_SetLevelOutput(t *testing.T) {
t.Fatal("Logger.SetLevelOutput(): nil")
}
if o.SetLevelOutput(InfoLevel, nil) == nil {
t.Fatal("Logger.SetLevelOutput(io.Writer, nil): nil")
t.Fatal("Logger.SetLevelOutput(Level, nil): nil")
}
}

func TestLogger_SetLevelsOutput(t *testing.T) {
o := New("test")
w := new(bytes.Buffer)

if o.SetLevelsOutput([]Level{InfoLevel, WarnLevel}, w) == nil {
t.Fatal("Logger.SetLevelsOutput(): nil")
}
if o.SetLevelsOutput([]Level{InfoLevel, WarnLevel}, nil) == nil {
t.Fatal("Logger.SetLevelsOutput(Level, nil): nil")
}
}

Expand Down

0 comments on commit b7aa77b

Please sign in to comment.