| 1 |
// Copyright 2019 PingCAP, Inc. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package log |
| 15 |
|
| 16 |
import ( |
| 17 |
"time" |
| 18 |
|
| 19 |
"go.uber.org/zap" |
| 20 |
"go.uber.org/zap/zapcore" |
| 21 |
) |
| 22 |
|
| 23 |
const ( |
| 24 |
defaultLogMaxSize = 300 // MB |
| 25 |
) |
| 26 |
|
| 27 |
// FileLogConfig serializes file log related config in toml/json. |
| 28 |
type FileLogConfig struct { |
| 29 |
// Log filename, leave empty to disable file log. |
| 30 |
Filename string `toml:"filename" json:"filename"` |
| 31 |
// Max size for a single file, in MB. |
| 32 |
MaxSize int `toml:"max-size" json:"max-size"` |
| 33 |
// Max log keep days, default is never deleting. |
| 34 |
MaxDays int `toml:"max-days" json:"max-days"` |
| 35 |
// Maximum number of old log files to retain. |
| 36 |
MaxBackups int `toml:"max-backups" json:"max-backups"` |
| 37 |
} |
| 38 |
|
| 39 |
// Config serializes log related config in toml/json. |
| 40 |
type Config struct { |
| 41 |
// Log level. |
| 42 |
Level string `toml:"level" json:"level"` |
| 43 |
// Log format. one of json, text, or console. |
| 44 |
Format string `toml:"format" json:"format"` |
| 45 |
// Disable automatic timestamps in output. |
| 46 |
DisableTimestamp bool `toml:"disable-timestamp" json:"disable-timestamp"` |
| 47 |
// File log config. |
| 48 |
File FileLogConfig `toml:"file" json:"file"` |
| 49 |
// Development puts the logger in development mode, which changes the |
| 50 |
// behavior of DPanicLevel and takes stacktraces more liberally. |
| 51 |
Development bool `toml:"development" json:"development"` |
| 52 |
// DisableCaller stops annotating logs with the calling function's file |
| 53 |
// name and line number. By default, all logs are annotated. |
| 54 |
DisableCaller bool `toml:"disable-caller" json:"disable-caller"` |
| 55 |
// DisableStacktrace completely disables automatic stacktrace capturing. By |
| 56 |
// default, stacktraces are captured for WarnLevel and above logs in |
| 57 |
// development and ErrorLevel and above in production. |
| 58 |
DisableStacktrace bool `toml:"disable-stacktrace" json:"disable-stacktrace"` |
| 59 |
// DisableErrorVerbose stops annotating logs with the full verbose error |
| 60 |
// message. |
| 61 |
DisableErrorVerbose bool `toml:"disable-error-verbose" json:"disable-error-verbose"` |
| 62 |
// SamplingConfig sets a sampling strategy for the logger. Sampling caps the |
| 63 |
// global CPU and I/O load that logging puts on your process while attempting |
| 64 |
// to preserve a representative subset of your logs. |
| 65 |
// |
| 66 |
// Values configured here are per-second. See zapcore.NewSampler for details. |
| 67 |
Sampling *zap.SamplingConfig `toml:"sampling" json:"sampling"` |
| 68 |
} |
| 69 |
|
| 70 |
// ZapProperties records some information about zap. |
| 71 |
type ZapProperties struct { |
| 72 |
Core zapcore.Core |
| 73 |
Syncer zapcore.WriteSyncer |
| 74 |
Level zap.AtomicLevel |
| 75 |
} |
| 76 |
|
| 77 |
func newZapTextEncoder(cfg *Config) zapcore.Encoder { |
| 78 |
return NewTextEncoder(cfg) |
| 79 |
} |
| 80 |
|
| 81 |
func (cfg *Config) buildOptions(errSink zapcore.WriteSyncer) []zap.Option { |
| 82 |
opts := []zap.Option{zap.ErrorOutput(errSink)} |
| 83 |
|
| 84 |
if cfg.Development { |
| 85 |
opts = append(opts, zap.Development()) |
| 86 |
} |
| 87 |
|
| 88 |
if !cfg.DisableCaller { |
| 89 |
opts = append(opts, zap.AddCaller()) |
| 90 |
} |
| 91 |
|
| 92 |
stackLevel := zap.ErrorLevel |
| 93 |
if cfg.Development { |
| 94 |
stackLevel = zap.WarnLevel |
| 95 |
} |
| 96 |
if !cfg.DisableStacktrace { |
| 97 |
opts = append(opts, zap.AddStacktrace(stackLevel)) |
| 98 |
} |
| 99 |
|
| 100 |
if cfg.Sampling != nil { |
| 101 |
opts = append(opts, zap.WrapCore(func(core zapcore.Core) zapcore.Core { |
| 102 |
return zapcore.NewSampler(core, time.Second, int(cfg.Sampling.Initial), int(cfg.Sampling.Thereafter)) |
| 103 |
})) |
| 104 |
} |
| 105 |
return opts |
| 106 |
} |