init_production_logger

Function init_production_logger 

Source
pub fn init_production_logger<P: AsRef<Path>>(
    file_path: Option<P>,
) -> Result<(), LogError>
Expand description

Initialize a production-ready logger (no file info, concise format).

This uses LoggerConfig::production() which is optimized for clean, minimal output in production environments:

  • Info as the minimum log level (no debug messages)
  • No file information shown in logs
  • No date in stdout output (only time)
  • Colors enabled for better readability

§Arguments

  • file_path - Optional path to a log file. If None, logs will only go to stdout.

§Returns

Ok(()) if initialization succeeded, or an error if it failed.

§Example

use fstdout_logger::init_production_logger;

init_production_logger(Some("app.log"))
    .expect("Failed to initialize production logger");
Examples found in repository?
examples/production.rs (line 42)
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}