In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to create a Zap out-of-the-box log component, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Logrus is an excellent logging framework for golang, and its advantages are obvious:
Elegant code framework design can be used as a reference for our design components. For more information, please see my previous article (given at the end of the link article). Flexible output mode using simple component development ideas.
However, after all, performance is the Achilles heel of logrus. The previous article deeply studied the reasons for the low performance of logrus.
At present, the public choice of golang log database is mainly concentrated in: logrus, zap, zerolog. Both zap and zerolog have excellent performance, but I prefer zap in terms of usage.
Briefly introduce the use of Zap
Zap provides three different ways of output (take Info as an example)
Log.Info ("hello zap") / / {"level": "info", "ts": 1576423173.016333, "caller": "test_zap/main.go:28", "msg": "hello zap"}
Log.Infof ("hello% s", "zap") / / {"level": "info", "ts": 1576423203.056074, "caller": "test_zap/main.go:29", "msg": "hello zap"}
Log.Infow ("hello zap", "field1", "value1") / / {"level": "info", "ts": 1576423203.0560799, "caller": "test_zap/main.go:30", "msg": "hello zap", "field1": "value1"}
If we care about logrus's key-value theory, it can be solved perfectly by using zap infow.
Where Zap is inconvenient to use, Zap cannot be used out of the box like logrus. Users need to assemble the related functions themselves. Zap also does not provide the function of log cutting, but adding this function is not as convenient as logrus.
Based on these problems, I encapsulated a set of out-of-the-box logging components: https://github.com/georgehao/log
Create Zap out-of-the-box log components
Features provided:
Like logrus, global Debug, Info... Function log segmentation function. Default file size 1024m, automatic compression, up to 3 file backups, backup save time 7 days, will not print log called file name and location log will be divided into five types of files by default: xxx.log.DEBUG,xxx.log.INFO, xxx.log.WARN, xxx.log.ERROR, xxx.log.Request. Error, panic. Fatal will print the frame diagram where xxx.log.ERROR. Xxx.log.Request outputs the request log (if necessary)
Log usage go get github.com/georgehao/log example package main
Import "github.com/georgehao/log"
Func main () {
/ / init log
/ / set absolute path, and level
/ / set output level
/ / don't need request log
/ / set log's caller using logOption
Log.Init (". / test.log", log.DebugLevel, false, log.SetCaller (true))
Log.Info ("hello george log")
/ / flush
Log.Sync ()
/ output: {"level": "info", "ts": "2019-12-16T10:37:11.364+0800", "caller": "example/example.go:12", "msg": "hello george log"}
} the above content is how to build Zap out-of-the-box log components. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.