2021-02-19 06:00:45 +00:00
|
|
|
use anyhow::Result;
|
2021-03-04 00:28:58 +00:00
|
|
|
use tracing_subscriber::filter::LevelFilter;
|
2022-07-26 12:08:55 +00:00
|
|
|
use tracing_subscriber::fmt::time::UtcTime;
|
2021-03-04 00:28:58 +00:00
|
|
|
use tracing_subscriber::FmtSubscriber;
|
2020-10-15 22:14:39 +00:00
|
|
|
|
2021-09-09 11:35:03 +00:00
|
|
|
pub fn init(level: LevelFilter, json_format: bool, timestamp: bool) -> Result<()> {
|
2021-02-22 01:41:04 +00:00
|
|
|
if level == LevelFilter::OFF {
|
2020-10-15 22:14:39 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2020-12-04 05:27:17 +00:00
|
|
|
let is_terminal = atty::is(atty::Stream::Stderr);
|
2021-03-17 00:41:28 +00:00
|
|
|
|
|
|
|
let builder = FmtSubscriber::builder()
|
|
|
|
.with_env_filter(format!("asb={},swap={}", level, level))
|
2020-12-04 05:27:17 +00:00
|
|
|
.with_writer(std::io::stderr)
|
2020-10-15 22:14:39 +00:00
|
|
|
.with_ansi(is_terminal)
|
2022-07-26 12:08:55 +00:00
|
|
|
.with_timer(UtcTime::rfc_3339())
|
2021-03-17 00:41:28 +00:00
|
|
|
.with_target(false);
|
|
|
|
|
2021-09-09 11:35:03 +00:00
|
|
|
match (json_format, timestamp) {
|
|
|
|
(true, true) => builder.json().init(),
|
|
|
|
(true, false) => builder.json().without_time().init(),
|
|
|
|
(false, true) => builder.init(),
|
|
|
|
(false, false) => builder.without_time().init(),
|
2021-03-17 00:41:28 +00:00
|
|
|
}
|
2020-10-15 22:14:39 +00:00
|
|
|
|
2021-05-05 03:49:11 +00:00
|
|
|
tracing::info!(%level, "Initialized tracing");
|
2020-10-15 22:14:39 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|