当前位置:网站首页>zap语法糖
zap语法糖
2022-06-22 02:11:00 【水是睡着的冰】
zap日志封装
package main
import (
"github.com/natefinch/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: 10, // 每个日志文件保存10M,默认 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()
// 构造日志
logger = zap.New(core, caller, development, filed)
logger.Info("DefaultLogger init success")
}
func main() {
// 历史记录日志名字为:all.log,服务重新启动,日志会追加,不会删除
InitLogger("/var/log/pingan-operator-manager/all.log", "debug")
// 强结构形式
//logger.Info("test",
// zap.String("string", "string"),
// zap.Int("int", 3),
// zap.Duration("time", time.Second),
//)
必须 key-value 结构形式 性能下降一点
//logger.Sugar().Infow("test-",
// "string", "string",
// "int", 1,
// "time", time.Second,
//)
logger.Info("vvvvvvvvvvvvvvvvvvvvvvvvvvvv1")
logger.Info("vvvvvvvvvvvvvvvvvvvvvvvvvvvv2")
logger.Info("vvvvvvvvvvvvvvvvvvvvvvvvvvvv3")
}
边栏推荐
- rt_thread的消息队列
- Qt程序怎么实现选中ListWidget中的某一行为默认选中
- 机器学习编译第1讲:机器学习编译概述
- Leetcode 513 find the value in the lower left corner of the tree [bfs binary tree] the leetcode path of heroding
- cmake常用命令分类备忘
- Ansible 配置文件
- LabVIEW开发发电厂合规性测试系统
- Games-101-personal summary shading
- What is your understanding of interface testing?
- 众昂矿业:萤石稀缺资源属性增强,未来或出现供需缺口
猜你喜欢

idea----bookmark

idea----bookmark
![[phantom engine UE] package error appears! Solutions to findpin errors](/img/d5/6747e20da6a8a4ca461094bd27bbf0.png)
[phantom engine UE] package error appears! Solutions to findpin errors

众昂矿业:萤石稀缺资源属性增强,未来或出现供需缺口

Lianfa science and technology -- Introduction to Lianfa science and technology ++ attached

Mysql database easy learning 06 - commonly used by data analysts: single table query of data query language DQL

Pdf to word PDF to picture picture to PDF modifying PDF files is as convenient as operating word (Introduction to acrobat DC)

excel常用快捷鍵excel快捷鍵匯總

Mba-day24 best value problem

优秀的 Verilog/FPGA开源项目介绍(二十七)- 小型CPU
随机推荐
Array common methods
光照相关 shader
Review of mathematical knowledge: triple integral
新员工入职,了解工作信息
[Chapter 17 corner feature detection based on Harris -- actual combat of MATLAB machine learning project]
idea----bookmark
微信小程序影视评论交流平台系统毕业设计毕设(3)后台功能
Mysql database easy learning 09 - commonly used by data analysts: multi table query of data query language DQL
Common shortcut keys in Excel summary of shortcut keys in Excel
理财产品到期赎回时间是什么意思?
cmake常用命令分类备忘
atguigu----列表渲染
Leetcode 41 - 45 dynamic planning topic
基于DPDK的高效包处理系统
Test APK exception control WiFi scan attacker development
创建rt_thread线程
The "cloud" end of the 6th world intelligence conference will be held soon
Common shortcut keys in Excel summary of shortcut keys in Excel
Games-101 personal summary rasterization
es-object vs nested vs has_child and has_parent