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 std::io::Write;
41
42use env_logger::Builder;
43use log::LevelFilter;
44
45use crate::Run::Constant::LogEnv;
46
47/// Initializes the logger for run operations.
48///
49/// This function sets up a colored, structured logger with support for
50/// configurable log levels via the `RUST_LOG` environment variable.
51///
52/// # Log Levels
53///
54/// Control log verbosity with `RUST_LOG`:
55/// ```sh
56/// export RUST_LOG=debug # More verbose output
57/// export RUST_LOG=info # Standard output
58/// export RUST_LOG=error # Only errors
59/// export RUST_LOG=Run=debug # Run module only
60/// ```
61///
62/// # Example
63///
64/// ```rust
65/// use crate::Run::Logger;
66/// Logger();
67/// ```
68pub fn Logger() {
69 Builder::from_env(LogEnv)
70 .format(|buf, record| {
71 use colored::Colorize;
72
73 let level = record.level();
74
75 let level_str = match level {
76 log::Level::Error => "ERROR".red(),
77 log::Level::Warn => "WARN ".yellow(),
78 log::Level::Info => "INFO ".green(),
79 log::Level::Debug => "DEBUG".blue(),
80 log::Level::Trace => "TRACE".magenta(),
81 };
82
83 let target = record.target();
84
85 let module = target.split("::").last().unwrap_or("Run");
86
87 writeln!(buf, "{} [{}] {}", level_str, module.white().bold(), record.args())
88 })
89 .filter(None, LevelFilter::Info)
90 .init();
91}
92
93/// Logs a run header message.
94///
95/// # Arguments
96///
97/// * `profile` - The profile name
98/// * `workbench` - The workbench type
99pub fn LogRunHeader(Profile:&str, Workbench:Option<&str>) {
100 use log::info;
101
102 info!("========================================");
103
104 info!("Land Run: {}", Profile);
105
106 info!("========================================");
107
108 if let Some(Wb) = Workbench {
109 info!("Workbench: {}", Wb);
110 }
111}
112
113/// Logs environment variable resolution.
114///
115/// # Arguments
116///
117/// * `env` - The resolved environment variables
118pub fn LogEnvironment(Env:&std::collections::HashMap<String, String>) {
119 use log::debug;
120
121 debug!("Resolved environment variables:");
122
123 for (Key, Value) in Env {
124 let DisplayValue = if Value.is_empty() { "(empty)" } else { Value.as_str() };
125
126 debug!(" {} = {}", Key, DisplayValue);
127 }
128}
129
130/// Logs a success message.
131///
132/// # Arguments
133///
134/// * `message` - The success message
135pub fn LogSuccess(Message:&str) {
136 use log::info;
137 use colored::Colorize;
138
139 info!("{}", Message.green());
140}
141
142/// Logs an error message.
143///
144/// # Arguments
145///
146/// * `message` - The error message
147pub fn LogError(Message:&str) {
148 use log::error;
149 use colored::Colorize;
150
151 error!("{}", Message.red());
152}
153
154/// Logs a warning message.
155///
156/// # Arguments
157///
158/// * `message` - The warning message
159pub fn LogWarning(Message:&str) {
160 use log::warn;
161 use colored::Colorize;
162
163 warn!("{}", Message.yellow());
164}
165
166/// Logs the start of a run process.
167///
168/// # Arguments
169///
170/// * `command` - The command being executed
171pub fn LogRunStart(Command:&str) {
172 use log::info;
173
174 info!("Starting run: {}", Command);
175}
176
177/// Logs the completion of a run process.
178///
179/// # Arguments
180///
181/// * `success` - Whether the run completed successfully
182pub fn LogRunComplete(Success:bool) {
183 use log::info;
184 use colored::Colorize;
185
186 if Success {
187 info!("{}", "Run completed successfully".green());
188 } else {
189 info!("{}", "Run completed with errors".red());
190 }
191}
192
193/// Logs hot-reload status.
194///
195/// # Arguments
196///
197/// * `enabled` - Whether hot-reload is enabled
198/// * `port` - The live-reload port
199pub fn LogHotReloadStatus(Enabled:bool, Port:u16) {
200 use log::info;
201 use colored::Colorize;
202
203 if Enabled {
204 info!("Hot-reload enabled on port {}", Port.to_string().cyan());
205 } else {
206 info!("Hot-reload disabled");
207 }
208}
209
210/// Logs watch mode status.
211///
212/// # Arguments
213///
214/// * `enabled` - Whether watch mode is enabled
215pub fn LogWatchStatus(Enabled:bool) {
216 use log::info;
217 use colored::Colorize;
218
219 if Enabled {
220 info!("Watch mode {}", "enabled".green());
221 } else {
222 info!("Watch mode disabled");
223 }
224}