Files
tidb/util/logutil/log_test.go

140 lines
4.2 KiB
Go

// 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)
}