Go languagelog
Package provides basic functions for logging. compared tofmt
Bag,log
The package adds log management functions such as timestamps and log levels, which are very suitable for debugging and recording operational information. The following is the rightlog
The detailed description of the package, and it withfmt
The difference between packages.
1. Basic functions of log package
log
The package contains a series of functions that can be used to print timestamped log information, and the logs are written to standard error output by default () and add a timestamp at the beginning of each log.
Common functions
: Basic log output function, similar to
, but timestamp will be added before output.
: Similar to
, a timestamp will be added before the output and a newline will be added at the end.
: Similar to
, supports formatting output and adds a timestamp before output.
Sample code:
package main import "log" func main() { ("This is a log message.") ("This is another log message.") ("Formatted log: %d + %d = %d", 2, 3, 2+3) }
2. Log level function of log package
log
The package also provides two special logging functions for handling errors and program exits:
: Used to record serious error information, it will be called after outputting the log
(1)
, terminate the program execution.
: Used to record error information and call
panic
, causing panic (program crashes and throws stack information), suitable for debugging and handling abnormal situations.
Sample code:
package main import "log" func main() { ("This is a normal log.") ("This is a fatal error log, program will exit.") // The program will exit after output ("This will not be executed because of Fatal above.") }
3. Customize the log output location
log
The package outputs the logs to the standard error stream by default (), but can be passed
Method customize output location. Logs can be output to files, networks, or other compliant
The target of the interface.
Example: Output logs to file
package main import ( "log" "os" ) func main() { file, err := ("") if err != nil { ("Failed to create log file:", err) } defer () (file) // Set log output to file ("This log will be written to file.") }
4. Customize log prefixes and tags
log
Packages allow custom log prefixes and formats to help identify the source or type of log. useMethods can set the prefix of the log, using
You can control the marking of the log.
Log tags
log
The package provides the following tags (usingSetFlags
set up):
-
: Date (yyyy/mm/dd).
-
: Time (hh:mm).
-
: Microsecond time.
-
: Complete file path and line number.
-
: Short file name and line number.
-
: UTC time.
-
: The default mark, equivalent to
|
。
Example: Custom log prefixes and tags
package main import "log" func main() { ("INFO: ") // Set log prefix ( | | ) // Set log mark ("This is a customized log message.") }
5. Create a custom logger
useYou can create a custom logger (Logger) and specify different output targets, prefixes, and tags. Multiple loggers can record different types of log information.
Example: Create two different loggers
package main import ( "log" "os" ) func main() { // Create an error logger and output to standard error errorLogger := (, "ERROR: ", ||) // Create an information logger and output it to a file infoFile, err := ("") if err != nil { (err) } defer () infoLogger := (infoFile, "INFO: ", |) ("This is an informational message.") ("This is an error message.") }
The difference between log package and fmt package
Different uses:
-
fmt
The package is mainly used to format input and output, suitable for outputting general information, and does not have log management functions. -
log
The package is specially designed for logging and provides logging functions, suitable for the output of important information, errors, debugging information, etc. in the program.
Different output content:
-
fmt
Package output has no timestamps and does not contain additional information by default. -
log
Package output contains timestamps and can be set with additional tags and prefixes.
Output location:
-
fmt
The output target of the package defaults to standard output (), can be passed
Directs the output to the specified destination.
-
log
The default output of the package is standard error () and can be passed
Easily modify the output target.
Log Level:
-
fmt
Packages do not have the concept of log level. -
log
The package provides log-level support (e.g.Fatal
andPanic
)。
Program Control:
-
fmt
Package functions will not affect the program's control flow. -
The program will be terminated.
It will cause panic and help handle exceptions.
Summarize
-
fmt
Packages are suitable for general formatted outputs, such as printing ordinary messages, debug outputs, etc. -
log
Packages are suitable for formal logging, providing features such as timestamps, log levels, log marks and custom outputs, which can better record and track the running status of the program.
This is the end of this article about the detailed explanation of the log package in go language. For more related go language log package content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!