diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-06-18 20:56:05 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-06-18 20:56:05 +0800 |
| commit | 68daa10abfe3015beca966825d32cf67c9f5d5d7 (patch) | |
| tree | c28f0470240e7cdc3748cee57ef74353514c47b7 /rola-cli/src/output/env_logger.rs | |
| parent | 669898193bebeadc975881bee496fe0239df76a0 (diff) | |
feat(bucket): implement bucket initialization and logging infrastructure
Add bucket init logic with directory structure creation and log macros
for tracing
Diffstat (limited to 'rola-cli/src/output/env_logger.rs')
| -rw-r--r-- | rola-cli/src/output/env_logger.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/rola-cli/src/output/env_logger.rs b/rola-cli/src/output/env_logger.rs new file mode 100644 index 0000000..289de77 --- /dev/null +++ b/rola-cli/src/output/env_logger.rs @@ -0,0 +1,54 @@ +use chrono::Local; +use std::io::Write; + +use crate::output::ansi_control::Colorize; + +/// Simple env logger that prints formatted messages. +/// Usage: `env_logger::init_with(EnvLogger { show_time: true, show_level: true });` +pub struct EnvLogger { + pub show_time: bool, + pub show_level: bool, + pub level: log::Level, +} + +impl log::Log for EnvLogger { + fn enabled(&self, metadata: &log::Metadata) -> bool { + metadata.level() <= log::Level::Info + } + + fn log(&self, record: &log::Record) { + if !self.enabled(record.metadata()) { + return; + } + + let mut buf = Vec::new(); + + if self.show_time { + let now = Local::now().format("%Y-%m-%d %H:%M:%S%.3f"); + write!(buf, "[{}] ", now).ok(); + } + + if self.show_level { + let level_str = match record.level() { + log::Level::Trace => "TRACE".bright_black(), + log::Level::Debug => "DEBUG".cyan(), + log::Level::Info => "INFO".green(), + log::Level::Warn => "WARN".yellow(), + log::Level::Error => "ERROR".red(), + }; + write!(buf, "{}: ", level_str.bold()).ok(); + } + + write!(buf, "{}", record.args()).ok(); + eprintln!("{}", String::from_utf8_lossy(&buf)); + } + + fn flush(&self) {} +} + +/// Initialize the env logger with the given configuration. +pub fn init_envlogger(config: EnvLogger) { + log::set_boxed_logger(Box::new(config)) + .map(|()| log::set_max_level(log::LevelFilter::Info)) + .ok(); +} |
