Created
June 30, 2023 07:16
-
-
Save leiless/42d074b997142a1d065aae39a94c3d35 to your computer and use it in GitHub Desktop.
Rust: log + env_logger + color-backtrace
This file contains 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 std::io::Write; | |
struct FileStream { | |
file: std::fs::File, | |
} | |
impl FileStream { | |
fn new<S: AsRef<std::path::Path>>(s: S) -> std::io::Result<Self> { | |
//let file = std::fs::File::create(s)?; | |
let file = std::fs::File::options().create(true).append(true).open(s)?; | |
Ok(Self { | |
file, | |
}) | |
} | |
} | |
impl std::io::Write for FileStream { | |
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { | |
self.file.write(buf) | |
} | |
fn flush(&mut self) -> std::io::Result<()> { | |
self.file.flush() | |
} | |
} | |
impl color_backtrace::termcolor::WriteColor for FileStream { | |
fn supports_color(&self) -> bool { | |
false | |
} | |
fn set_color(&mut self, _spec: &color_backtrace::termcolor::ColorSpec) -> std::io::Result<()> { | |
Ok(()) | |
} | |
fn reset(&mut self) -> std::io::Result<()> { | |
Ok(()) | |
} | |
} | |
// log = "0.4" | |
// env_logger = "0.10" | |
// chrono = "0.4" | |
// color-backtrace = "0.5" | |
fn main() -> Result<(), Box<dyn std::error::Error>> { | |
let file_name = "app.log"; | |
let target = Box::new(std::fs::File::create(file_name).expect("Can't create file")); | |
env_logger::Builder::new() | |
.target(env_logger::Target::Pipe(target)) | |
.filter(None, log::LevelFilter::Trace) | |
.format(|buf, record| { | |
writeln!( | |
buf, | |
"[{} {:>5} {}:{}] {}", | |
chrono::Local::now().format("%Y-%m-%d %H:%M:%S%.3f %z"), | |
record.level(), | |
record.file().unwrap_or("unknown"), | |
record.line().unwrap_or(0), | |
record.args() | |
) | |
}) | |
.init(); | |
log::debug!("Debug"); | |
log::info!("Info"); | |
log::warn!("Warn"); | |
log::trace!("Trace"); | |
log::error!("Error"); | |
color_backtrace::BacktracePrinter::new() | |
.message("\n[CRITICAL] The application panicked (crashed)") | |
.install(FileStream::new(file_name)?); | |
// Test | |
assert!(false); | |
Ok(()) | |
} |
Author
leiless
commented
Jun 30, 2023
•
log4rs
+ RollingFileAppender
solution
struct FileStream {
file: std::fs::File,
}
impl FileStream {
fn new<S: AsRef<std::path::Path>>(s: S) -> std::io::Result<Self> {
//let file = std::fs::File::create(s)?;
let file = std::fs::File::options().create(true).append(true).open(s)?;
Ok(Self {
file,
})
}
}
impl std::io::Write for FileStream {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.file.write(buf)
}
fn flush(&mut self) -> std::io::Result<()> {
self.file.flush()
}
}
impl color_backtrace::termcolor::WriteColor for FileStream {
fn supports_color(&self) -> bool {
false
}
fn set_color(&mut self, _spec: &color_backtrace::termcolor::ColorSpec) -> std::io::Result<()> {
Ok(())
}
fn reset(&mut self) -> std::io::Result<()> {
Ok(())
}
}
// log = "0.4"
// log4rs = "1.2"
// color-backtrace = "0.5"
fn main() -> Result<(), Box<dyn std::error::Error>> {
let file_name = "app.log";
let max_rolling_file = 3;
let fixed_window_roller =
log4rs::append::rolling_file::policy::compound::roll::fixed_window::FixedWindowRoller::builder().build(&format!("{}.{{}}", file_name), max_rolling_file)?;
let max_log_size_in_bytes = 1024 * 1024;
let size_trigger = log4rs::append::rolling_file::policy::compound::trigger::size::SizeTrigger::new(max_log_size_in_bytes);
let compound_policy =
log4rs::append::rolling_file::policy::compound::CompoundPolicy::new(Box::new(size_trigger), Box::new(fixed_window_roller));
let logfile = log4rs::append::rolling_file::RollingFileAppender::builder()
// https://docs.rs/log4rs/1.2.0/log4rs/encode/pattern/index.html
.encoder(Box::new(log4rs::encode::pattern::PatternEncoder::new("[{d(%Y-%m-%d %H:%M:%S%.3f %z)} {t}:{T} {l:>5} {f}:{L:0>3}] {m}{n}")))
.build(file_name, Box::new(compound_policy))?;
let appender_name = "dummy";
let config = log4rs::config::Config::builder()
.appender(log4rs::config::Appender::builder().build(appender_name, Box::new(logfile)))
.build(log4rs::config::Root::builder()
.appender(appender_name)
.build(log::LevelFilter::Trace))?;
log4rs::init_config(config)?;
color_backtrace::BacktracePrinter::new()
.message("\n[CRITICAL] The application panicked (crashed)")
.install(FileStream::new(file_name)?);
// Tests
log::debug!("Debug");
log::info!("Info");
log::warn!("Warn");
log::trace!("Trace");
log::error!("Error");
assert!(false);
Ok(())
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment