File size: 1,650 Bytes
bbb1195 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | use tracing::{info, warn, error};
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
// Custom local timezone time formatter
struct LocalTimer;
impl tracing_subscriber::fmt::time::FormatTime for LocalTimer {
fn format_time(&self, w: &mut tracing_subscriber::fmt::format::Writer<'_>) -> std::fmt::Result {
let now = chrono::Local::now();
write!(w, "{}", now.to_rfc3339())
}
}
/// Initialize logging system (stdout only for cloud deployment)
pub fn init_logger() {
// Capture log macro logs
let _ = tracing_log::LogTracer::init();
// Console output layer with local timezone
let console_layer = fmt::Layer::new()
.with_target(false)
.with_thread_ids(false)
.with_level(true)
.with_timer(LocalTimer);
// Filter layer (default INFO and above)
let filter_layer = EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("info"));
// Initialize global subscriber
let _ = tracing_subscriber::registry()
.with(filter_layer)
.with(console_layer)
.try_init();
info!("Logger initialized (stdout only for cloud deployment)");
}
/// Log info message (backward compatible interface)
pub fn log_info(message: &str) {
info!("{}", message);
}
/// Log warning message (backward compatible interface)
pub fn log_warn(message: &str) {
warn!("{}", message);
}
/// Log error message (backward compatible interface)
pub fn log_error(message: &str) {
error!("{}", message);
}
/// Clear logs (no-op for cloud deployment)
pub fn clear_logs() -> Result<(), String> {
Ok(())
}
|