Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Zap Log Library in Goto language Project

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to use Zap log library in GE language project". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Introduction to the use of Zap Log Library in Goto language Project

In many Go language projects, we need a good logger that provides the following features:

Events can be logged to a file instead of the application console.

Log cutting-can cut log files according to file size, time, interval, and so on.

Different log levels are supported. Such as INFO,DEBUG,ERROR and so on.

Be able to print basic information, such as call file / function name and line number, log time, etc.

Default Go Logger log library

Before introducing Uber-go 's zap package, let's take a look at the basic logging capabilities provided by the Go language. The default log package provided by the Go language is https://golang.org/pkg/log/.

Implement Go Logger

Implementing a logger in the Go language is simple-create a new log file and set it as the output location of the log.

Set up Logger

We can set up the logger like the following code

Func SetupLogger () {logFileLocation, _: = os.OpenFile ("/ Users/q1mi/test.log", os.O_CREATE | os.O_APPEND | os.O_RDWR, 0744) log.SetOutput (logFileLocation)} use Logger

Let's write some virtual code to use this logger.

In the current example, we will establish an HTTP connection to URL and log the status code / error to the log file.

Func simpleHttpGet (url string) {resp, err: = http.Get (url) if err! = nil {log.Printf ("Error fetching url% s:% s", url, err.Error ())} else {log.Printf ("Status Code for% s:% s", url, resp.Status) resp.Body.Close ()} Logger

Now let's execute the above code and see how the logger is running.

Func main () {SetupLogger () simpleHttpGet ("www.google.com") simpleHttpGet ("http://www.google.com")}"

When we execute the above code, we can see that a test.log file is created and the following contents are added to the log file.

2019-05-24 01:14:13 Error fetching url www.google.com: Get www.google.com: unsupported protocol scheme "" 2019-05-24 01:14:14 Status Code for http://www.google.com: 200OKGo Logger strengths and weaknesses

Its biggest advantage is that it is very easy to use. We can set any io.Writer as logging output and send it the log to write to.

Inferior position

Basic log level only

There is only one Print option. Multiple levels such as INFO/DEBUG are not supported.

For error logs, it has

Fatal

And

Panic

The Fatal log ends the program by calling os.Exit (1)

The Panic log throws a panic after the log message is written

But it lacks an ERROR log level that logs errors without throwing a panic or exiting the program

Lack the ability to format logs-such as recording the caller's function name and line number, formatting the date and time format. Wait.

The ability to cut logs is not provided.

Uber-go Zap log database

Zap is a very fast, structured, log-level Go log library.

Why choose Uber-go zap?

It provides both structured logging and printf-style logging

It's very fast.

According to Uber-go Zap documentation, its performance is better than similar structured log packages-and faster than standard libraries. The following is the benchmark information released by Zap

Log a message and 10 fields:

PackageTimeTime to zapObjects Allocated ⚡️ zap862 ns/op+0%5 allocs/op ⚡️ zap (sugared) 1250 ns/op+45%11 allocs/opzerolog4021 ns/op+366%76 allocs/opgo-kit4542 ns/op+427%105 allocs/opapex/log26785 ns/op+3007%115 allocs/oplogrus29501 ns/op+3322%125 allocs/oplog1529906 ns/op+3369%122 allocs/op

Record a static string without any context or printf-style template:

PackageTimeTime% to zapObjects Allocated ⚡️ zap118 ns/op+0%0 allocs/op ⚡️ zap (sugared) 191 ns/op+62%2 allocs/opzerolog93 ns/op-21%0 allocs/opgo-kit280 ns/op+137%11 allocs/opstandard library499 ns/op+323%2 allocs/opapex/log1990 ns/op+1586%10 allocs/oplogrus3129 ns/op+2552%24 allocs/oplog153887 ns/op+3194%23 allocs/op installation

Run the following command to install zap

Go get-u go.uber.org/zap configure Zap Logger

Zap provides two types of loggers-Sugared Logger and Logger.

Use SugaredLogger in a context where performance is good but not critical. It is 4-10 times faster than other structured logging packages and supports structured and printf-style logging.

Use Logger in a context where every microsecond and every memory allocation is important. It is even faster and has fewer memory allocations than SugaredLogger, but it only supports strongly typed structured logging.

Logger

Create a Logger by calling zap.NewProduction () / zap.NewDevelopment () or zap.Example ().

Each of the above functions creates a logger. The only difference is that it will record different information. For example, production logger records the calling function information, date, and time by default.

Call Info/Error and so on through Logger.

By default, logs are printed to the console interface of the application.

Var logger * zap.Loggerfunc main () {InitLogger () defer logger.Sync () simpleHttpGet ("www.google.com") simpleHttpGet ("http://www.google.com")}func InitLogger () {logger, _ = zap.NewProduction ()} func simpleHttpGet (url string) {resp) Err: = http.Get (url) if err! = nil {logger.Error ("Error fetching url..", zap.String ("url", url), zap.Error (err))} else {logger.Info ("Success..") Zap.String ("statusCode", resp.Status), zap.String ("url", url) resp.Body.Close ()}}

In the above code, we first create a Logger and then log the message using Logger methods such as Info/ Error.

The syntax of the logger method is as follows:

Func (log * Logger) MethodXXX (msg string, fields... Field)

Where MethodXXX is a variable parameter function, which can be Info / Error/ Debug / Panic and so on. Each method accepts a message string and any number of zapcore.Field field parameters.

Each zapcore.Field is actually a set of key-value pair parameters.

When we execute the above code, we get the following output:

{"level": "error", "ts": 1572159218.912792, "caller": "zap_demo/temp.go:25", "msg": "Error fetching url..", "url": "www.sogo.com", "error": "Get www.sogo.com: unsupported protocol scheme\"\ "" "stacktrace": "main.simpleHttpGet\ n\ t/Users/q1mi/zap_demo/temp.go:25\ nmain.main\ n\ t/Users/q1mi/zap_demo/temp.go:14\ nruntime.main\ n\ t/usr/local/go/src/runtime/proc.go:203"} {"level": "info", "ts": 1572159219.1227388, "caller": "zap_demo/temp.go:30", "msg": "Success..", "statusCode": "200 OK" "url": "http://www.sogo.com"}Sugared Logger

Now let's use Sugared Logger to do the same thing.

Most of the implementations are basically the same.

The only difference is that we call the main logger's. The Sugar () method to get a SugaredLogger.

Then use SugaredLogger to record the statement in printf format

Here is the modified code to use SugaredLogger instead of Logger:

Var sugarLogger * zap.SugaredLoggerfunc main () {InitLogger () defer sugarLogger.Sync () simpleHttpGet ("www.google.com") simpleHttpGet ("http://www.google.com")}func InitLogger () {logger, _: = zap.NewProduction () sugarLogger = logger.Sugar ()} func simpleHttpGet (url string) {sugarLogger.Debugf (" Trying to hit GET request for% s ", url) resp Err: = http.Get (url) if err! = nil {sugarLogger.Errorf ("Error fetching URL% s: Error =% s", url, err)} else {sugarLogger.Infof ("Success! StatusCode =% s for URL% s ", resp.Status, url) resp.Body.Close ()}}

When you execute the above code, you will get the following output:

{"level": "error", "ts": 1572159149.923002, "caller": "logic/temp2.go:27", "msg": "Error fetching URL www.sogo.com: Error = Get www.sogo.com: unsupported protocol scheme\"\ "" "stacktrace": "main.simpleHttpGet\ n\ t/Users/q1mi/zap_demo/logic/temp2.go:27\ nmain.main\ n\ t/Users/q1mi/zap_demo/logic/temp2.go:14\ nruntime.main\ n\ t/usr/local/go/src/runtime/proc.go:203"} {"level": "info", "ts": 1572159150.192585, "caller": "logic/temp2.go:29", "msg": "Success! StatusCode = 200 OK for URL http://www.sogo.com"}

You should have noticed that so far both logger have been printed out in JSON structure format.

Later in this blog, we will discuss SugaredLogger in more detail and learn how to configure it further.

Custom logger writes logs to a file instead of the terminal

The first change we need to make is to write the log to a file instead of printing it to the application console.

We will use zap.New (...) Method to pass all configurations manually instead of using preset methods such as zap.NewProduction () to create logger.

Func New (core zapcore.Core, options... Option) * Logger

Zapcore.Core requires three configurations-- Encoder,WriteSyncer,LogLevel.

1.Encoder: encoder (how to write to the log). We will use out-of-the-box NewJSONEncoder () and pre-set ProductionEncoderConfig ().

Zapcore.NewJSONEncoder (zap.NewProductionEncoderConfig ())

2.WriterSyncer: specify where the log will be written. We use the zapcore.AddSync () function and pass in the open file handle.

File, _: = os.Create (". / test.log") writeSyncer: = zapcore.AddSync (file)

3.Log Level: which level of log will be written.

We will modify the Logger code in the above section and override the InitLogger () method. The rest of the method-main () / SimpleHttpGet () remains the same.

Func InitLogger () {writeSyncer: = getLogWriter () encoder: = getEncoder () core: = zapcore.NewCore (encoder, writeSyncer, zapcore.DebugLevel) logger: = zap.New (core) sugarLogger = logger.Sugar ()} func getEncoder () zapcore.Encoder {return zapcore.NewJSONEncoder (zap.NewProductionEncoderConfig ())} func getLogWriter () zapcore.WriteSyncer {file _: = os.Create (". / test.log") return zapcore.AddSync (file)}

When the main () function of the above section is called using these modified logger configurations, the following output will be printed in the file-- test.log.

{"level": "debug", "ts": 1572160754.994731, "msg": "Trying to hit GET request for www.sogo.com"} {"level": "error", "ts": 1572160754.994982, "msg": "Error fetching URL www.sogo.com: Error = Get www.sogo.com: unsupported protocol scheme\"} {"level": "debug", "ts": 1572160754.994996, "msg": "Trying to hit GET request for http://www.sogo.com"}{"level":"info"," "ts": 1572160757.3755069, "msg": "Success! StatusCode = 200 OK for URL http://www.sogo.com"} change JSON Encoder to normal Log Encoder

Now we want to change the encoder from JSON Encoder to normal Encoder. To do this, we need to change NewJSONEncoder () to NewConsoleEncoder ().

Return zapcore.NewConsoleEncoder (zap.NewProductionEncoderConfig ())

When the main () function of the above section is called using these modified logger configurations, the following output will be printed in the file-- test.log.

1.572161051846623e+09 debug Trying to hit GET request for www.sogo.com1.572161051846828e+09 error Error fetching URL www.sogo.com: Error = Get www.sogo.com: unsupported protocol scheme "" 1.5721610518468401e+09 debug Trying to hit GET request for http://www.sogo.com1.572161052068744e+09 info Success! StatusCode = 200 OK for URL http://www.sogo.com change the time code and add caller details

Given the changes we have made to the configuration, there are two issues:

Time is displayed in non-human readable ways, such as 1.572161051846623e+09

The details of the caller's function are not shown in the log

The first thing we need to do is override the default ProductionConfig () and make the following changes:

Modification time encoder

Use uppercase letters to record log levels in log files

Func getEncoder () zapcore.Encoder {encoderConfig: = zap.NewProductionEncoderConfig () encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder return zapcore.NewConsoleEncoder (encoderConfig)}

Next, we will modify the zap logger code to add the ability to log the calling function information. To this end, we will be in zap.New (..) Add an Option to the function.

Logger: = zap.New (core, zap.AddCaller ())

When the main () function of the above section is called using these modified logger configurations, the following output will be printed in the file-- test.log.

2019-10-27T15:33:29.855+0800 DEBUG logic/temp2.go:47 Trying to hit GET request for www.sogo.com2019-10-27T15:33:29.855+0800 ERROR logic/temp2.go:50 Error fetching URL www.sogo.com: Error = Get www.sogo.com: unsupported protocol scheme "" 2019-10-27T15:33:29.856+0800 DEBUG logic/temp2.go:47 Trying to hit GET request for http://www.sogo. Com2019-10-27T15:33:30.125+0800 INFO logic/temp2.go:52 Success! StatusCode = 200 OK for URL http://www.sogo.com uses Lumberjack for log cutting and archiving

The only thing missing in this logger is the log cutting and archiving function.

Zap itself does not support slicing archive log files

In order to add the log cutting and archiving function, we will use the third-party library Lumberjack to implement it.

Installation

Execute the following command to install Lumberjack

Add Lumberjack to go get-u github.com/natefinch/lumberjackzap logger

To add Lumberjack support to zap, we need to modify the WriteSyncer code. We will modify the getLogWriter () function as follows:

Func getLogWriter () zapcore.WriteSyncer {lumberJackLogger: = & lumberjack.Logger {Filename: ". / test.log", MaxSize: 10, MaxBackups: 5, MaxAge: 30, Compress: false,} return zapcore.AddSync (lumberJackLogger)}

Lumberjack Logger takes the following attributes as input:

Filename: location of the log file

MaxSize: the maximum size of the log file (in MB) before cutting

MaxBackups: maximum number of old files to be retained

MaxAges: maximum number of days to keep old files

Compress: whether to compress / archive old files

Test all functions

Finally, the complete sample code for using Zap/Lumberjack logger is as follows:

Package mainimport ("net/http"github.com/natefinch/lumberjack"go.uber.org/zap"go.uber.org/zap/zapcore") var sugarLogger * zap.SugaredLoggerfunc main () {InitLogger () defer sugarLogger.Sync () simpleHttpGet ("www.sogo.com") simpleHttpGet ("http://www.sogo.com")}func InitLogger ()) {writeSyncer: = getLogWriter () encoder: = getEncoder () core: = zapcore.NewCore (encoder WriteSyncer, zapcore.DebugLevel) logger: = zap.New (core, zap.AddCaller ()) sugarLogger = logger.Sugar ()} func getEncoder () zapcore.Encoder {encoderConfig: = zap.NewProductionEncoderConfig () encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder return zapcore.NewConsoleEncoder (encoderConfig)} func getLogWriter () zapcore.WriteSyncer {lumberJackLogger: = & lumberjack.Logger {Filename: ". / test.log" MaxSize: 1, MaxBackups: 5, MaxAge: 30, Compress: false,} return zapcore.AddSync (lumberJackLogger)} func simpleHttpGet (url string) {sugarLogger.Debugf ("Trying to hit GET request for% s", url) resp Err: = http.Get (url) if err! = nil {sugarLogger.Errorf ("Error fetching URL% s: Error =% s", url, err)} else {sugarLogger.Infof ("Success! StatusCode =% s for URL% s ", resp.Status, url) resp.Body.Close ()}}

Execute the above code, and the following will be output to the file-- test.log.

2019-10-27T15:50:32.944+0800 DEBUG logic/temp2.go:48 Trying to hit GET request for www.sogo.com2019-10-27T15:50:32.944+0800 ERROR logic/temp2.go:51 Error fetching URL www.sogo.com: Error = Get www.sogo.com: unsupported protocol scheme "" 2019-10-27T15:50:32.944+0800 DEBUG logic/temp2.go:48 Trying to hit GET request for http://www.sogo.com2019- 10-27T15:50:33.165+0800 INFO logic/temp2.go:53 Success! StatusCode = 200 OK for URL http://www.sogo.com

At the same time, you can cycle through the log in the main function to test whether log files are automatically sliced and archived (log files are sliced per 1MB and a maximum of 5 backups are saved in the current directory).

This is the end of the content of "how to use the Zap log library in the Goto language project". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report