I am trying to use tracing-subscriber crate for logging purposes. So far, I was able to use the file rolling correctly and log the events in a file.
let appender = tracing_appender::rolling::daily("/logs", "log_test.log");
let (non_blocking_appender, _guard) = tracing_appender::non_blocking(appender);
tracing_subscriber::fmt().with_writer(non_blocking_appender).with_ansi(false).init();
info!("logging info!");
Output:
2023-07-13T09:48:10.321104Z INFO logging: logging info!
I want to customize the message with a
[year-month-day hour:minute:seconds]
timestamp instead.
I've tried both the event_format
and with_timer
, but if I use them, the init()
method is not longer available:
let timer = time::format_description::parse("[year]-[month padding:zero]-[day padding:zero] [hour]:[minute]:[second]").expect("Cataplum");
let timer_format = fmt::format().with_timer(timer);
let subscriber = tracing_subscriber::fmt().with_writer(non_blocking_appender).with_timer(timer_format);
Is there an easy way for format this message?
You can set a time format this way:
let timer = time::format_description::parse(
"[year]-[month padding:zero]-[day padding:zero] [hour]:[minute]:[second]",
)
.expect("Cataplum");
let time_offset =
time::UtcOffset::current_local_offset().unwrap_or_else(|_| time::UtcOffset::UTC);
let timer = fmt::time::OffsetTime::new(time_offset, timer);
let subscriber = tracing_subscriber::fmt()
.with_write(non_blocking_appender)
.with_timer(timer)
.init();