当前位置:网站首页>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("开始登录...")
}
边栏推荐
猜你喜欢

6 yuan per catty, why do Japanese companies come to China to collect cigarette butts?

【PHPWord】Quick Start of PHPWord in PHPOffice Suite

SimpleOSS第三方库libcurl与引擎libcurl错误解决方法

Hello, my new name is "Bronze Lock/Tongsuo"

【PHPWord】PHPOffice 套件之PHPWord快速入门

【剑指 Offer】剑指 Offer 22. 链表中倒数第k个节点
![【Prometheus】Prometheus联邦的一次优化记录[续]](/img/5d/56e171b7a02584337a0cfe5c731fb2.png)
【Prometheus】Prometheus联邦的一次优化记录[续]

Mysql execution principle analysis

Scrapy framework is introduced

Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
随机推荐
2种手绘风格效果比较,你更喜欢哪一种呢?
kotlin by lazy
【Prometheus】Prometheus联邦的一次优化记录[续]
SimpleOSS第三方库libcurl与引擎libcurl错误解决方法
Listen to the boot broadcast
MySql中@符号的使用
第十七届“振兴杯”全国青年 职业技能大赛——计算机程序设计员(云计算平台与运维)参赛回顾与总结
线性筛求积性函数
Recommendation | People who are kind to you, don't repay them by inviting them to eat
VS Code 连接SQL Server
【刷题篇】计算质数
《痞子衡嵌入式半月刊》 第 59 期
7.30模拟赛总结
vxe-table实现复选框鼠标拖动选中
NC | Tao Liang Group of West Lake University - TMPRSS2 "assists" virus infection and mediates the host invasion of Clostridium sothrix hemorrhagic toxin...
跨进程启动后台服务
Swiper轮播图片并播放背景音乐
终端分屏工具Terminalx的使用
部分分类网络性能对比
Delay queue optimization (2)