当前位置:网站首页>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("开始登录...")
}
边栏推荐
- ctf.show_web5
- vxe-table实现复选框鼠标拖动选中
- AWS 控制台
- 固定资产可视化智能管理系统
- CCNA-网络汇总 超网(CIDR) 路由最长掩码匹配
- JsonUtil基于字符串操作josn
- Hello, my new name is "Bronze Lock/Tongsuo"
- Swiper rotates pictures and plays background music
- CIMC Shilian Dafeitong is the global industrial artificial intelligence AI leader, the world's top AI core technology, high generalization, high robustness, sparse sample continuous learning, industri
- 432.4 FPS 快STDC 2.84倍 | LPS-Net 结合内存、FLOPs、CUDA实现超快语义分割模型
猜你喜欢

VBA 运行时错误‘-2147217900(80040e14):自动化(Automation)错误

运营 23 年,昔日“国内第一大电商网站”黄了...

你好,我的新名字叫“铜锁/Tongsuo”

Swiper rotates pictures and plays background music

CCNA-网络汇总 超网(CIDR) 路由最长掩码匹配

nlohmann json 使用指南【visual studio 2022】

载誉而归,重磅发布!润和软件亮相2022开放原子全球开源峰会

C# wpf borderless window add shadow effect

cocos creater 热更重启导致崩溃

深入浅出边缘云 | 3. 资源配置
随机推荐
Pytorch foundation -- tensorboard use (1)
Codeblocks + Widgets create window code analysis
部分分类网络性能对比
MySQL data types
CCNA-NAT协议(理论与实验练习)
深入浅出边缘云 | 3. 资源配置
3D机器视觉厂商的场景争夺战役
防抖和节流有什么区别,分别用于什么场景?
【Qt Designer工具的使用】
NC | Tao Liang Group of West Lake University - TMPRSS2 "assists" virus infection and mediates the host invasion of Clostridium sothrix hemorrhagic toxin...
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
Anaconda Navigator stuck on loading applications
kotlin by lazy
"Ruffian Heng Embedded Bimonthly" Issue 59
C# wpf 无边框窗口添加阴影效果
深化校企合作 搭建技术技能人才成长“立交桥”
MySQL——基础知识
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
【Prometheus】Prometheus联邦的一次优化记录[续]
第14章 类型信息