当前位置:网站首页>Logrus set log format and output function name
Logrus set log format and output function name
2022-04-23 05:04:00 【canger_】
Reference resources :https://juejin.cn/post/6844904061393698823
Normal log settings
func InitLogger() {
level := GetLogLevel(viper.GetString("LOG_LEVEL"))
log.SetLevel(level)
formatter := &log.TextFormatter{
ForceColors: true,// Set the color
FullTimestamp: true,
DisableQuote: true,
TimestampFormat: "2006-01-02 15:04:05",
}
log.SetFormatter(formatter)
log.Debug("debug log level")
log.Info("start")
}
Output of function name and file name
import (
log "github.com/sirupsen/logrus"
)
func loggerInit() {
// Initialize log
log.SetReportCaller(true) // You need to set this to true
log.SetFormatter(&log.TextFormatter{
TimestampFormat: "2006-01-02 15:03:04",
ForceColors: true,
FullTimestamp: true,
DisableQuote: true,
CallerPrettyfier: func(frame *runtime.Frame) (function string, file string) {
// Handle function name
fs := strings.Split(frame.Function, ".")
fun := ""
if len(fs) > 0 {
fun = fs[len(fs)-1]
}
fileName := path.Base(frame.File)
return fmt.Sprintf("[\033[1;34m%s\033[0m]", fun), fmt.Sprintf("[%s:%d]", fileName, frame.Line)
},
})
}
Third party log plugin
Install first
$ go get github.com/antonfisher/nested-logrus-formatter
Then call
package main
import (
nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/sirupsen/logrus"
)
func main() {
logrus.SetFormatter(&nested.Formatter{
HideKeys: true,
TimestampFormat: "2006-01-02 15:04:05",
FieldsOrder: []string{
"name", "age"},// You can configure the output order of fields
})
logrus.Info("info msg")
}
版权声明
本文为[canger_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220552063821.html
边栏推荐
- Sword finger offer: the path with a certain value in the binary tree (backtracking)
- scp命令详解
- View, modify and delete [database] table
- leetcode——启发式搜索
- Leetcode -- heuristic search
- vscode ipynb文件没有代码高亮和代码补全解决方法
- How can continuous integration (CI) / continuous delivery (CD) revolutionize automated testing
- L2-011 play binary tree (build tree + BFS)
- Deep learning notes - fine tuning
- [winui3] write an imitation Explorer file manager
猜你喜欢
Wechat payment function
JS engine loop mechanism: synchronous, asynchronous, event loop
Detailed explanation of the differences between TCP and UDP
Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)
Independent station operation | Facebook marketing artifact - chat robot manychat
DIY 一个 Excel 版的子网计算器
2022/4/22
Wine (COM) - basic concept
【数据库】MySQL基本操作(基操~)
泰克示波器DPO3054自校准SPC失败维修
随机推荐
和谐宿舍(线性dp / 区间dp)
Basic concepts of multithreading (concurrency and parallelism, threads and processes) and entry cases
Define defines constants and macros, pointers and structures
Analysis of POM files
Chapter II project scope management of information system project manager summary
In aggregated query without group by, expression 1 of select list contains nonaggregated column
JS engine loop mechanism: synchronous, asynchronous, event loop
Chapter III project schedule management of information system project manager summary
MySQL slow query
A trinomial expression that causes a null pointer
C list field sorting contains numbers and characters
Docker installation and mysql5 7 installation
Machine learning - linear regression
HRegionServer的详解
TypeError: ‘Collection‘ object is not callable. If you meant to call the ......
Luogu p2731 horse riding fence repair
Sword finger offer: symmetric binary tree (recursive iteration leetcode 101)
Innovation training (IX) integration
Leetcode 1547: minimum cost of cutting sticks
redis数据类型有哪些