当前位置:网站首页>golang日志库zerolog使用记录
golang日志库zerolog使用记录
2022-07-30 18:58:00 【geekqian】
效果
安装
github官方地址:https://github.com/rs/zerolog
go get -u github.com/rs/zerolog/log
配置
分享下自己用的,有特殊需求的自己看下官方文档,更全面。
需求是: 把日志同时往控制台和日志文件里输出,日志文件用日期每日分拆。
建了log包单独封装。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()
}
使用
使用时引入自己项目下的log包并且用包里的 Logger
输出。
import "myproject/log"
func SignIn(){
log.Logger.Info().
Str("website", "xx").
Str("account", account).
Msg("开始登录...")
}
边栏推荐
猜你喜欢
深化校企合作 搭建技术技能人才成长“立交桥”
运营 23 年,昔日“国内第一大电商网站”黄了...
Mysql execution principle analysis
"Ruffian Heng Embedded Bimonthly" Issue 59
自然语言处理nltk
Basic use of scrapy
Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
Node encapsulates a console progress bar plugin
What is the value of biomedical papers? How to translate the papers into Chinese and English?
vxe-table实现复选框鼠标拖动选中
随机推荐
MYSQL (Basic) - An article takes you into the wonderful world of MYSQL
Fixed asset visualization intelligent management system
6块钱1斤,日本公司为何来中国收烟头?
部分分类网络性能对比
Spark学习:用spark实现ETL
What is the value of biomedical papers? How to translate the papers into Chinese and English?
Critical Reviews | 南农邹建文组综述全球农田土壤抗生素与耐药基因分布
你好,我的新名字叫“铜锁/Tongsuo”
【网站放大镜效果】两种方式实现
Scrapy framework is introduced
【Prometheus】Prometheus联邦的一次优化记录[续]
[Prometheus] An optimization record of the Prometheus federation [continued]
实体中增加操作方法
生物医学论文有何价值 论文中译英怎样翻译效果好
博纳影通过IPO注册:阿里腾讯是股东 受疫情冲击明显
Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
mysql的多实例
MySQL数据类型
Codeblocks + Widgets 创建窗口代码分析
Multiple instances of mysql