This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
75 lines (63 loc) · 2.06 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
package logger_test
import (
"io/ioutil"
"os"
"sync"
"testing"
"time"
"github.com/deweppro/go-logger"
"github.com/stretchr/testify/require"
)
func TestNew(t *testing.T) {
require.NotNil(t, logger.Default())
filename, err := ioutil.TempFile(os.TempDir(), "test_new_default-*.log")
require.NoError(t, err)
logger.SetOutput(filename)
logger.SetLevel(logger.LevelDebug)
require.Equal(t, logger.LevelDebug, logger.GetLevel())
go logger.Infof("async %d", 1)
go logger.Warnf("async %d", 2)
go logger.Errorf("async %d", 3)
go logger.Debugf("async %d", 4)
logger.Infof("sync %d", 1)
logger.Warnf("sync %d", 2)
logger.Errorf("sync %d", 3)
logger.Debugf("sync %d", 4)
logger.WithFields(logger.Fields{"ip": "0.0.0.0"}).Infof("context1")
logger.WithFields(logger.Fields{"nil": nil}).Infof("context2")
logger.WithFields(logger.Fields{"func": func() {}}).Infof("context3")
<-time.After(time.Second * 1)
logger.Close()
require.NoError(t, filename.Close())
data, err := ioutil.ReadFile(filename.Name())
require.NoError(t, err)
require.NoError(t, os.Remove(filename.Name()))
sdata := string(data)
require.Contains(t, sdata, `"lvl":"INF","msg":"async 1"`)
require.Contains(t, sdata, `"lvl":"WRN","msg":"async 2"`)
require.Contains(t, sdata, `"lvl":"ERR","msg":"async 3"`)
require.Contains(t, sdata, `"lvl":"DBG","msg":"async 4"`)
require.Contains(t, sdata, `"lvl":"INF","msg":"sync 1"`)
require.Contains(t, sdata, `"lvl":"WRN","msg":"sync 2"`)
require.Contains(t, sdata, `"lvl":"ERR","msg":"sync 3"`)
require.Contains(t, sdata, `"msg":"context1","ctx":{"ip":"0.0.0.0"}`)
require.Contains(t, sdata, `"msg":"context2","ctx":{"nil":null}`)
require.Contains(t, sdata, `"msg":"context3","ctx":{"func":"unsupported field value: (func())`)
}
func BenchmarkNew(b *testing.B) {
b.ReportAllocs()
ll := logger.New()
ll.SetOutput(ioutil.Discard)
ll.SetLevel(logger.LevelDebug)
wg := sync.WaitGroup{}
b.ResetTimer()
b.RunParallel(func(p *testing.PB) {
wg.Add(1)
for p.Next() {
ll.WithFields(logger.Fields{"a": "b"}).Infof("hello")
}
wg.Done()
})
wg.Wait()
ll.Close()
}