logger: only log to file in release builds

This commit is contained in:
Alex Janka 2024-09-11 09:47:19 +10:00
parent f49bf7b949
commit 0ced792b8c
2 changed files with 34 additions and 32 deletions

View file

@ -14,44 +14,45 @@ use log4rs::{
use crate::prelude::*; use crate::prelude::*;
pub async fn init_logger() -> Res<()> { pub async fn init_logger(file: bool) -> Res<()> {
let stderr = ConsoleAppender::builder() let stderr = ConsoleAppender::builder()
.target(Target::Stderr) .target(Target::Stderr)
.encoder(Box::new(PatternEncoder::new("{h({l})}: {m}\n"))) .encoder(Box::new(PatternEncoder::new("{h({l})}: {m}\n")))
.build(); .build();
let trigger = SizeTrigger::new(8 * 1024); let mut builder = Config::builder().appender(
let roller = FixedWindowRoller::builder() Appender::builder()
.base(0) .filter(Box::new(ThresholdFilter::new(log::LevelFilter::Info)))
.build( .build("stderr", Box::new(stderr)),
format!("{}/{{}}.log", CONFIG.get().await.log_dir.display()).as_str(), );
3, let mut root = Root::builder().appender("stderr");
)
.unwrap();
let policy = CompoundPolicy::new(Box::new(trigger), Box::new(roller));
let logfile = log4rs::append::rolling_file::RollingFileAppender::builder() if file {
.encoder(Box::new(PatternEncoder::new("{l}: {m}\n"))) eprintln!("logging to file");
.build( let trigger = SizeTrigger::new(8 * 1024);
format!("{}/current.log", CONFIG.get().await.log_dir.display()).as_str(), let roller = FixedWindowRoller::builder()
Box::new(policy), .base(0)
) .build(
.unwrap(); format!("{}/{{}}.log", CONFIG.get().await.log_dir.display()).as_str(),
3,
)
.unwrap();
let policy = CompoundPolicy::new(Box::new(trigger), Box::new(roller));
let config = Config::builder() let logfile = log4rs::append::rolling_file::RollingFileAppender::builder()
.appender(Appender::builder().build("logfile", Box::new(logfile))) .encoder(Box::new(PatternEncoder::new("{l}: {m}\n")))
.appender( .build(
Appender::builder() format!("{}/current.log", CONFIG.get().await.log_dir.display()).as_str(),
.filter(Box::new(ThresholdFilter::new(log::LevelFilter::Info))) Box::new(policy),
.build("stderr", Box::new(stderr)), )
) .unwrap();
.build( builder = builder.appender(Appender::builder().build("logfile", Box::new(logfile)));
Root::builder() root = root.appender("logfile");
.appender("logfile") } else {
.appender("stderr") eprintln!("not logging to file");
.build(LevelFilter::Trace), }
)
.unwrap(); let config = builder.build(root.build(LevelFilter::Trace)).unwrap();
let _handle = log4rs::init_config(config).map_err(|_| Error::Log4Rs)?; let _handle = log4rs::init_config(config).map_err(|_| Error::Log4Rs)?;

View file

@ -23,7 +23,8 @@ static DEFAULT_BORDER: tokio::sync::OnceCell<colour::Format> = tokio::sync::Once
#[tokio::main] #[tokio::main]
async fn main() -> Res<()> { async fn main() -> Res<()> {
let _watch_handle = config::parse_config().await?; let _watch_handle = config::parse_config().await?;
logger::init_logger().await?; // only log to file in release builds
logger::init_logger(!cfg!(debug_assertions)).await?;
let mut event_connection = swayipc_async::Connection::new().await?; let mut event_connection = swayipc_async::Connection::new().await?;
let mut autosplit_connection = swayipc_async::Connection::new().await?; let mut autosplit_connection = swayipc_async::Connection::new().await?;