当前位置:网站首页>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("开始登录...")
}
边栏推荐
猜你喜欢
VBA批量将Excel数据导入Access数据库
生物医学论文有何价值 论文中译英怎样翻译效果好
中集世联达飞瞳全球工业人工智能AI领军者,全球顶尖AI核心技术高泛化性高鲁棒性稀疏样本持续学习,工业级高性能成熟AI产品规模应用
The large-scale application of artificial intelligence AI products in industrial-grade mature shipping ports of CIMC World Lianda will create a new generation of high-efficiency smart ports and innova
你好,我的新名字叫“铜锁/Tongsuo”
【PHPWord】PHPOffice 套件之PHPWord快速入门
沉浸式体验科大讯飞2022消博会“官方指定产品”
AI基础:图解Transformer
NXP IMX8QXP更换DDR型号操作流程
node封装一个控制台进度条插件
随机推荐
高精度加法
开心的聚餐
自己需要努力
谷歌AlphaFold近日宣称预测出地球上几乎所有蛋白质结构
【Qt Designer工具的使用】
Recommendation | People who are kind to you, don't repay them by inviting them to eat
6 yuan per catty, why do Japanese companies come to China to collect cigarette butts?
常见链表题及其 Go 实现
SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)
What kind of framework is friendly to developers?
"Ruffian Heng Embedded Bimonthly" Issue 59
NC | 西湖大学陶亮组-TMPRSS2“助攻”病毒感染并介导索氏梭菌出血毒素的宿主入侵...
【科普】无线电波怎样传送信息?
VBA批量将Excel数据导入Access数据库
WeChat Mini Program Cloud Development | Urban Information Management
(2022杭电多校四)1001-Link with Bracket Sequence II(区间动态规划)
vxe-table实现复选框鼠标拖动选中
Common linked list problems and their Go implementation
LeetCode Exercise - Two Questions About Finding Sum of Array Elements
golang日志库zerolog使用记录