-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathlog_test.go
139 lines (123 loc) · 4.21 KB
/
log_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logutil
import (
"bufio"
"context"
"io"
"os"
"runtime"
"testing"
"github.com/pingcap/log"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
func TestZapLoggerWithKeys(t *testing.T) {
if runtime.GOOS == "windows" {
// Skip this test on windows for two reason:
// 1. The pattern match fails somehow. It seems windows treat \n as slash and character n.
// 2. Remove file doesn't work as long as the log instance hold the file.
t.Skip("skip on windows")
}
fileCfg := FileLogConfig{log.FileLogConfig{Filename: "zap_log", MaxSize: 4096}}
conf := NewLogConfig("info", DefaultLogFormat, "", fileCfg, false)
err := InitLogger(conf)
require.NoError(t, err)
connID := uint64(123)
ctx := WithConnID(context.Background(), connID)
testZapLogger(ctx, t, fileCfg.Filename, zapLogWithConnIDPattern)
err = os.Remove(fileCfg.Filename)
require.NoError(t, err)
err = InitLogger(conf)
require.NoError(t, err)
key := "ctxKey"
val := "ctxValue"
ctx1 := WithKeyValue(context.Background(), key, val)
testZapLogger(ctx1, t, fileCfg.Filename, zapLogWithKeyValPattern)
err = os.Remove(fileCfg.Filename)
require.NoError(t, err)
}
func testZapLogger(ctx context.Context, t *testing.T, fileName, pattern string) {
Logger(ctx).Debug("debug msg", zap.String("test with key", "true"))
Logger(ctx).Info("info msg", zap.String("test with key", "true"))
Logger(ctx).Warn("warn msg", zap.String("test with key", "true"))
Logger(ctx).Error("error msg", zap.String("test with key", "true"))
f, err := os.Open(fileName)
require.NoError(t, err)
defer func() {
err = f.Close()
require.NoError(t, err)
}()
r := bufio.NewReader(f)
for {
var str string
str, err = r.ReadString('\n')
if err != nil {
break
}
require.Regexp(t, pattern, str)
require.NotContains(t, str, "stack")
require.NotContains(t, str, "errorVerbose")
}
require.Equal(t, io.EOF, err)
}
func TestSetLevel(t *testing.T) {
conf := NewLogConfig("info", DefaultLogFormat, "", EmptyFileLogConfig, false)
err := InitLogger(conf)
require.NoError(t, err)
require.Equal(t, zap.InfoLevel, log.GetLevel())
err = SetLevel("warn")
require.NoError(t, err)
require.Equal(t, zap.WarnLevel, log.GetLevel())
err = SetLevel("Error")
require.NoError(t, err)
require.Equal(t, zap.ErrorLevel, log.GetLevel())
err = SetLevel("DEBUG")
require.NoError(t, err)
require.Equal(t, zap.DebugLevel, log.GetLevel())
}
func TestGrpcLoggerCreation(t *testing.T) {
level := "info"
conf := NewLogConfig(level, DefaultLogFormat, "", EmptyFileLogConfig, false)
_, p, err := initGRPCLogger(conf)
// assert after init grpc logger, the original conf is not changed
require.Equal(t, conf.Level, level)
require.Nil(t, err)
require.Equal(t, p.Level.Level(), zap.ErrorLevel)
os.Setenv("GRPC_DEBUG", "1")
defer os.Unsetenv("GRPC_DEBUG")
_, newP, err := initGRPCLogger(conf)
require.Nil(t, err)
require.Equal(t, newP.Level.Level(), zap.DebugLevel)
}
func TestSlowQueryLoggerCreation(t *testing.T) {
level := "warn"
conf := NewLogConfig(level, DefaultLogFormat, "", EmptyFileLogConfig, false)
_, prop, err := newSlowQueryLogger(conf)
// assert after init slow query logger, the original conf is not changed
require.Equal(t, conf.Level, level)
require.Nil(t, err)
require.Equal(t, prop.Level.String(), conf.Level)
}
func TestGlobalLoggerReplace(t *testing.T) {
fileCfg := FileLogConfig{log.FileLogConfig{Filename: "zap_log", MaxDays: 0, MaxSize: 4096}}
conf := NewLogConfig("info", DefaultLogFormat, "", fileCfg, false)
err := InitLogger(conf)
require.NoError(t, err)
conf.Config.File.MaxDays = 14
err = ReplaceLogger(conf)
require.NoError(t, err)
err = os.Remove(fileCfg.Filename)
require.NoError(t, err)
}