Created
May 11, 2024 21:18
-
-
Save kwdowicz/2317f5939ef9d20eb9008070e45aa9e3 to your computer and use it in GitHub Desktop.
Basic but cool Rust logging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[dependencies] | |
chrono = "0.4.38" | |
fern = "0.6.2" | |
log = "0.4.21" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use log::info; | |
extern crate fern; | |
extern crate log; | |
extern crate chrono; | |
fn main() { | |
setup_logging().expect("Failed to initialize logging."); | |
info!("Started DockerFace"); | |
} | |
fn setup_logging() -> Result<(), fern::InitError> { | |
fern::Dispatch::new() | |
.format(|out, message, record| { | |
out.finish(format_args!( | |
"{}[{}][{}] {}", | |
chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"), | |
record.target(), | |
record.level(), | |
message | |
)) | |
}) | |
.level(log::LevelFilter::Info) | |
.chain(fern::log_file("output.log")?) | |
.chain(std::io::stdout()) | |
.apply()?; | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment