pub struct LoggingBuilder { /* private fields */ }Expand description
Builder for constructing a tracing subscriber with flexible configuration
This builder provides a fluent API for configuring logging with different formats and output targets. It eliminates code duplication by centralizing layer creation and subscriber initialization.
Supports independent format control for file and stderr outputs, with automatic ANSI code handling (disabled for files, enabled for stderr).
§Examples
use std::path::Path;
use torrust_tracker_deployer_lib::bootstrap::logging::{LogOutput, LogFormat, LoggingBuilder};
// Basic usage with defaults (Compact file format, Pretty stderr format, FileAndStderr output)
LoggingBuilder::new(Path::new("./data/logs")).init();
// Custom configuration with independent formats
LoggingBuilder::new(Path::new("./data/logs"))
.with_file_format(LogFormat::Json)
.with_stderr_format(LogFormat::Pretty)
.with_output(LogOutput::FileAndStderr)
.init();
// Backward compatible with single format for both outputs
LoggingBuilder::new(Path::new("./data/logs"))
.with_format(LogFormat::Compact)
.with_output(LogOutput::FileOnly)
.init();Implementations§
Source§impl LoggingBuilder
impl LoggingBuilder
Sourcepub fn new(log_dir: &Path) -> Self
pub fn new(log_dir: &Path) -> Self
Create a new logging builder with default settings
Default configuration:
- File Format:
LogFormat::Compact(no ANSI codes) - Stderr Format:
LogFormat::Pretty(with ANSI codes) - Output:
LogOutput::FileAndStderr
§Arguments
log_dir- Directory where log files should be written (e.g.,./data/logs)
Sourcepub fn with_format(self, format: LogFormat) -> Self
pub fn with_format(self, format: LogFormat) -> Self
Set the logging format for both file and stderr outputs
This is a convenience method for backward compatibility.
For independent format control, use with_file_format() and with_stderr_format().
§Arguments
format- The desired logging format (Pretty, Json, or Compact)
Sourcepub fn with_file_format(self, format: LogFormat) -> Self
pub fn with_file_format(self, format: LogFormat) -> Self
Set the logging format for file output
ANSI codes are automatically disabled for file output to ensure logs are easily parsed with standard text tools (grep, awk, sed).
§Arguments
format- The desired logging format for files (Pretty, Json, or Compact)
Sourcepub fn with_stderr_format(self, format: LogFormat) -> Self
pub fn with_stderr_format(self, format: LogFormat) -> Self
Set the logging format for stderr output
ANSI codes are automatically enabled for stderr output to provide colored terminal output for better readability.
§Arguments
format- The desired logging format for stderr (Pretty, Json, or Compact)
Sourcepub fn with_output(self, output: LogOutput) -> Self
pub fn with_output(self, output: LogOutput) -> Self
Sourcepub fn init(self)
pub fn init(self)
Initialize the global tracing subscriber with the configured settings
This consumes the builder and sets up the global logging infrastructure.
After calling this, all logging macros (tracing::info!, etc.) will use
this configuration.
§Panics
Panics if:
- Log directory cannot be created (filesystem permissions issue)
- Subscriber initialization fails (usually means it was already initialized)
Both panics are intentional as logging is critical for observability.
Auto Trait Implementations§
impl Freeze for LoggingBuilder
impl RefUnwindSafe for LoggingBuilder
impl Send for LoggingBuilder
impl Sync for LoggingBuilder
impl Unpin for LoggingBuilder
impl UnsafeUnpin for LoggingBuilder
impl UnwindSafe for LoggingBuilder
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request