From bb5f9d518b7dce3474db51e00f41cd8391a41121 Mon Sep 17 00:00:00 2001 From: ZymoticB Date: Sun, 23 Apr 2017 21:23:11 -0700 Subject: [PATCH] Add MarshalText to AtomicLevel (#416) --- level.go | 7 +++++++ level_test.go | 10 +++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/level.go b/level.go index 86cc98a8b..591e1a08a 100644 --- a/level.go +++ b/level.go @@ -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() +} diff --git a/level_test.go b/level_test.go index 3092bb904..fd8827d6e 100644 --- a/level_test.go +++ b/level_test.go @@ -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 @@ -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") + } } }