Maintain/Run/
Logger.rs

1//=============================================================================//
2// File Path: Element/Maintain/Source/Run/Logger.rs
3//=============================================================================//
4// Module: Logger
5//
6// Brief Description: Logging utilities for the Run module.
7//
8// RESPONSIBILITIES:
9// ================
10//
11// Primary:
12// - Initialize the logging infrastructure for run operations
13// - Provide colored, structured log output
14// - Support configurable log levels
15//
16// Secondary:
17// - Format log messages consistently
18// - Support file and console output
19//
20// ARCHITECTURAL ROLE:
21// ===================
22//
23// Position:
24// - Infrastructure/Logging layer
25// - Log initialization
26//
27// Dependencies (What this module requires):
28// - External crates: env_logger, log, colored
29// - Internal modules: Constant
30// - Traits implemented: None
31//
32// Dependents (What depends on this module):
33// - Run entry point (Fn)
34// - Run Process module
35//
36//=============================================================================//
37// IMPLEMENTATION
38//=============================================================================//
39
40use crate::Run::Constant::LogEnv;
41
42use env_logger::Builder;
43use log::LevelFilter;
44use std::io::Write;
45
46/// Initializes the logger for run operations.
47///
48/// This function sets up a colored, structured logger with support for
49/// configurable log levels via the `RUST_LOG` environment variable.
50///
51/// # Log Levels
52///
53/// Control log verbosity with `RUST_LOG`:
54/// ```sh
55/// export RUST_LOG=debug # More verbose output
56/// export RUST_LOG=info  # Standard output
57/// export RUST_LOG=error # Only errors
58/// export RUST_LOG=Run=debug # Run module only
59/// ```
60///
61/// # Example
62///
63/// ```rust
64/// use crate::Run::Logger;
65/// Logger();
66/// ```
67pub fn Logger() {
68    Builder::from_env(LogEnv)
69        .format(|buf, record| {
70            use colored::Colorize;
71
72            let level = record.level();
73            let level_str = match level {
74                log::Level::Error => "ERROR".red(),
75                log::Level::Warn => "WARN ".yellow(),
76                log::Level::Info => "INFO ".green(),
77                log::Level::Debug => "DEBUG".blue(),
78                log::Level::Trace => "TRACE".magenta(),
79            };
80
81            let target = record.target();
82            let module = target.split("::").last().unwrap_or("Run");
83
84            writeln!(
85                buf,
86                "{} [{}] {}",
87                level_str,
88                module.white().bold(),
89                record.args()
90            )
91        })
92        .filter(None, LevelFilter::Info)
93        .init();
94}
95
96/// Logs a run header message.
97///
98/// # Arguments
99///
100/// * `profile` - The profile name
101/// * `workbench` - The workbench type
102pub fn log_run_header(profile: &str, workbench: Option<&str>) {
103    use log::info;
104
105    info!("========================================");
106    info!("Land Run: {}", profile);
107    info!("========================================");
108
109    if let Some(wb) = workbench {
110        info!("Workbench: {}", wb);
111    }
112}
113
114/// Logs environment variable resolution.
115///
116/// # Arguments
117///
118/// * `env` - The resolved environment variables
119pub fn log_environment(env: &std::collections::HashMap<String, String>) {
120    use log::debug;
121
122    debug!("Resolved environment variables:");
123    for (key, value) in env {
124        let display_value = if value.is_empty() {
125            "(empty)"
126        } else {
127            value.as_str()
128        };
129        debug!("  {} = {}", key, display_value);
130    }
131}
132
133/// Logs a success message.
134///
135/// # Arguments
136///
137/// * `message` - The success message
138pub fn log_success(message: &str) {
139    use log::info;
140    use colored::Colorize;
141
142    info!("{}", message.green());
143}
144
145/// Logs an error message.
146///
147/// # Arguments
148///
149/// * `message` - The error message
150pub fn log_error(message: &str) {
151    use log::error;
152    use colored::Colorize;
153
154    error!("{}", message.red());
155}
156
157/// Logs a warning message.
158///
159/// # Arguments
160///
161/// * `message` - The warning message
162pub fn log_warning(message: &str) {
163    use log::warn;
164    use colored::Colorize;
165
166    warn!("{}", message.yellow());
167}
168
169/// Logs the start of a run process.
170///
171/// # Arguments
172///
173/// * `command` - The command being executed
174pub fn log_run_start(command: &str) {
175    use log::info;
176
177    info!("Starting run: {}", command);
178}
179
180/// Logs the completion of a run process.
181///
182/// # Arguments
183///
184/// * `success` - Whether the run completed successfully
185pub fn log_run_complete(success: bool) {
186    use log::info;
187    use colored::Colorize;
188
189    if success {
190        info!("{}", "Run completed successfully".green());
191    } else {
192        info!("{}", "Run completed with errors".red());
193    }
194}
195
196/// Logs hot-reload status.
197///
198/// # Arguments
199///
200/// * `enabled` - Whether hot-reload is enabled
201/// * `port` - The live-reload port
202pub fn log_hot_reload_status(enabled: bool, port: u16) {
203    use log::info;
204    use colored::Colorize;
205
206    if enabled {
207        info!("Hot-reload enabled on port {}", port.to_string().cyan());
208    } else {
209        info!("Hot-reload disabled");
210    }
211}
212
213/// Logs watch mode status.
214///
215/// # Arguments
216///
217/// * `enabled` - Whether watch mode is enabled
218pub fn log_watch_status(enabled: bool) {
219    use log::info;
220    use colored::Colorize;
221
222    if enabled {
223        info!("Watch mode {}", "enabled".green());
224    } else {
225        info!("Watch mode disabled");
226    }
227}