Documentation
¶
Overview ¶
Package logger 提供基于 zap + lumberjack 的日志封装。
一份配置(LogConfig)同时完成日志级别、输出格式、多目标输出(控制台/滚动文件)与 全局句柄初始化; Init 后替换 zap 全局句柄, 全项目可直接使用 zap.L()/zap.S(), database 包的 gorm SQL 日志同样经此输出。
打日志直接用包级函数, 无需先取 L/S 句柄; Init 之前调用同样安全 (内置标准错误兜底句柄, 日志不会静默丢失):
logger.Info("user login", zap.String("user", "note")) // 结构化日志
logger.Infof("listen on %d", 8080) // 格式化日志
使用示例:
err := logger.Init(logger.LogConfig{
Level: "info",
Format: logger.FormatConsole, // console | json
Console: true,
})
if err != nil {
panic(err)
}
defer logger.Sync() // 进程退出前刷新缓冲
Index ¶
- Constants
- Variables
- func DPanic(msg string, fields ...zap.Field)
- func DPanicf(template string, args ...any)
- func Debug(msg string, fields ...zap.Field)
- func Debugf(template string, args ...any)
- func Error(msg string, fields ...zap.Field)
- func Errorf(template string, args ...any)
- func Fatal(msg string, fields ...zap.Field)
- func Fatalf(template string, args ...any)
- func Info(msg string, fields ...zap.Field)
- func Infof(template string, args ...any)
- func Init(cfg LogConfig) error
- func Panic(msg string, fields ...zap.Field)
- func Panicf(template string, args ...any)
- func Sync()
- func Warn(msg string, fields ...zap.Field)
- func Warnf(template string, args ...any)
- type LogConfig
Constants ¶
View Source
const ( FormatConsole = "console" FormatJSON = "json" )
日志输出格式,由 log.format 配置
Variables ¶
View Source
var ( // L 全局日志句柄; Init 之前为标准错误兜底句柄, 未初始化直接使用也不会 nil panic L = defaultLogger() // S 全局 sugared 日志句柄, 随 L 一起在 Init 时重建 S = L.Sugar() )
Functions ¶
Types ¶
type LogConfig ¶
type LogConfig struct {
Level string `yaml:"level" toml:"level" json:"level" mapstructure:"level"`
Format string `yaml:"format" toml:"format" json:"format" mapstructure:"format"`
Filename string `yaml:"filename" toml:"filename" json:"filename" mapstructure:"filename"`
MaxSize int `yaml:"maxSize" toml:"maxSize" json:"maxSize" mapstructure:"maxSize"`
MaxAge int `yaml:"maxAge" toml:"maxAge" json:"maxAge" mapstructure:"maxAge"`
MaxBackups int `yaml:"maxBackups" toml:"maxBackups" json:"maxBackups" mapstructure:"maxBackups"`
Compress bool `yaml:"compress" toml:"compress" json:"compress" mapstructure:"compress"`
Console bool `yaml:"console" toml:"console" json:"console" mapstructure:"console"`
}
LogConfig 日志配置
Click to show internal directories.
Click to hide internal directories.