Go by Example: Logging
Go by Example : Logging
The Go standard library provides straightforward tools for outputting logs from Go programs, with the log package for free-form output and the log/slog package for structured output. |
|
| |
| |
| |
Simply invoking functions like
|
|
Loggers can be configured with
flags
to set
their output format. By default, the standard
logger has the
|
|
It also supports emitting the file name and
line from which the
|
|
It may be useful to create a custom logger and pass it around. When creating a new logger, we can set a prefix to distinguish its output from other loggers. |
|
We can set the prefix
on existing loggers (including the standard one)
with the
|
|
Loggers can have custom output targets;
any
|
|
This call writes the log output into
|
|
This will actually show it on standard output. |
|
The
|
|
In addition to the message,
|
|
Sample output; the date and time emitted will depend on when the example ran. |
|
These are wrapped for clarity of presentation on the website; in reality they are emitted on a single line. |
|
Next example: HTTP Client .