Skip to content

Commit

Permalink
Add NewAtomicLevelAt helper function (#411)
Browse files Browse the repository at this point in the history
Add a helper for the common new-and-set pattern.
  • Loading branch information
bufdev authored and akshayjshah committed Apr 21, 2017
1 parent 47cfaea commit f2fca9a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
7 changes: 2 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func NewProductionEncoderConfig() zapcore.EncoderConfig {
// Stacktraces are automatically included on logs of ErrorLevel and above.
func NewProductionConfig() Config {
return Config{
Level: NewAtomicLevel(),
Level: NewAtomicLevelAt(InfoLevel),
Development: false,
Sampling: &SamplingConfig{
Initial: 100,
Expand Down Expand Up @@ -138,11 +138,8 @@ func NewDevelopmentEncoderConfig() zapcore.EncoderConfig {
// console encoder, writes to standard error, and disables sampling.
// Stacktraces are automatically included on logs of WarnLevel and above.
func NewDevelopmentConfig() Config {
dyn := NewAtomicLevel()
dyn.SetLevel(DebugLevel)

return Config{
Level: dyn,
Level: NewAtomicLevelAt(DebugLevel),
Development: true,
Encoding: "console",
EncoderConfig: NewDevelopmentEncoderConfig(),
Expand Down
8 changes: 8 additions & 0 deletions level.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ func NewAtomicLevel() AtomicLevel {
}
}

// NewAtomicLevelAt is a convienence function that creates an AtomicLevel
// and then calls SetLevel with the given level.
func NewAtomicLevelAt(l zapcore.Level) AtomicLevel {
a := NewAtomicLevel()
a.SetLevel(l)
return a
}

// Enabled implements the zapcore.LevelEnabler interface, which allows the
// AtomicLevel to be used in place of traditional static levels.
func (lvl AtomicLevel) Enabled(l zapcore.Level) bool {
Expand Down
2 changes: 2 additions & 0 deletions level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func TestNewAtomicLevel(t *testing.T) {
assert.Equal(t, InfoLevel, lvl.Level(), "Unexpected initial level.")
lvl.SetLevel(ErrorLevel)
assert.Equal(t, ErrorLevel, lvl.Level(), "Unexpected level after SetLevel.")
lvl = NewAtomicLevelAt(WarnLevel)
assert.Equal(t, WarnLevel, lvl.Level(), "Unexpected level after SetLevel.")
}

func TestAtomicLevelMutation(t *testing.T) {
Expand Down

0 comments on commit f2fca9a

Please sign in to comment.