当前位置:网站首页>go语言记录日志uber-go/zap/lumberjack的用法
go语言记录日志uber-go/zap/lumberjack的用法
2022-04-21 06:58:00 【唐维康】
0 需求分析
- 日志切割能够根据文件大小、时间或间隔等来切割日志文件;
- 支持不同的日志级别,例如 DEBUG , INFO , WARN , ERROR 等;
- 能够打印基本信息,如调用文件、函数名和行号,日志时间等;
- 根据时间或者天数来保存日志信息
1 环境安装
go get -u go.uber.org/zap
go get -v github.com/uber-go/atomic
go get -v github.com/uber-go/multierr
go get -uv github.com/natefinch/lumberjack
如果安装失败,就将我GitHub上的安装包go.uber.org解压后拷贝到 GOPATH/src 下,整个测试用例也在github上
GitHub地址
https://github.com/taw19960426/learning-go-language/tree/main/uber-go
在这里插入代码片
2 参考博客
这里面写得很详细
https://blog.csdn.net/wohu1104/article/details/107326794
3 结果展示

4 源代码
main.go
package main
import (
"time"
"go.uber.org/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var logger *zap.Logger
// logpath 日志文件路径
// loglevel 日志级别
func InitLogger(logpath string, loglevel string) {
// 日志分割
hook := lumberjack.Logger{
Filename: logpath, // 日志文件路径,默认 os.TempDir()
MaxSize: 1, // 每个日志文件保存1M,默认 100M
MaxBackups: 30, // 保留30个备份,默认不限
MaxAge: 7, // 保留7天,默认不限
Compress: true, // 是否压缩,默认不压缩
}
write := zapcore.AddSync(&hook)
// 设置日志级别
// debug 可以打印出 info debug warn
// info 级别可以打印 warn info
// warn 只能打印 warn
// debug->info->warn->error
var level zapcore.Level
switch loglevel {
case "debug":
level = zap.DebugLevel
case "info":
level = zap.InfoLevel
case "error":
level = zap.ErrorLevel
default:
level = zap.InfoLevel
}
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,
}
// 设置日志级别
atomicLevel := zap.NewAtomicLevel()
atomicLevel.SetLevel(level)
core := zapcore.NewCore(
// zapcore.NewConsoleEncoder(encoderConfig),
zapcore.NewJSONEncoder(encoderConfig),
// zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(&write)), // 打印到控制台和文件
write,
level,
)
// 开启开发模式,堆栈跟踪
caller := zap.AddCaller()
// 开启文件及行号
development := zap.Development()
// 设置初始化字段,如:添加一个服务器名称
filed := zap.Fields(zap.String("serviceName", "192.168.1.199"))
// 构造日志 如果不需要一些参数可以删除
logger = zap.New(core, caller, development, filed)
//logger = zap.New(core, development)
logger.Info("DefaultLogger init success")
}
func main() {
// 历史记录日志名字为:my.log,服务重新启动,日志会追加,不会删除
InitLogger("./logs/my.log", "debug")
// 强结构形式
logger.Info("test",
zap.String("string", "xiaotang"),
zap.Int("int", 3),
zap.Duration("time", time.Second),
)
// // 必须 key-value 结构形式 性能下降一点
// logger.Sugar().Infow("test-",
// "string", "kk",
// "int", 1,
// "time", time.Second,
// )
logger.Error("test02",
zap.String("string", "x666g"),
zap.Int("int", 4),
zap.Duration("time", time.Second),
)
for {
logger.Error("test02",
zap.String("string", "x666g"),
zap.Int("int", 4),
zap.Duration("time", time.Second),
)
}
}
版权声明
本文为[唐维康]所创,转载请带上原文链接,感谢
https://blog.csdn.net/taw19960426/article/details/124275363
边栏推荐
- Shortcut keys for batch modification of variable name and batch replacement code in idea
- Implementation and application of STM32 system and custom bootloader
- Fuzzy query between two tables of SQL server and steps of importing Excel data into SQL Server
- Supplément à la fonction d'annotation
- Understanding of data warehouse ODS, DW and DM concepts in data governance platform
- Axure产品原型工具使用笔记
- asp. Net JS to realize dynamic addition and file upload
- PHP determines whether it is the same month
- 联合类型和类型保护
- 为什么mysql里面设置符合主键它显示我错了?
猜你喜欢

【牛客刷题18】 查找两个字符串a,b中的最长公共子串

IIC bus design ① - IIC communication protocol

Introduction to btstack 1.0 of Bluetooth open source protocol stack

MongoDB 实验——数据备份和恢复和数据库优化

The fifth stop is the hometown of Confucius and Mencius ------- the shortest path to the maze

动态规划--LC474.一和零

数学实验——函数绘图实验

云原生KubeSphere实战多租户系统实战

leetcode题目--386.字典序排数,DFS

【第31题】创造两只完全相同的宠物
随机推荐
Golang learning resource list
Introduction to zephyr 1.0 of Internet of things operating system zephyr (Introduction)
PHP infinite classification (recursive)
Files and Directories
Record the problems and solutions encountered in using fastjson message converter
Zephyr IOT operating system column summary
[image fusion] image fusion based on curvelet transform (evaluation index), including Matlab source code
Time and Duration and Epoch
Bluetooth profile specification (AVRCP chapter) 5.1 connection and release of vctp
Oracle-SQL脚本记录
【知行】浅谈Switch与If
Flutter 基础组件的使用
使用Hutool的流方式读取Excel大文件
Assembly language -- Method of memory location
【牛客刷题18】 查找两个字符串a,b中的最长公共子串
Laravel print SQL
Leetcode title -- 386 Dictionary rank, DFS
路由器设备选型参照天梯
asp. Net JS to realize dynamic addition and file upload
云原生KubeSphere实战多租户系统实战