当前位置:网站首页>Zap grammar sugar
Zap grammar sugar
2022-06-22 02:21:00 【Water is sleeping ice】
zap Log encapsulation
package main
import (
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var logger *zap.Logger
// logpath Log file path
// loglevel The level of logging
func InitLogger(logpath string, loglevel string) {
// Log segmentation
hook := lumberjack.Logger{
Filename: logpath, // Log file path , Default os.TempDir()
MaxSize: 10, // Each log file is saved 10M, Default 100M
MaxBackups: 30, // Retain 30 Backup , The default is unlimited
MaxAge: 7, // Retain 7 God , The default is unlimited
Compress: true, // Is it compressed? , No compression by default
}
write := zapcore.AddSync(&hook)
// Set the log level
// debug You can print it out info debug warn
// info Levels can be printed warn info
// warn Only print 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, // Lowercase encoder
EncodeTime: zapcore.ISO8601TimeEncoder, // ISO8601 UTC Time format
EncodeDuration: zapcore.SecondsDurationEncoder, //
EncodeCaller: zapcore.FullCallerEncoder, // Full path encoder
EncodeName: zapcore.FullNameEncoder,
}
// Set the log level
atomicLevel := zap.NewAtomicLevel()
atomicLevel.SetLevel(level)
core := zapcore.NewCore(
zapcore.NewConsoleEncoder(encoderConfig),
//zapcore.NewJSONEncoder(encoderConfig),
// zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(&write)), // Print to console and file
write,
level,
)
// Open development mode , stack trace
caller := zap.AddCaller()
// Open file and line number
development := zap.Development()
// Set initialization field , Such as : Add a server name
filed := zap.Fields()
// Build log
logger = zap.New(core, caller, development, filed)
logger.Info("DefaultLogger init success")
}
func main() {
// The name of the history log is :all.log, Service restart , The log will be appended , Does not delete
InitLogger("/var/log/pingan-operator-manager/all.log", "debug")
// Strong structural form
//logger.Info("test",
// zap.String("string", "string"),
// zap.Int("int", 3),
// zap.Duration("time", time.Second),
//)
must key-value Structure form Performance drops a little
//logger.Sugar().Infow("test-",
// "string", "string",
// "int", 1,
// "time", time.Second,
//)
logger.Info("vvvvvvvvvvvvvvvvvvvvvvvvvvvv1")
logger.Info("vvvvvvvvvvvvvvvvvvvvvvvvvvvv2")
logger.Info("vvvvvvvvvvvvvvvvvvvvvvvvvvvv3")
}
边栏推荐
- What is a neural network
- On Monday, I asked the meaning of the | -leaf attribute?
- Wechat applet film and television review and exchange platform system graduation design completion (8) graduation design thesis template
- 理财产品赎回确认日是什么意思?
- C # judge whether the application is started and displayed
- Excel Common shortcut keys Excel shortcut keys Summary
- PostgreSQL fetches data according to the size of the time field
- Chapter 09 English printed character recognition based on feature matching matlab deep learning practical case
- [chapter 06 MATLAB realizes lung cancer diagnosis based on watershed segmentation]
- Leetcode 41 - 45 dynamic planning topic
猜你喜欢

微信小程序影視評論交流平臺系統畢業設計畢設(4)開題報告

Efficient packet processing system based on dpdk

微信小程序影视评论交流平台系统毕业设计毕设(3)后台功能

Excel Common shortcut keys Excel shortcut keys Summary

EMC整改小技巧

Matlab learning notes (5) import and export of MATLAB data
![[chapter 07 face QR code recognition based on principal component analysis matlab deep learning practical case]](/img/cd/65a541e8d0fc7a49ee3ef1d266790a.png)
[chapter 07 face QR code recognition based on principal component analysis matlab deep learning practical case]
![[chapter 01 image defogging technology based on histogram optimization - full system matlab intelligent driving depth learning]](/img/ba/a63bd93812cabef82a187a361d8487.png)
[chapter 01 image defogging technology based on histogram optimization - full system matlab intelligent driving depth learning]

1277_ Implementation analysis of vtaskdelay in FreeRTOS

Atguigu---- list rendering
随机推荐
1277_ Implementation analysis of vtaskdelay in FreeRTOS
创建rt_thread线程
新员工入职,了解工作信息
How to obtain the comment information of tables and columns in gbase8a database?
In the era of industrial Internet, there is no real center
word文档转markdown文档?
[chapter 01 image defogging technology based on histogram optimization - full system matlab intelligent driving depth learning]
Games-101-personal summary shading
Wechat applet Film & TV Review Exchange Platform System Graduation Design (4) Rapport d'ouverture
使用 OKR 进行 HR 数字化转型
快速学会CAD绘制传输线路图纸
Development of power plant compliance test system with LabVIEW
Chapter 25 digital watermarking technology based on Wavelet Transform
postgresql根据时间字段的大小来取数
rt_thread线程管理
Ansible 配置文件
微信小程序影视评论交流平台系统毕业设计毕设(4)开题报告
Chapter 21 design of pavement crack detection and identification system -- matlab deep learning practice
微信小程序影视评论交流平台系统毕业设计毕设(1)开发概要
Matlab learning notes (4) matlab array