Log Messages
Recipe | Crates | Categories |
---|---|---|
Log a Debug Message to the Console | ||
Log an Error Message to the Console | ||
Log to stdout Instead of stderr | ||
Log Messages with a Custom Logger | ||
Log to the Unix Syslog |
Log a Debug Message to the Console
The log
⮳ crate provides logging utilities. The env_logger
⮳ crate configures logging via an environment variable. The log::debug
⮳ macro works like other std::fmt
⮳ formatted strings.
No output prints when running this code. By default, the log level is error
⮳, and any lower levels are dropped.
Set the RUST_LOG
⮳ environment variable to print the message:
RUST_LOG=debug cargo run
Cargo prints debugging information then the following line at the very end of the output:
DEBUG:main: Executing query: DROP TABLE students
Log an Error Message to the Console
Proper error handling considers exceptions exceptional. Here, an error logs to stderr with log
's convenience macro log::error
⮳.
Log to stdout
Instead of stderr
log
⮳
Creates a custom logger configuration using the env_logger::Builder::target
⮳ to set the target of the log output to env_logger::fmt::Target
⮳.
Log Messages with a Custom Logger
Implements a custom logger ConsoleLogger
which prints to stdout. In order to use the logging macros, ConsoleLogger
implements the log::Log
⮳ trait and log::Log
⮳ installs it.
Log to the Unix Syslog
log
⮳
Logs messages to UNIX syslog
⮳. Initializes logger backend with syslog::init
⮳ syslog::init
⮳ records the program submitting the log entry's classification, syslog::init
⮳ denotes allowed log verbosity and Option<&str>
holds optional application name.