From cef6e7a2cd30468f4e641a1f024282647528c473 Mon Sep 17 00:00:00 2001 From: Tyler Dixon Date: Fri, 21 Apr 2017 09:11:16 -0700 Subject: [PATCH] Add MarshalText to AtomicLevel --- 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") + } } }