config.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. package log
  14. import (
  15. "time"
  16. "go.uber.org/zap"
  17. "go.uber.org/zap/zapcore"
  18. )
  19. const (
  20. defaultLogMaxSize = 300 // MB
  21. )
  22. // FileLogConfig serializes file log related config in toml/json.
  23. type FileLogConfig struct {
  24. // Log filename, leave empty to disable file log.
  25. Filename string `toml:"filename" json:"filename"`
  26. // Max size for a single file, in MB.
  27. MaxSize int `toml:"max-size" json:"max-size"`
  28. // Max log keep days, default is never deleting.
  29. MaxDays int `toml:"max-days" json:"max-days"`
  30. // Maximum number of old log files to retain.
  31. MaxBackups int `toml:"max-backups" json:"max-backups"`
  32. }
  33. // Config serializes log related config in toml/json.
  34. type Config struct {
  35. // Log level.
  36. Level string `toml:"level" json:"level"`
  37. // Log format. one of json, text, or console.
  38. Format string `toml:"format" json:"format"`
  39. // Disable automatic timestamps in output.
  40. DisableTimestamp bool `toml:"disable-timestamp" json:"disable-timestamp"`
  41. // File log config.
  42. File FileLogConfig `toml:"file" json:"file"`
  43. // Development puts the logger in development mode, which changes the
  44. // behavior of DPanicLevel and takes stacktraces more liberally.
  45. Development bool `toml:"development" json:"development"`
  46. // DisableCaller stops annotating logs with the calling function's file
  47. // name and line number. By default, all logs are annotated.
  48. DisableCaller bool `toml:"disable-caller" json:"disable-caller"`
  49. // DisableStacktrace completely disables automatic stacktrace capturing. By
  50. // default, stacktraces are captured for WarnLevel and above logs in
  51. // development and ErrorLevel and above in production.
  52. DisableStacktrace bool `toml:"disable-stacktrace" json:"disable-stacktrace"`
  53. // DisableErrorVerbose stops annotating logs with the full verbose error
  54. // message.
  55. DisableErrorVerbose bool `toml:"disable-error-verbose" json:"disable-error-verbose"`
  56. // SamplingConfig sets a sampling strategy for the logger. Sampling caps the
  57. // global CPU and I/O load that logging puts on your process while attempting
  58. // to preserve a representative subset of your logs.
  59. //
  60. // Values configured here are per-second. See zapcore.NewSampler for details.
  61. Sampling *zap.SamplingConfig `toml:"sampling" json:"sampling"`
  62. }
  63. // ZapProperties records some information about zap.
  64. type ZapProperties struct {
  65. Core zapcore.Core
  66. Syncer zapcore.WriteSyncer
  67. Level zap.AtomicLevel
  68. }
  69. func newZapTextEncoder(cfg *Config) zapcore.Encoder {
  70. return NewTextEncoder(cfg)
  71. }
  72. func (cfg *Config) buildOptions(errSink zapcore.WriteSyncer) []zap.Option {
  73. opts := []zap.Option{zap.ErrorOutput(errSink)}
  74. if cfg.Development {
  75. opts = append(opts, zap.Development())
  76. }
  77. if !cfg.DisableCaller {
  78. opts = append(opts, zap.AddCaller())
  79. }
  80. stackLevel := zap.ErrorLevel
  81. if cfg.Development {
  82. stackLevel = zap.WarnLevel
  83. }
  84. if !cfg.DisableStacktrace {
  85. opts = append(opts, zap.AddStacktrace(stackLevel))
  86. }
  87. if cfg.Sampling != nil {
  88. opts = append(opts, zap.WrapCore(func(core zapcore.Core) zapcore.Core {
  89. return zapcore.NewSampler(core, time.Second, int(cfg.Sampling.Initial), int(cfg.Sampling.Thereafter))
  90. }))
  91. }
  92. return opts
  93. }