当前位置:网站首页>Golang logging library zerolog use record
Golang logging library zerolog use record
2022-07-30 19:02:00 【geekqian】
效果
安装
github官方地址:https://github.com/rs/zerolog
go get -u github.com/rs/zerolog/log
配置
Share what you use,If you have special needs, please take a look at the official documentation,更全面.
需求是: Output the log to both the console and the log file,The log file is split daily by date.
建了logPackages are individually packaged.log.go
package log
import (
"fmt"
"github.com/rs/zerolog"
"os"
"strings"
"time"
)
var Logger zerolog.Logger
func init() {
timeFormat := "2006-01-02 15:04:05"
zerolog.TimeFieldFormat = timeFormat
// 创建log目录
logDir := "./run_log/"
err := os.MkdirAll(logDir, os.ModePerm)
if err != nil {
fmt.Println("Mkdir failed, err:", err)
return
}
fileName := logDir + time.Now().Format("2006-01-02") + ".log"
logFile, _ := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
consoleWriter := zerolog.ConsoleWriter{
Out: os.Stdout, TimeFormat: timeFormat}
consoleWriter.FormatLevel = func(i interface{
}) string {
return strings.ToUpper(fmt.Sprintf("| %-6s|", i))
}
consoleWriter.FormatMessage = func(i interface{
}) string {
return fmt.Sprintf("%s", i)
}
consoleWriter.FormatFieldName = func(i interface{
}) string {
return fmt.Sprintf("%s:", i)
}
consoleWriter.FormatFieldValue = func(i interface{
}) string {
return fmt.Sprintf("%s;", i)
}
multi := zerolog.MultiLevelWriter(consoleWriter, logFile)
Logger = zerolog.New(multi).With().Timestamp().Logger()
}
使用
When using it, it is imported under its own projectlogpackage and use what's in the package Logger
输出.
import "myproject/log"
func SignIn(){
log.Logger.Info().
Str("website", "xx").
Str("account", account).
Msg("开始登录...")
}
边栏推荐
- 【Qt Designer工具的使用】
- OneFlow source code analysis: Op, Kernel and interpreter
- SimpleOSS第三方库libcurl与引擎libcurl错误解决方法
- node封装一个控制台进度条插件
- SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)
- 自然语言处理nltk
- 深入浅出边缘云 | 3. 资源配置
- 生物医学论文有何价值 论文中译英怎样翻译效果好
- Critical Reviews | 南农邹建文组综述全球农田土壤抗生素与耐药基因分布
- 中集世联达工业级成熟航运港口人工智能AI产品规模化应用,打造新一代高效能智慧港口和创新数字港口,全球港航人工智能能领军者中集飞瞳
猜你喜欢
Recommendation | People who are kind to you, don't repay them by inviting them to eat
延时队列优化 (2)
固定资产可视化智能管理系统
OneFlow source code analysis: Op, Kernel and interpreter
生物医学论文有何价值 论文中译英怎样翻译效果好
SimpleOSS第三方库libcurl与引擎libcurl错误解决方法
After 23 years of operation, the former "China's largest e-commerce website" has turned yellow...
Meta元宇宙部门第二季度亏损28亿!仍要继续押注?元宇宙发展尚未看到出路!
MongoDB打破了原则引入SQL?
Deepen school-enterprise cooperation and build an "overpass" for the growth of technical and skilled talents
随机推荐
微信小程序云开发 | 城市信息管理
Node encapsulates a console progress bar plugin
AI基础:图解Transformer
VBA 连接Access数据库和Excle
【Prometheus】Prometheus联邦的一次优化记录[续]
JsonUtil基于字符串操作josn
【网站放大镜效果】两种方式实现
Object和Map的区别
Spark学习:编译Spark项目时遇到的报错
DM8: Single database and single instance to build a local data guard service
基于inquirer封装一个控制台文件选择器
牛客刷题系列之进阶版(组队竞赛,排序子序列,倒置字符串, 删除公共字符,修理牧场)
荐号 | 对你有恩的人,不要请吃饭来报答
电脑死机的时候,发生了什么?
浅聊对比学习(Contrastive Learning)第一弹
【Swords Offer】Swords Offer 17. Print n digits from 1 to the largest
NXP IMX8QXP更换DDR型号操作流程
Pytorch foundation -- tensorboard use (1)
[Summary] 1396- 60+ VSCode plugins to create a useful editor
开心的聚餐