| 1 |
// Copyright (c) 2016 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 |
package zapcore_test |
| 21 |
|
| 22 |
import ( |
| 23 |
"testing" |
| 24 |
"time" |
| 25 |
|
| 26 |
"github.com/stretchr/testify/assert" |
| 27 |
//revive:disable:dot-imports |
| 28 |
. "go.uber.org/zap/zapcore" |
| 29 |
) |
| 30 |
|
| 31 |
var testEntry = Entry{ |
| 32 |
LoggerName: "main", |
| 33 |
Level: InfoLevel, |
| 34 |
Message: `hello`, |
| 35 |
Time: _epoch, |
| 36 |
Stack: "fake-stack", |
| 37 |
Caller: EntryCaller{Defined: true, File: "foo.go", Line: 42, Function: "foo.Foo"}, |
| 38 |
} |
| 39 |
|
| 40 |
func TestConsoleEncodeEntry(t *testing.T) { |
| 41 |
tests := []struct { |
| 42 |
desc string |
| 43 |
expected string |
| 44 |
ent Entry |
| 45 |
fields []Field |
| 46 |
}{ |
| 47 |
{ |
| 48 |
desc: "info no fields", |
| 49 |
expected: "2018-06-19T16:33:42Z\tinfo\tbob\tlob law\n", |
| 50 |
ent: Entry{ |
| 51 |
Level: InfoLevel, |
| 52 |
Time: time.Date(2018, 6, 19, 16, 33, 42, 99, time.UTC), |
| 53 |
LoggerName: "bob", |
| 54 |
Message: "lob law", |
| 55 |
}, |
| 56 |
}, |
| 57 |
{ |
| 58 |
desc: "zero_time_omitted", |
| 59 |
expected: "info\tname\tmessage\n", |
| 60 |
ent: Entry{ |
| 61 |
Level: InfoLevel, |
| 62 |
Time: time.Time{}, |
| 63 |
LoggerName: "name", |
| 64 |
Message: "message", |
| 65 |
}, |
| 66 |
}, |
| 67 |
} |
| 68 |
|
| 69 |
cfg := testEncoderConfig() |
| 70 |
cfg.EncodeTime = RFC3339TimeEncoder |
| 71 |
enc := NewConsoleEncoder(cfg) |
| 72 |
|
| 73 |
for _, tt := range tests { |
| 74 |
t.Run(tt.desc, func(t *testing.T) { |
| 75 |
buf, err := enc.EncodeEntry(tt.ent, tt.fields) |
| 76 |
if assert.NoError(t, err, "Unexpected console encoding error.") { |
| 77 |
assert.Equal(t, tt.expected, buf.String(), "Incorrect encoded entry.") |
| 78 |
} |
| 79 |
buf.Free() |
| 80 |
}) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func TestConsoleSeparator(t *testing.T) { |
| 85 |
tests := []struct { |
| 86 |
desc string |
| 87 |
separator string |
| 88 |
wantConsole string |
| 89 |
}{ |
| 90 |
{ |
| 91 |
desc: "space console separator", |
| 92 |
separator: " ", |
| 93 |
wantConsole: "0 info main foo.go:42 foo.Foo hello\nfake-stack\n", |
| 94 |
}, |
| 95 |
{ |
| 96 |
desc: "default console separator", |
| 97 |
separator: "", |
| 98 |
wantConsole: "0\tinfo\tmain\tfoo.go:42\tfoo.Foo\thello\nfake-stack\n", |
| 99 |
}, |
| 100 |
{ |
| 101 |
desc: "tag console separator", |
| 102 |
separator: "\t", |
| 103 |
wantConsole: "0\tinfo\tmain\tfoo.go:42\tfoo.Foo\thello\nfake-stack\n", |
| 104 |
}, |
| 105 |
{ |
| 106 |
desc: "dash console separator", |
| 107 |
separator: "--", |
| 108 |
wantConsole: "0--info--main--foo.go:42--foo.Foo--hello\nfake-stack\n", |
| 109 |
}, |
| 110 |
} |
| 111 |
|
| 112 |
for _, tt := range tests { |
| 113 |
console := NewConsoleEncoder(encoderTestEncoderConfig(tt.separator)) |
| 114 |
t.Run(tt.desc, func(t *testing.T) { |
| 115 |
entry := testEntry |
| 116 |
consoleOut, err := console.EncodeEntry(entry, nil) |
| 117 |
if !assert.NoError(t, err) { |
| 118 |
return |
| 119 |
} |
| 120 |
assert.Equal( |
| 121 |
t, |
| 122 |
tt.wantConsole, |
| 123 |
consoleOut.String(), |
| 124 |
"Unexpected console output", |
| 125 |
) |
| 126 |
}) |
| 127 |
|
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
func encoderTestEncoderConfig(separator string) EncoderConfig { |
| 132 |
testEncoder := testEncoderConfig() |
| 133 |
testEncoder.ConsoleSeparator = separator |
| 134 |
return testEncoder |
| 135 |
} |