Logging
Printing log messages from a program can be an important tool for troubleshooting. Log messages tell you what is going on at any given moment, and provide much-needed contextual information when something goes wrong. Go standard library provides convenient packages to generate and manage log messages from programs. Here, we will look at using the log
package, which can be used to generate text messages, and the slog
package, which can be used to generate structured log messages from programs.
This chapter contains the following recipes:
- Using the standard logger
- Writing log messages
- Controlling format
- Changing where to log
- Using the structured logger
- Logging using the global logger
- Writing structured logs using different levels
- Changing log level at runtime
- Using loggers with additional attributes
- Changing where to log
- Adding logging information from the context