1use log4rs::config::RawConfig;
2use serde::Deserialize;
3use crate::context::context::Context;
4use crate::core::Error;
5
6const DEFAULT_LOGGING_CONFIG: &str = r#"
7{
8 "appenders": {
9 "stdout": {
10 "kind": "console",
11 "encoder": {
12 "pattern": "{d(%Y-%m-%dT%H:%M:%S%.3fZ)} {pid} --- [{T:15.15}] {h({l:>5.5})} {M}: {m}{n}"
13 }
14 }
15 },
16 "root": {
17 "level": "info",
18 "appenders": ["stdout"]
19 }
20}
21"#;
22
23pub fn init_logger(config: &Context) -> Result<(), Error> {
24 let config = config.get_bean::<config::Config>("config")
25 .map_err(|e| Error::from(format!("Failed to get config bean for logger initialization: {}", e)))?;
26
27 let raw_config_value = config.get::<serde_json::Value>("logging")
28 .unwrap_or_else(|e| {
29 log::debug!("No custom logging configuration found ({}), using default configuration", e);
30 serde_json::from_str::<serde_json::Value>(DEFAULT_LOGGING_CONFIG)
31 .expect("Failed to parse default logging configuration")
32 });
33
34 let raw_config = RawConfig::deserialize(raw_config_value)
35 .map_err(|e| {
36 log::warn!("Failed to deserialize logging configuration: {}, using default configuration", e);
37 e
38 })
39 .unwrap_or_else(|_| RawConfig::default());
40
41 log4rs::init_raw_config(raw_config).map_err(|e| {
42 Error::from(format!("Failed to initialize log4rs logger: {}", e))
43 })
44}