Skip to main content

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