Using the structured logger
Since the standard logger has limited practical use, many third-party logging libraries were developed by the community. Some of the patterns that emerged from these libraries emphasized structured logging and performance. The structured logging package was added to the standard library with these usage patterns in mind. The log
package is still a useful tool for development as it provides a simple interface for developers and the users of the program, but the log/slog
package is a production quality library that enables automated log analysis tools while providing a simple-to-use and flexible interface.
Logging using the global logger
Similar to the log
package, there is a global structured logger accessible via the slog.Default()
function. You can simply configure a global logger and use that in your program.
Tip
It is advisable to pass an instance of a logger around for any nontrivial project. The logging requirements may change from environment...