Skip to content

日志配置

zap 日志配置,在 core/zap.go 中初始化全局 logger;访问日志记录开关由 middleware/access_log.gomiddleware/operation.go 消费,截断字节数兜底逻辑在 utils/logger/sanitize.go

yaml

yaml
# zap logger configuration
zap:
  level: 'info'
  format: 'console'
  prefix: '[GIN-VUE-ADMIN]'
  director: 'log'
  show-line: true
  encode-level: 'LowercaseColorLevelEncoder'
  stacktrace-key: 'stacktrace'
  log-in-console: true
  retention-day: 7
  access-req-body: true
  access-resp-data: true
  access-req-headers: true
  access-log-max-bytes: 1024
  file-only-modules:
    - http
    - sql

struct

go
type Zap struct {
	Level             string   `mapstructure:"level" json:"level" yaml:"level"`                                              // 级别
	Prefix            string   `mapstructure:"prefix" json:"prefix" yaml:"prefix"`                                           // 日志前缀
	Format            string   `mapstructure:"format" json:"format" yaml:"format"`                                           // 输出
	Director          string   `mapstructure:"director" json:"director"  yaml:"director"`                                    // 日志文件夹
	EncodeLevel       string   `mapstructure:"encode-level" json:"encode-level" yaml:"encode-level"`                         // 编码级
	StacktraceKey     string   `mapstructure:"stacktrace-key" json:"stacktrace-key" yaml:"stacktrace-key"`                   // 栈名
	ShowLine          bool     `mapstructure:"show-line" json:"show-line" yaml:"show-line"`                                  // 显示行
	LogInConsole      bool     `mapstructure:"log-in-console" json:"log-in-console" yaml:"log-in-console"`                   // 输出控制台
	RetentionDay      int      `mapstructure:"retention-day" json:"retention-day" yaml:"retention-day"`                      // 日志保留天数
	AccessReqBody     bool     `mapstructure:"access-req-body" json:"access-req-body" yaml:"access-req-body"`                // 访问日志记录请求体
	AccessRespData    bool     `mapstructure:"access-resp-data" json:"access-resp-data" yaml:"access-resp-data"`             // 访问日志记录响应体
	AccessReqHeaders  bool     `mapstructure:"access-req-headers" json:"access-req-headers" yaml:"access-req-headers"`       // 访问日志记录请求头
	AccessLogMaxBytes int      `mapstructure:"access-log-max-bytes" json:"access-log-max-bytes" yaml:"access-log-max-bytes"` // 访问日志/操作记录请求体与响应体的最大字节数,超过则截断为占位标记;0 表示用代码兜底默认值
	// 这些模块的日志只进文件,不进控制台(即使 log-in-console 为 true)。
	// 模块名对应 logger.WithCtx(ctx).Mod("xxx") 的 xxx
	FileOnlyModules []string `mapstructure:"file-only-modules" json:"file-only-modules" yaml:"file-only-modules"`
}

description

配置名类型默认值说明
levelstringinfolevel的模式的详细说明,请看zap官方文档
info: info模式,无错误的堆栈信息,只输出信息
debug:debug模式,有错误的堆栈详细信息
warn:warn模式
error: error模式,有错误的堆栈详细信息
dpanic: dpanic模式
panic: panic模式
fatal: fatal模式
formatstringconsoleconsole: 控制台形式输出日志 json: json格式输出日志
prefixstring[GIN-VUE-ADMIN]日志的前缀
directorstringlog存放日志的文件夹,修改即可,不需要手动创建
show_linebooltrue显示行号, 默认为true,不建议修改
encode_levelstringLowercaseColorLevelEncoderLowercaseLevelEncoder:小写
LowercaseColorLevelEncoder:小写带颜色
CapitalLevelEncoder: 大写
CapitalColorLevelEncoder: 大写带颜色
stacktrace_keystringstacktrace堆栈的名称,即在json格式输出日志时的josn的key
log_in_consolebooltrue是否输出到控制台,默认为true
retention_dayint7日志保留天数,负数表示不清理
access-req-bodybooltrue访问日志是否记录请求体(v3.0 新增,记录前会脱敏)
access-resp-databooltrue访问日志是否记录响应体(v3.0 新增)
access-req-headersbooltrue访问日志是否记录请求头(v3.0 新增,authorization/cookie/x-token 等敏感头会脱敏)
access-log-max-bytesint1024访问日志/操作记录中请求体与响应体的最大记录字节数,超出截断;为 0 时兜底 1024(v3.0 新增)
file-only-modules[]string-指定模块(对应 logger.WithCtx(ctx).Mod("xxx") 的 xxx)的日志只写文件、不进控制台,即使 log-in-console 为 true(v3.0 新增)
  • 开发环境 || 调试环境配置建议
    • level:debug
    • format:console
    • encode-level:LowercaseColorLevelEncoder或者encode-leve:CapitalColorLevelEncoder
  • 部署环境配置建议
    • level:error
    • format:json
    • encode-level: LowercaseLevelEncoder 或者 encode-level:CapitalLevelEncoder
    • log-in-console: false
  • 建议只是建议,按照自己的需求进行即可,给出建议仅供参考

注意事项

  • retention-daycore/internal/zap_core.go 中用于日志切割清理,负数表示不清理;官方 config.yaml 示例值为 -1
  • access-log-max-bytes 的兜底逻辑在 utils/logger/sanitize.goAccessLogMaxBytes():配置值大于 0 时生效,否则按 1024 处理。
  • app 节的 node/app-id/env 会作为静态字段注入每条日志,见其他配置