-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
slow_query_logger.go
76 lines (65 loc) · 3.23 KB
/
slow_query_logger.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
package logutil
import (
"fmt"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"go.uber.org/zap"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
)
var _pool = buffer.NewPool()
func newSlowQueryLogger(cfg *LogConfig) (*zap.Logger, error) {
// reuse global config and override slow query log file
// if slow query log filename is empty, slow query log will behave the same as global log
sqConfig := &cfg.Config
if len(cfg.SlowQueryFile) != 0 {
sqConfig.File = log.FileLogConfig{
MaxSize: cfg.File.MaxSize,
Filename: cfg.SlowQueryFile,
}
}
// create the slow query logger
sqLogger, prop, err := log.InitLogger(sqConfig)
if err != nil {
return nil, errors.Trace(err)
}
// replace 2018-12-19-unified-log-format text encoder with slow log encoder
sqLogger = sqLogger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return log.NewTextCore(&slowLogEncoder{}, prop.Syncer, prop.Level)
}))
return sqLogger, nil
}
type slowLogEncoder struct{}
func (e *slowLogEncoder) EncodeEntry(entry zapcore.Entry, _ []zapcore.Field) (*buffer.Buffer, error) {
b := _pool.Get()
fmt.Fprintf(b, "# Time: %s\n", entry.Time.Format(SlowLogTimeFormat))
fmt.Fprintf(b, "%s\n", entry.Message)
return b, nil
}
func (e *slowLogEncoder) Clone() zapcore.Encoder { return e }
func (e *slowLogEncoder) AddArray(string, zapcore.ArrayMarshaler) error { return nil }
func (e *slowLogEncoder) AddObject(string, zapcore.ObjectMarshaler) error { return nil }
func (e *slowLogEncoder) AddBinary(string, []byte) {}
func (e *slowLogEncoder) AddByteString(string, []byte) {}
func (e *slowLogEncoder) AddBool(string, bool) {}
func (e *slowLogEncoder) AddComplex128(string, complex128) {}
func (e *slowLogEncoder) AddComplex64(string, complex64) {}
func (e *slowLogEncoder) AddDuration(string, time.Duration) {}
func (e *slowLogEncoder) AddFloat64(string, float64) {}
func (e *slowLogEncoder) AddFloat32(string, float32) {}
func (e *slowLogEncoder) AddInt(string, int) {}
func (e *slowLogEncoder) AddInt64(string, int64) {}
func (e *slowLogEncoder) AddInt32(string, int32) {}
func (e *slowLogEncoder) AddInt16(string, int16) {}
func (e *slowLogEncoder) AddInt8(string, int8) {}
func (e *slowLogEncoder) AddString(string, string) {}
func (e *slowLogEncoder) AddTime(string, time.Time) {}
func (e *slowLogEncoder) AddUint(string, uint) {}
func (e *slowLogEncoder) AddUint64(string, uint64) {}
func (e *slowLogEncoder) AddUint32(string, uint32) {}
func (e *slowLogEncoder) AddUint16(string, uint16) {}
func (e *slowLogEncoder) AddUint8(string, uint8) {}
func (e *slowLogEncoder) AddUintptr(string, uintptr) {}
func (e *slowLogEncoder) AddReflected(string, interface{}) error { return nil }
func (e *slowLogEncoder) OpenNamespace(string) {}