分享
Go日志框架zap与lumberjack简单配置封装
clawhub · · 12273 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
简介
zap
zap是uber开源的Go高性能日志库
https://github.com/uber-go/zap
lumberjack
Lumberjack用于将日志写入滚动文件。zap 不支持文件归档,如果要支持文件按大小或者时间归档,需要使用lumberjack,lumberjack也是zap官方推荐的。
https://github.com/natefinch/lumberjack
简单使用
工程结构
项目结构.png
核心代码
LogCore
/**
* 获取日志
* filePath 日志文件路径
* level 日志级别
* maxSize 每个日志文件保存的最大尺寸 单位:M
* maxBackups 日志文件最多保存多少个备份
* maxAge 文件最多保存多少天
* compress 是否压缩
* serviceName 服务名
*/
func NewLogger(filePath string, level zapcore.Level, maxSize int, maxBackups int, maxAge int, compress bool, serviceName string) *zap.Logger {
core := newCore(filePath, level, maxSize, maxBackups, maxAge, compress)
return zap.New(core, zap.AddCaller(), zap.Development(), zap.Fields(zap.String("serviceName", serviceName)))
}
/**
* zapcore构造
*/
func newCore(filePath string, level zapcore.Level, maxSize int, maxBackups int, maxAge int, compress bool) zapcore.Core {
//日志文件路径配置2
hook := lumberjack.Logger{
Filename: filePath, // 日志文件路径
MaxSize: maxSize, // 每个日志文件保存的最大尺寸 单位:M
MaxBackups: maxBackups, // 日志文件最多保存多少个备份
MaxAge: maxAge, // 文件最多保存多少天
Compress: compress, // 是否压缩
}
// 设置日志级别
atomicLevel := zap.NewAtomicLevel()
atomicLevel.SetLevel(level)
//公用编码器
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "linenum",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder, // 小写编码器
EncodeTime: zapcore.ISO8601TimeEncoder, // ISO8601 UTC 时间格式
EncodeDuration: zapcore.SecondsDurationEncoder, //
EncodeCaller: zapcore.FullCallerEncoder, // 全路径编码器
EncodeName: zapcore.FullNameEncoder,
}
return zapcore.NewCore(
zapcore.NewJSONEncoder(encoderConfig), // 编码器配置
zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(&hook)), // 打印到控制台和文件
atomicLevel, // 日志级别
)
}
Log
var MainLogger *zap.Logger
var GatewayLogger *zap.Logger
func init() {
MainLogger = NewLogger("./logs/main.log", zapcore.InfoLevel, 128, 30, 7, true, "Main")
GatewayLogger = NewLogger("./logs/gateway.log", zapcore.DebugLevel, 128, 30, 7, true, "Gateway")
}
AppMain
func main() {
fmt.Println("init main")
log.MainLogger.Debug("hello main Debug")
log.MainLogger.Info("hello main Info")
log.GatewayLogger.Debug("Hi Gateway Im Debug")
log.GatewayLogger.Info("Hi Gateway Im Info")
}
github原码
https://github.com/ClawHub/go-study
参考
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信12273 次点击
被以下专栏收入,发现更多相似内容
上一篇:Go冒泡排序练习
下一篇:study_go_day5
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
简介
zap
zap是uber开源的Go高性能日志库
https://github.com/uber-go/zap
lumberjack
Lumberjack用于将日志写入滚动文件。zap 不支持文件归档,如果要支持文件按大小或者时间归档,需要使用lumberjack,lumberjack也是zap官方推荐的。
https://github.com/natefinch/lumberjack
简单使用
工程结构
项目结构.png
核心代码
LogCore
/**
* 获取日志
* filePath 日志文件路径
* level 日志级别
* maxSize 每个日志文件保存的最大尺寸 单位:M
* maxBackups 日志文件最多保存多少个备份
* maxAge 文件最多保存多少天
* compress 是否压缩
* serviceName 服务名
*/
func NewLogger(filePath string, level zapcore.Level, maxSize int, maxBackups int, maxAge int, compress bool, serviceName string) *zap.Logger {
core := newCore(filePath, level, maxSize, maxBackups, maxAge, compress)
return zap.New(core, zap.AddCaller(), zap.Development(), zap.Fields(zap.String("serviceName", serviceName)))
}
/**
* zapcore构造
*/
func newCore(filePath string, level zapcore.Level, maxSize int, maxBackups int, maxAge int, compress bool) zapcore.Core {
//日志文件路径配置2
hook := lumberjack.Logger{
Filename: filePath, // 日志文件路径
MaxSize: maxSize, // 每个日志文件保存的最大尺寸 单位:M
MaxBackups: maxBackups, // 日志文件最多保存多少个备份
MaxAge: maxAge, // 文件最多保存多少天
Compress: compress, // 是否压缩
}
// 设置日志级别
atomicLevel := zap.NewAtomicLevel()
atomicLevel.SetLevel(level)
//公用编码器
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "linenum",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder, // 小写编码器
EncodeTime: zapcore.ISO8601TimeEncoder, // ISO8601 UTC 时间格式
EncodeDuration: zapcore.SecondsDurationEncoder, //
EncodeCaller: zapcore.FullCallerEncoder, // 全路径编码器
EncodeName: zapcore.FullNameEncoder,
}
return zapcore.NewCore(
zapcore.NewJSONEncoder(encoderConfig), // 编码器配置
zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(&hook)), // 打印到控制台和文件
atomicLevel, // 日志级别
)
}
Log
var MainLogger *zap.Logger
var GatewayLogger *zap.Logger
func init() {
MainLogger = NewLogger("./logs/main.log", zapcore.InfoLevel, 128, 30, 7, true, "Main")
GatewayLogger = NewLogger("./logs/gateway.log", zapcore.DebugLevel, 128, 30, 7, true, "Gateway")
}
AppMain
func main() {
fmt.Println("init main")
log.MainLogger.Debug("hello main Debug")
log.MainLogger.Info("hello main Info")
log.GatewayLogger.Debug("Hi Gateway Im Debug")
log.GatewayLogger.Info("Hi Gateway Im Info")
}
github原码
https://github.com/ClawHub/go-study