Skip to content

Commit

Permalink
Add MarshalText to AtomicLevel (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZymoticB authored and akshayjshah committed Apr 24, 2017
1 parent f2fca9a commit bb5f9d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 7 additions & 0 deletions level.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,10 @@ func (lvl *AtomicLevel) UnmarshalText(text []byte) error {
lvl.SetLevel(l)
return nil
}

// MarshalText marshals the AtomicLevel to a byte slice. It uses the same
// text representation as the static zapcore.Levels ("debug", "info", "warn",
// "error", "dpanic", "panic", and "fatal").
func (lvl AtomicLevel) MarshalText() (text []byte, err error) {
return lvl.Level().MarshalText()
}
10 changes: 9 additions & 1 deletion level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestAtomicLevelMutation(t *testing.T) {
wg.Wait()
}

func TestAtomicLevelUnmarshalText(t *testing.T) {
func TestAtomicLevelText(t *testing.T) {
tests := []struct {
text string
expect zapcore.Level
Expand Down Expand Up @@ -104,5 +104,13 @@ func TestAtomicLevelUnmarshalText(t *testing.T) {
assert.Equal(t, tt.expect, lvl.Level(), "Unexpected level after unmarshaling.")
lvl.SetLevel(InfoLevel)
}

// Test marshalling
if tt.text != "" && !tt.err {
lvl.SetLevel(tt.expect)
marshaled, err := lvl.MarshalText()
assert.NoError(t, err, `Unexpected error marshalling level "%v" to text.`, tt.expect)
assert.Equal(t, tt.text, string(marshaled), "Expected marshaled text to match")
}
}
}

0 comments on commit bb5f9d5

Please sign in to comment.