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

redis

ctf.show_web5
![[Prometheus] An optimization record of the Prometheus federation [continued]](/img/5d/56e171b7a02584337a0cfe5c731fb2.png)
[Prometheus] An optimization record of the Prometheus federation [continued]

SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)

VBA 连接Access数据库和Excle

Node encapsulates a console progress bar plugin

kotlin by lazy

kotlin的by lazy

Fixed asset visualization intelligent management system

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
随机推荐
requet.getHeader(“token“) 为null
Delay queue optimization (2)
Range.CopyFromRecordset 方法 (Excel)
尊重客观事实
WEBSOCKETPP使用简介+demo
第一次进入小程序判断
云数据库和本地数据库有什么区别?
Go 系统收集
Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
natural language processing nltk
(2022杭电多校四)1001-Link with Bracket Sequence II(区间动态规划)
第14章 类型信息
kotlin的by lazy
"Ruffian Heng Embedded Bimonthly" Issue 59
JS提升:Promise中reject与then之间的关系
2种手绘风格效果比较,你更喜欢哪一种呢?
Basic use of scrapy
DM8: Single database and single instance to build a local data guard service
Multiple instances of mysql
DM8:单库单实例搭建本地数据守护服务