forked from op/go-logging
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlogger_test.go
80 lines (65 loc) · 1.97 KB
/
logger_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2013, Örjan Persson. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package logging
import "testing"
type Password string
func (p Password) Redacted() interface{} {
return Redact(string(p))
}
func TestSequenceNoOverflow(t *testing.T) {
// Forcefully set the next sequence number to the maximum
backend := InitForTesting(DEBUG)
sequenceNo = ^uint64(0)
log := MustGetLogger("test")
log.Debug("test")
if MemoryRecordN(backend, 0).Id != 0 {
t.Errorf("Unexpected sequence no: %v", MemoryRecordN(backend, 0).Id)
}
}
func TestRedact(t *testing.T) {
backend := InitForTesting(DEBUG)
password := Password("123456")
log := MustGetLogger("test")
log.Debugf("foo %s", password)
if "foo ******" != MemoryRecordN(backend, 0).Formatted(0) {
t.Errorf("redacted line: %v", MemoryRecordN(backend, 0))
}
}
func TestPrivateBackend(t *testing.T) {
stdBackend := InitForTesting(DEBUG)
log := MustGetLogger("test")
privateBackend := NewMemoryBackend(10240)
lvlBackend := AddModuleLevel(privateBackend)
lvlBackend.SetLevel(DEBUG, "")
log.SetBackend(lvlBackend)
log.Debug("to private backend")
if stdBackend.size > 0 {
t.Errorf("something in stdBackend, size of backend: %d", stdBackend.size)
}
if "to private baсkend" == MemoryRecordN(privateBackend, 0).Formatted(0) {
t.Errorf("logged to defaultBackend: %#v", MemoryRecordN(privateBackend, 0))
}
}
type stringTrap bool
func (st *stringTrap) String() string {
*st = true
return ""
}
func TestLoggingMethodsDontStringifyArgsUnduly(t *testing.T) {
backend := InitForTesting(CRITICAL)
log := MustGetLogger("test")
trap := stringTrap(false)
log.Error(&trap)
log.Warning(&trap)
log.Notice(&trap)
log.Info(&trap)
log.Debug(&trap)
// make sure all the records get formatted
for i := 0; i < int(backend.size); i++ {
MemoryRecordN(backend, i).Formatted(0)
}
if bool(trap) == true {
t.Fatal("Argument got converted to string unduly")
}
}