Golang Logging Tool Revealed: One article to understand common log libraries, you need specific code examples
Introduction:
In the software development process, Logging is a very important job. Through logging, we can track the running status of the program, troubleshoot errors and debug code. In Golang, there are many excellent logging tools to choose from. This article will introduce several common Golang logging libraries, including log package, logrus, zap and zerolog, and provide specific code examples to help readers better understand and use these logging libraries.
1. Log package
The log package in the Go language standard library is Golang’s own logging tool, which is very simple and easy to use. The following is a sample code using the log package:
package main import ( "log" "os" ) func main() { // 设置日志输出到标准输出和日志文件 logFile, err := os.Create("app.log") if err != nil { log.Fatalln("Unable to create log file:", err) } defer logFile.Close() log.SetOutput(io.MultiWriter(os.Stdout, logFile)) // 记录日志信息 log.Println("This is a log message.") log.Printf("This is a log message with arguments: %s, %d", "hello", 123) }
In the above code, we first create a log file through the os.Create function, and then use io.MultiWriter
to direct the log output separately Standard output and log files. Finally, two log messages were recorded using the log.Println and log.Printf functions.
2. logrus
logrus is a very popular logging library in Golang, providing rich functions and flexible configuration options. The following is a sample code using logrus:
package main import ( "os" "github.com/sirupsen/logrus" ) func main() { // 创建一个新的日志记录器 logger := logrus.New() // 将日志输出到标准输出和日志文件 logFile, err := os.Create("app.log") if err != nil { logger.Fatal("Unable to create log file:", err) } defer logFile.Close() logger.SetOutput(io.MultiWriter(os.Stdout, logFile)) // 设置日志级别为Debug logger.SetLevel(logrus.DebugLevel) // 记录日志信息 logger.Debug("This is a debug log message.") logger.Infof("This is an info log message with arguments: %s, %d", "hello", 123) }
In the above code, we first create a new logger through the logrus.New
function. Then, use io.MultiWriter
to direct the log output to the standard output and log files respectively. Next, we set the log level to Debug, and finally used the logger.Debug and logger.Infof functions to record two log messages.
3. zap
zap is a high-performance log library open sourced by Uber. It has a simple design and is suitable for high-concurrency scenarios. The following is a sample code using zap:
package main import ( "os" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) func main() { // 创建一个新的日志记录器 logger, err := zap.NewProduction() if err != nil { panic(err) } defer logger.Sync() // 将日志输出到标准输出和日志文件 logFile, err := os.Create("app.log") if err != nil { panic(err) } defer logFile.Close() logger = logger.WithOptions(zap.WrapCore(func(c zapcore.Core) zapcore.Core { return zapcore.NewMultiWriteCore(c, zapcore.AddSync(logFile)) })) // 记录日志信息 logger.Debug("This is a debug log message.") logger.Info("This is an info log message with arguments.", zap.String("arg1", "hello"), zap.Int("arg2", 123)) }
In the above code, we first create a new logger through the zap.NewProduction
function. Then, use zapcore.NewMultiWriteCore
through the zap.WithOptions
function to direct the log output to the standard output and log files respectively. Finally, two log messages were recorded using the logger.Debug and logger.Info functions.
4. Zerolog
Zerolog is another extremely high-performance Golang log library with a simple API and flexible configuration options. The following is a sample code using zerolog:
package main import ( "os" "github.com/rs/zerolog" ) func main() { // 创建一个新的日志记录器 logger := zerolog.New(os.Stdout).With().Timestamp().Logger() // 将日志输出到标准输出和日志文件 logFile, err := os.Create("app.log") if err != nil { panic(err) } defer logFile.Close() logger = logger.Output(zerolog.MultiLevelWriter(os.Stdout, logFile)) // 记录日志信息 logger.Debug().Msg("This is a debug log message.") logger.Info().Str("arg1", "hello").Int("arg2", 123).Msg("This is an info log message with arguments.") }
In the above code, we first create a new logger through the zerolog.New
function, and pass the logger.Output function Output is directed to standard output and log files respectively. Finally, two log messages were recorded using the logger.Debug and logger.Info functions.
Summary:
This article introduces several common Golang log libraries, including log package, logrus, zap and zerolog, and provides specific code examples. By learning and practicing these logging libraries, we can better record the running status of the program, troubleshoot errors, and debug code. Choosing a log library that suits your needs and learning to rationally use the functions provided by each log library will greatly improve our efficiency and debugging capabilities in software development. I hope that readers can have a deeper understanding of the Golang log library through the introduction and examples of this article, and can better apply it to actual projects.
The above is the detailed content of Demystifying Golang's common logging libraries: Understanding logging tools. For more information, please follow other related articles on the PHP Chinese website!