Home >Common Problem >What are the usages of log in go language?
Go language log usage includes: 1. Output text information, such as using the "Println()" function in the log package to output text information; 2. Output the value of variables, such as using "Printf() in the log package )" function outputs the value of the variable; 3. Record error information, such as using the "Printf()" function in the log package to record error information; 4. Set the log prefix, such as using "SetPrefix()" in the log package to set the log prefix.
The operating environment of this tutorial: Windows 10 system, go1.20 version, Dell g3 computer.
Go language is a programming language developed by Google and is widely used in network programming, concurrent programming, cloud computing and other fields. In the Go language, using logs to record errors, information, and debugging information during program running is an important part. There are many log libraries in the Go language, commonly used ones include log, pkglog, zap, logrus, seelog, etc.
In the Go language, the log package is provided in the standard library, which can be used to record log information.
The following are some common usage methods:
1. Output text information
log.Println("hello world")
Execution results:
2022/01/20 21:06:34 hello world
Output Log information includes output time and information content, and you can easily check the time and content of each output.
2. Output the value of the variable
a := 10 log.Printf("a的值为:%d", a)
Execution result:
2022/01/20 21:09:25 a的值为:10
3. Record error message
if err != nil { log.Printf("发生了一个错误:%v", err) }
Execution result:
2022/01/20 21:10:09 发生了一个错误:open test.txt: no such file or directory
4. Set the log prefix
log.SetPrefix("[prefix]") log.Printf("hello world")
Execution result:
[prefix]2022/01/20 21:11:21 hello world
Setting the log prefix can easily distinguish different log information sources and facilitate future log analysis.
Expansion:
Set the log level
When recording logs, sometimes you only need to record some more important information , without recording all log information. Therefore, setting the log level is very necessary.
In the Go language, the log package is provided in the standard library, but it does not provide a function for setting the log level. Generally speaking, we can customize a log package and implement the function of setting the log level by encapsulating the functions in the log package.
The following is a simple implementation:
type LogLevel uint8 const ( DEBUG LogLevel = iota + 1 INFO WARN ERROR FATAL ) func Debug(v ...interface{}) { logPrint(DEBUG, v...) } func Info(v ...interface{}) { logPrint(INFO, v...) } func Warn(v ...interface{}) { logPrint(WARN, v...) } func Error(v ...interface{}) { logPrint(ERROR, v...) } func Fatal(v ...interface{}) { logPrint(FATAL, v...) } func logPrint(lv LogLevel, v ...interface{}) { if lv < LogLevel) { return } log.Println(v...) }
In the above code, we define a LogLevel type to represent the log level. By customizing the Debug, Info, Warn, Error, and Fatal functions, you can output log information when the log level is higher than the set level.
Output to file
If you output the log information directly to the terminal, although it is convenient, it is not convenient to view. Therefore, it is more common practice to output log information to a file.
The following is a simple implementation:
func logToFile() { file, err := os.Create("log.txt") if err != nil { fmt.Println("创建文件失败:", err) return } defer file.Close() log.SetOutput(file) log.Println("hello world") }
In the above code, we first create a file named log.txt and output the log information to the file. By setting log.SetOutput(file), log information can be output to the file.
Summary
In the Go language, using the log package to record errors, information, and debugging information during program running is an important part. There are many log libraries in the Go language, commonly used ones include log, pkglog, zap, logrus, seelog, etc.
When using the log package, you can record different types of log information through functions such as log.Printf, log.Println, log.SetPrefix, etc., and you can set the log level by customizing the log level. Log information can also be output to a file for easy viewing and analysis
The above is the detailed content of What are the usages of log in go language?. For more information, please follow other related articles on the PHP Chinese website!