Struct LoggerConfigBuilder

Source
pub struct LoggerConfigBuilder { /* private fields */ }
Expand description

Builder for constructing a LoggerConfig using a fluent API.

This follows the builder pattern to provide a clean way to create custom logger configurations.

§Example

use fstdout_logger::LoggerConfigBuilder;
use log::LevelFilter;

let config = LoggerConfigBuilder::default()
    .level(LevelFilter::Warn)
    .show_file_info(true)
    .show_date_in_stdout(true)
    .use_colors(false)
    .build();

Implementations§

Source§

impl LoggerConfigBuilder

Source

pub fn show_file_info(self, show: bool) -> Self

Set whether to show file and line information in log messages.

When enabled, each log message will include the source file and line number where the log was created. This is useful for debugging but can make logs more verbose for production use.

Default: true

Examples found in repository?
examples/stdout_only.rs (line 17)
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
Hide additional examples
examples/basic_usage.rs (line 23)
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}
Source

pub fn show_date_in_stdout(self, show: bool) -> Self

Set whether to show date in stdout logs.

When enabled, stdout logs will include the full date (YYYY-MM-DD). When disabled, only the time (HH:MM:SS) will be shown. Note: Log files always include the full date regardless of this setting.

Default: false

Examples found in repository?
examples/basic_usage.rs (line 24)
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}
Source

pub fn use_colors(self, use_colors: bool) -> Self

Set whether to use colors in stdout logs.

When enabled, different log levels will be displayed in different colors:

  • ERROR: Red
  • WARN: Yellow
  • INFO: Blue
  • DEBUG: Green
  • TRACE: Default terminal color

Note: Log files never include color codes regardless of this setting.

Default: true

Examples found in repository?
examples/stdout_only.rs (line 18)
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
Hide additional examples
examples/basic_usage.rs (line 25)
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}
Source

pub fn level(self, level: LevelFilter) -> Self

Set the minimum log level to display.

This filters log messages based on their level:

  • Error: Only errors
  • Warn: Errors and warnings
  • Info: Errors, warnings, and info
  • Debug: Errors, warnings, info, and debug
  • Trace: All log levels

Default: Info

Examples found in repository?
examples/stdout_only.rs (line 16)
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
Hide additional examples
examples/basic_usage.rs (line 22)
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}
Source

pub fn build(self) -> LoggerConfig

Build the final configuration.

This consumes the builder and returns a LoggerConfig.

Examples found in repository?
examples/stdout_only.rs (line 19)
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
Hide additional examples
examples/basic_usage.rs (line 26)
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}

Trait Implementations§

Source§

impl Debug for LoggerConfigBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for LoggerConfigBuilder

Source§

fn default() -> LoggerConfigBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.