-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
adaptor_test.go
71 lines (58 loc) · 2.17 KB
/
adaptor_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
package lgr
import (
"bytes"
"log"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAdaptor_ToWriter(t *testing.T) {
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Out(rout), Err(rerr), Format(WithMsec))
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local) }
wr := ToWriter(l, "WARN")
sz, err := wr.Write([]byte("something blah 123"))
require.NoError(t, err)
assert.Equal(t, 18, sz)
assert.Equal(t, "2018/01/07 13:02:34.000 WARN something blah 123\n", rout.String())
}
func TestAdaptor_ToWriterNoLevel(t *testing.T) {
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Out(rout), Err(rerr), Msec, LevelBraces)
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local) }
wr := ToWriter(l, "")
sz, err := wr.Write([]byte("something blah 123"))
require.NoError(t, err)
assert.Equal(t, 18, sz)
assert.Equal(t, "2018/01/07 13:02:34.000 [INFO] something blah 123\n", rout.String())
rout.Reset()
rerr.Reset()
_, err = wr.Write([]byte("INFO something blah 123\n"))
require.NoError(t, err)
assert.Equal(t, "2018/01/07 13:02:34.000 [INFO] something blah 123\n", rout.String())
}
func TestAdaptor_ToStdLogger(t *testing.T) {
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Out(rout), Err(rerr), Format(WithMsec))
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local) }
wr := ToStdLogger(l, "WARN")
wr.Print("something\n")
assert.Equal(t, "2018/01/07 13:02:34.000 WARN something\n", rout.String())
rout.Reset()
rerr.Reset()
wr.Printf("xxx %s", "yyy")
assert.Equal(t, "2018/01/07 13:02:34.000 WARN xxx yyy\n", rout.String())
}
func TestSetupStdLogger(t *testing.T) {
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
SetupStdLogger(Out(rout), Err(rerr), Format(WithMsec))
log.Print("something\n")
assert.Contains(t, rout.String(), " INFO something\n")
rout.Reset()
log.Print("[WARN] something\n")
assert.Contains(t, rout.String(), " WARN something\n")
rout.Reset()
log.Print("[DEBUG] something\n")
assert.Empty(t, rout.String())
}