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	info!("Land Run: {}", Profile);
102	info!("========================================");
103
104	if let Some(Wb) = Workbench {
105		info!("Workbench: {}", Wb);
106	}
107}
108
109/// Logs environment variable resolution.
110///
111/// # Arguments
112///
113/// * `env` - The resolved environment variables
114pub fn LogEnvironment(Env:&std::collections::HashMap<String, String>) {
115	use log::debug;
116
117	debug!("Resolved environment variables:");
118	for (Key, Value) in Env {
119		let DisplayValue = if Value.is_empty() { "(empty)" } else { Value.as_str() };
120		debug!(" {} = {}", Key, DisplayValue);
121	}
122}
123
124/// Logs a success message.
125///
126/// # Arguments
127///
128/// * `message` - The success message
129pub fn LogSuccess(Message:&str) {
130	use log::info;
131	use colored::Colorize;
132
133	info!("{}", Message.green());
134}
135
136/// Logs an error message.
137///
138/// # Arguments
139///
140/// * `message` - The error message
141pub fn LogError(Message:&str) {
142	use log::error;
143	use colored::Colorize;
144
145	error!("{}", Message.red());
146}
147
148/// Logs a warning message.
149///
150/// # Arguments
151///
152/// * `message` - The warning message
153pub fn LogWarning(Message:&str) {
154	use log::warn;
155	use colored::Colorize;
156
157	warn!("{}", Message.yellow());
158}
159
160/// Logs the start of a run process.
161///
162/// # Arguments
163///
164/// * `command` - The command being executed
165pub fn LogRunStart(Command:&str) {
166	use log::info;
167
168	info!("Starting run: {}", Command);
169}
170
171/// Logs the completion of a run process.
172///
173/// # Arguments
174///
175/// * `success` - Whether the run completed successfully
176pub fn LogRunComplete(Success:bool) {
177	use log::info;
178	use colored::Colorize;
179
180	if Success {
181		info!("{}", "Run completed successfully".green());
182	} else {
183		info!("{}", "Run completed with errors".red());
184	}
185}
186
187/// Logs hot-reload status.
188///
189/// # Arguments
190///
191/// * `enabled` - Whether hot-reload is enabled
192/// * `port` - The live-reload port
193pub fn LogHotReloadStatus(Enabled:bool, Port:u16) {
194	use log::info;
195	use colored::Colorize;
196
197	if Enabled {
198		info!("Hot-reload enabled on port {}", Port.to_string().cyan());
199	} else {
200		info!("Hot-reload disabled");
201	}
202}
203
204/// Logs watch mode status.
205///
206/// # Arguments
207///
208/// * `enabled` - Whether watch mode is enabled
209pub fn LogWatchStatus(Enabled:bool) {
210	use log::info;
211	use colored::Colorize;
212
213	if Enabled {
214		info!("Watch mode {}", "enabled".green());
215	} else {
216		info!("Watch mode disabled");
217	}
218}