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

Add MarshalText to AtomicLevel #416

Merged
merged 1 commit into from
Apr 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}
}