当前位置:网站首页>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("开始登录...")
}
边栏推荐
- 阿里云武林头条活动分享
- DM8: Single database and single instance to build a local data guard service
- Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
- 基于inquirer封装一个控制台文件选择器
- 【Pointing to Offer】Pointing to Offer 22. The kth node from the bottom in the linked list
- redis
- The use of @ symbol in MySql
- 设计消息队列存储消息数据的 MySQL 表格
- requet.getHeader(“token“) 为null
- LeetCode 练习——关于查找数组元素之和的两道题
猜你喜欢
MySql中@符号的使用
深化校企合作 搭建技术技能人才成长“立交桥”
What is the value of biomedical papers? How to translate the papers into Chinese and English?
运营 23 年,昔日“国内第一大电商网站”黄了...
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
中集世联达工业级成熟航运港口人工智能AI产品规模化应用,打造新一代高效能智慧港口和创新数字港口,全球港航人工智能能领军者中集飞瞳
【剑指 Offer】剑指 Offer 22. 链表中倒数第k个节点
微信小程序云开发 | 城市信息管理
2种手绘风格效果比较,你更喜欢哪一种呢?
OneFlow源码解析:Op、Kernel与解释器
随机推荐
Tensorflow2.0 混淆矩阵与打印准确率不符
Codeblocks + Widgets 创建窗口代码分析
自己需要努力
经济新闻:错误# 15:初始化libiomp5md。dll,但发现libiomp5md。已经初始化dll。解决方法
vxe-table实现复选框鼠标拖动选中
NXP IMX8QXP replacement DDR model operation process
二分答案裸题(加一点鸽巢原理)
在华为云,见证迷你世界的神奇觉醒
中集世联达工业级成熟航运港口人工智能AI产品规模化应用,打造新一代高效能智慧港口和创新数字港口,全球港航人工智能能领军者中集飞瞳
电脑死机的时候,发生了什么?
Node encapsulates a console progress bar plugin
深入浅出边缘云 | 3. 资源配置
跨域问题的解决方法
[Use of Qt Designer tool]
LeetCode每日一题(1717. Maximum Score From Removing Substrings)
MySQL data types
Meta元宇宙部门第二季度亏损28亿!仍要继续押注?元宇宙发展尚未看到出路!
The Meta metaverse division lost 2.8 billion in the second quarter!Still want to keep betting?Metaverse development has yet to see a way out!
跨进程启动后台服务
第一次进入小程序判断