baizekit_log/
component.rs1use crate::config::LogConfig;
2use crate::format::LogFormat;
3use crate::timer::LocalTimer;
4use baizekit_app::application::ApplicationInner;
5use baizekit_app::async_trait::async_trait;
6use baizekit_app::component::Component;
7use std::sync::Arc;
8use tracing_appender::non_blocking::WorkerGuard;
9
10pub struct LogComponent {
11 #[allow(unused)]
12 guard: WorkerGuard,
13}
14
15impl LogComponent {
16 pub async fn new(inner: Arc<ApplicationInner>, _label: String) -> baizekit_app::anyhow::Result<Self> {
17 let conf = inner.config().await;
18 let conf: LogConfig = conf.get("log")?;
19
20 let (non_blocking, guard) = tracing_appender::non_blocking(std::io::stdout());
21
22 let sub_builder = tracing_subscriber::fmt()
24 .with_ansi(conf.ansi)
25 .with_file(conf.with_filename)
26 .with_line_number(conf.with_line_number)
27 .with_max_level(conf.level)
28 .with_timer(LocalTimer)
29 .with_writer(non_blocking);
30
31 match conf.format {
32 LogFormat::Compact => sub_builder.compact().init(),
33 LogFormat::Pretty => sub_builder.pretty().init(),
34 LogFormat::Json => sub_builder.json().init(),
35 };
36
37 Ok(LogComponent { guard })
38 }
39}
40
41#[async_trait]
42impl Component for LogComponent {}