| 1 |
// Copyright (c) 2020 Uber Technologies, Inc. |
| 2 |
// |
| 3 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
// of this software and associated documentation files (the "Software"), to deal |
| 5 |
// in the Software without restriction, including without limitation the rights |
| 6 |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 |
// copies of the Software, and to permit persons to whom the Software is |
| 8 |
// furnished to do so, subject to the following conditions: |
| 9 |
// |
| 10 |
// The above copyright notice and this permission notice shall be included in |
| 11 |
// all copies or substantial portions of the Software. |
| 12 |
// |
| 13 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 |
// THE SOFTWARE. |
| 20 |
|
| 21 |
package zapcore_test |
| 22 |
|
| 23 |
import ( |
| 24 |
"fmt" |
| 25 |
"testing" |
| 26 |
|
| 27 |
"github.com/stretchr/testify/assert" |
| 28 |
"github.com/stretchr/testify/require" |
| 29 |
"go.uber.org/zap" |
| 30 |
|
| 31 |
//revive:disable:dot-imports |
| 32 |
. "go.uber.org/zap/zapcore" |
| 33 |
"go.uber.org/zap/zaptest/observer" |
| 34 |
) |
| 35 |
|
| 36 |
func TestIncreaseLevel(t *testing.T) { |
| 37 |
tests := []struct { |
| 38 |
coreLevel Level |
| 39 |
increaseLevel Level |
| 40 |
wantErr bool |
| 41 |
with []Field |
| 42 |
}{ |
| 43 |
{ |
| 44 |
coreLevel: InfoLevel, |
| 45 |
increaseLevel: DebugLevel, |
| 46 |
wantErr: true, |
| 47 |
}, |
| 48 |
{ |
| 49 |
coreLevel: InfoLevel, |
| 50 |
increaseLevel: InfoLevel, |
| 51 |
}, |
| 52 |
{ |
| 53 |
coreLevel: InfoLevel, |
| 54 |
increaseLevel: ErrorLevel, |
| 55 |
}, |
| 56 |
{ |
| 57 |
coreLevel: InfoLevel, |
| 58 |
increaseLevel: ErrorLevel, |
| 59 |
with: []Field{zap.String("k", "v")}, |
| 60 |
}, |
| 61 |
{ |
| 62 |
coreLevel: ErrorLevel, |
| 63 |
increaseLevel: DebugLevel, |
| 64 |
wantErr: true, |
| 65 |
}, |
| 66 |
{ |
| 67 |
coreLevel: ErrorLevel, |
| 68 |
increaseLevel: InfoLevel, |
| 69 |
wantErr: true, |
| 70 |
}, |
| 71 |
{ |
| 72 |
coreLevel: ErrorLevel, |
| 73 |
increaseLevel: WarnLevel, |
| 74 |
wantErr: true, |
| 75 |
}, |
| 76 |
{ |
| 77 |
coreLevel: ErrorLevel, |
| 78 |
increaseLevel: PanicLevel, |
| 79 |
}, |
| 80 |
} |
| 81 |
|
| 82 |
for _, tt := range tests { |
| 83 |
msg := fmt.Sprintf("increase %v to %v", tt.coreLevel, tt.increaseLevel) |
| 84 |
t.Run(msg, func(t *testing.T) { |
| 85 |
logger, logs := observer.New(tt.coreLevel) |
| 86 |
|
| 87 |
// sanity check |
| 88 |
require.Equal(t, tt.coreLevel, LevelOf(logger), "Original logger has the wrong level") |
| 89 |
|
| 90 |
filteredLogger, err := NewIncreaseLevelCore(logger, tt.increaseLevel) |
| 91 |
if tt.wantErr { |
| 92 |
assert.ErrorContains(t, err, "invalid increase level") |
| 93 |
return |
| 94 |
} |
| 95 |
|
| 96 |
if len(tt.with) > 0 { |
| 97 |
filteredLogger = filteredLogger.With(tt.with) |
| 98 |
} |
| 99 |
|
| 100 |
require.NoError(t, err) |
| 101 |
|
| 102 |
t.Run("LevelOf", func(t *testing.T) { |
| 103 |
assert.Equal(t, tt.increaseLevel, LevelOf(filteredLogger), "Filtered logger has the wrong level") |
| 104 |
}) |
| 105 |
|
| 106 |
for l := DebugLevel; l <= FatalLevel; l++ { |
| 107 |
enabled := filteredLogger.Enabled(l) |
| 108 |
entry := Entry{Level: l} |
| 109 |
ce := filteredLogger.Check(entry, nil) |
| 110 |
ce.Write() |
| 111 |
entries := logs.TakeAll() |
| 112 |
|
| 113 |
if l >= tt.increaseLevel { |
| 114 |
assert.True(t, enabled, "expect %v to be enabled", l) |
| 115 |
assert.NotNil(t, ce, "expect non-nil Check") |
| 116 |
assert.NotEmpty(t, entries, "Expect log to be written") |
| 117 |
} else { |
| 118 |
assert.False(t, enabled, "expect %v to be disabled", l) |
| 119 |
assert.Nil(t, ce, "expect nil Check") |
| 120 |
assert.Empty(t, entries, "No logs should have been written") |
| 121 |
} |
| 122 |
|
| 123 |
// Write should always log the entry as per the Core interface |
| 124 |
require.NoError(t, filteredLogger.Write(entry, nil), "Write failed") |
| 125 |
require.NoError(t, filteredLogger.Sync(), "Sync failed") |
| 126 |
assert.NotEmpty(t, logs.TakeAll(), "Write should always log") |
| 127 |
} |
| 128 |
}) |
| 129 |
} |
| 130 |
} |