pub struct LoggerConfig {
pub show_file_info: bool,
pub show_date_in_stdout: bool,
pub use_colors: bool,
pub level: LevelFilter,
}Expand description
Configuration for the logger.
This struct controls the behavior and appearance of logs, including:
- Minimum log level to display
- Whether to show file and line information
- Whether to show dates in stdout logs
- Whether to use colors in stdout output
§Examples
use fstdout_logger::LoggerConfig;
use log::LevelFilter;
// Create with default settings
let default_config = LoggerConfig::default();
// Create using a builder
let custom_config = LoggerConfig::builder()
.level(LevelFilter::Debug)
.show_file_info(false)
.build();
// Create using presets
let prod_config = LoggerConfig::production();
let dev_config = LoggerConfig::development();Fields§
§show_file_info: boolWhether to show file and line information in log messages
show_date_in_stdout: boolWhether to show date in stdout logs (always shown in file logs)
use_colors: boolWhether to use colors in stdout logs
level: LevelFilterMinimum log level to display
Implementations§
Source§impl LoggerConfig
impl LoggerConfig
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new logger configuration with default settings.
This is equivalent to calling LoggerConfig::default().
Sourcepub fn builder() -> LoggerConfigBuilder
pub fn builder() -> LoggerConfigBuilder
Create a new configuration builder.
This returns a LoggerConfigBuilder that can be used to construct
a custom configuration with a fluent API.
§Example
use fstdout_logger::LoggerConfig;
use log::LevelFilter;
let config = LoggerConfig::builder()
.level(LevelFilter::Debug)
.show_file_info(false)
.build();Examples found in repository?
examples/stdout_only.rs (line 15)
13fn main() {
14 // Initialize logger with stdout only (no file output) with colors
15 let config = LoggerConfig::builder()
16 .level(LevelFilter::Debug) // Show Debug level and above
17 .show_file_info(false) // Don't show file and line information
18 .use_colors(true) // Use colors in output
19 .build();
20
21 if let Err(e) = init_stdout_logger(config) {
22 eprintln!("Failed to initialize logger: {e}");
23 return;
24 }
25
26 println!("Logger initialized! Output will only appear on stdout.");
27 println!("Notice that TRACE level messages won't show because we set Debug as minimum level.");
28 println!("Also note that file and line information is hidden for cleaner output.");
29
30 // Log messages at different levels to demonstrate filtering
31 trace!("This is a TRACE message - you won't see this");
32 debug!("This is a DEBUG message - visible");
33 info!("This is an INFO message - visible");
34 warn!("This is a WARNING message - visible");
35 error!("This is an ERROR message - visible");
36
37 // Log messages with dynamic content
38 for i in 1..=3 {
39 let value = i * 10;
40 debug!("Debug calculation: {i} * 10 = {value}");
41 info!("Processing item #{i} with value {value}");
42 }
43
44 info!("Example completed");
45}More examples
examples/basic_usage.rs (line 21)
15fn main() {
16 // Initialize logger with file output and colored stdout
17 let log_path = "application.log";
18
19 // Create a custom configuration with the builder pattern
20 // This shows how to configure every aspect of the logger
21 let config = LoggerConfig::builder()
22 .level(LevelFilter::Trace) // Show all log levels, including TRACE
23 .show_file_info(true) // Include file and line info in logs
24 .show_date_in_stdout(false) // Only show time (HH:MM:SS) in console output
25 .use_colors(true) // Use colors for different log levels
26 .build();
27
28 if let Err(e) = init_logger_with_config(Some(log_path), config) {
29 eprintln!("Failed to initialize logger: {e}");
30 return;
31 }
32
33 println!("Logger initialized! Check {log_path} for log output.");
34 println!("Log messages will appear both on stdout and in the log file.");
35 println!("Notice that stdout logs show time only while the file includes dates.");
36
37 // Log messages at different levels to demonstrate the hierarchy
38 // All of these will appear because we set level to Trace
39 trace!("This is a TRACE message"); // Lowest level, normally hidden
40 debug!("This is a DEBUG message"); // For developer information
41 info!("This is an INFO message"); // Normal application events
42 warn!("This is a WARNING message"); // Important but non-critical issues
43 error!("This is an ERROR message"); // Critical issues that need attention
44
45 // Simulate some application activity
46 for i in 1..=5 {
47 info!("Application is running... iteration {i}");
48 sleep(Duration::from_millis(500));
49 }
50
51 // Log a final message
52 info!("Application finished successfully");
53
54 println!("\nAfter running this example, check the 'application.log' file");
55 println!("to see how logs are formatted differently for file output.");
56}Sourcepub fn production() -> Self
pub fn production() -> Self
Create a new configuration optimized for production use.
Production settings:
show_file_info:false- Hide file/line for cleaner logsshow_date_in_stdout:false- Only show time in stdoutuse_colors:true- Keep colors for readabilitylevel:Info- Hide Debug/Trace logs in production
Examples found in repository?
examples/production.rs (line 11)
7fn main() {
8 // Step 1: Compare production vs development logger output
9 println!("=== PRODUCTION LOGGER SIMULATION ===");
10 // Production config: no file info, INFO level minimum
11 let prod_config = LoggerConfig::production();
12 println!("Production settings:");
13 println!(" - Log level: Info (no Debug or Trace messages)");
14 println!(" - File info: Hidden (cleaner logs)");
15 println!(" - Colors: Enabled (for better readability)");
16 println!(" - Date in console: Hidden (time only for brevity)\n");
17
18 // Manually format some logs with production settings
19 format_and_print_logs("Production", &prod_config);
20
21 // Reset logs
22 println!("\n\n=== DEVELOPMENT LOGGER SIMULATION ===");
23 // Development config: with file info, DEBUG level minimum
24 let dev_config = LoggerConfig::development();
25 println!("Development settings:");
26 println!(" - Log level: Debug (includes debug messages)");
27 println!(" - File info: Shown (helps with debugging)");
28 println!(" - Colors: Enabled (for better readability)");
29 println!(" - Date in console: Hidden (time only for brevity)\n");
30
31 // Manually format logs with development settings
32 format_and_print_logs("Development", &dev_config);
33
34 println!("\nNote: In production mode, logs are more concise (no file info, only time)");
35 println!(
36 "but the log file still contains complete information including date and file details."
37 );
38
39 // Step 2: Initialize a real logger for demonstration
40 println!("\n=== ACTUAL LOGGER IMPLEMENTATION ===");
41 println!("Initializing a real logger with production settings...");
42 if let Err(e) = fstdout_logger::init_production_logger(Some("prod.log")) {
43 eprintln!("Failed to initialize logger: {e}");
44 return;
45 }
46
47 // Log some real messages
48 info!("This message was logged with the actual logger");
49 debug!("Debug info won't appear in production mode");
50 warn!("Warnings will appear");
51 error!("Errors will appear too");
52
53 println!("\nCheck 'prod.log' to see the file output format with full details!");
54}Sourcepub fn development() -> Self
pub fn development() -> Self
Create a new configuration optimized for development use.
Development settings:
show_file_info:true- Show file/line for debuggingshow_date_in_stdout:false- Only show time in stdoutuse_colors:true- Use colors for readabilitylevel:Debug- Show Debug logs (but not Trace)
Examples found in repository?
examples/production.rs (line 24)
7fn main() {
8 // Step 1: Compare production vs development logger output
9 println!("=== PRODUCTION LOGGER SIMULATION ===");
10 // Production config: no file info, INFO level minimum
11 let prod_config = LoggerConfig::production();
12 println!("Production settings:");
13 println!(" - Log level: Info (no Debug or Trace messages)");
14 println!(" - File info: Hidden (cleaner logs)");
15 println!(" - Colors: Enabled (for better readability)");
16 println!(" - Date in console: Hidden (time only for brevity)\n");
17
18 // Manually format some logs with production settings
19 format_and_print_logs("Production", &prod_config);
20
21 // Reset logs
22 println!("\n\n=== DEVELOPMENT LOGGER SIMULATION ===");
23 // Development config: with file info, DEBUG level minimum
24 let dev_config = LoggerConfig::development();
25 println!("Development settings:");
26 println!(" - Log level: Debug (includes debug messages)");
27 println!(" - File info: Shown (helps with debugging)");
28 println!(" - Colors: Enabled (for better readability)");
29 println!(" - Date in console: Hidden (time only for brevity)\n");
30
31 // Manually format logs with development settings
32 format_and_print_logs("Development", &dev_config);
33
34 println!("\nNote: In production mode, logs are more concise (no file info, only time)");
35 println!(
36 "but the log file still contains complete information including date and file details."
37 );
38
39 // Step 2: Initialize a real logger for demonstration
40 println!("\n=== ACTUAL LOGGER IMPLEMENTATION ===");
41 println!("Initializing a real logger with production settings...");
42 if let Err(e) = fstdout_logger::init_production_logger(Some("prod.log")) {
43 eprintln!("Failed to initialize logger: {e}");
44 return;
45 }
46
47 // Log some real messages
48 info!("This message was logged with the actual logger");
49 debug!("Debug info won't appear in production mode");
50 warn!("Warnings will appear");
51 error!("Errors will appear too");
52
53 println!("\nCheck 'prod.log' to see the file output format with full details!");
54}Trait Implementations§
Source§impl Clone for LoggerConfig
impl Clone for LoggerConfig
Source§fn clone(&self) -> LoggerConfig
fn clone(&self) -> LoggerConfig
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for LoggerConfig
impl Debug for LoggerConfig
Auto Trait Implementations§
impl Freeze for LoggerConfig
impl RefUnwindSafe for LoggerConfig
impl Send for LoggerConfig
impl Sync for LoggerConfig
impl Unpin for LoggerConfig
impl UnwindSafe for LoggerConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more