Struct LogRollerBuilder

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

Provides a fluent interface for configuring LogRoller instances.

The builder pattern allows for flexible configuration of log rolling behavior, with sensible defaults that can be overridden as needed. Configuration options include:

  • Time zone - Control when rotations occur
  • Rotation strategy - Choose between size-based or time-based rotation
  • Compression - Optionally compress rotated files to save space
  • File retention - Limit the number of historical log files to keep
  • File naming - Add custom suffixes to help identify different log types
  • Permissions - Set specific file permissions (Unix systems only)

§Default Configuration

If not explicitly configured, LogRoller uses these defaults:

  • Daily rotation at midnight
  • Local system time zone
  • No compression
  • Keep all historical files
  • Standard file permissions

§Examples

Basic configuration for daily log rotation:

use logroller::{LogRollerBuilder, Rotation, RotationAge};

let appender = LogRollerBuilder::new("./logs", "app.log")
    .rotation(Rotation::AgeBased(RotationAge::Daily))
    .build()
    .unwrap();

Advanced configuration with multiple options:

use logroller::{Compression, LogRollerBuilder, Rotation, RotationAge, TimeZone};

let appender = LogRollerBuilder::new("./logs", "app.log")
    .rotation(Rotation::AgeBased(RotationAge::Hourly))
    .time_zone(TimeZone::UTC)  // Use UTC for consistent timing
    .max_keep_files(24)        // Keep one day's worth of hourly logs
    .compression(Compression::Gzip)  // Compress old logs
    // .compression(Compression::XZ(6))  // Compress using XZ. Requires `xz` feature.
    .suffix("error".to_string())  // Name format: app.log.2025-04-01-19.error
    .build()
    .unwrap();

Size-based rotation for large log files:

use logroller::{LogRollerBuilder, Rotation, RotationSize};

let appender = LogRollerBuilder::new("./logs", "large_app.log")
    .rotation(Rotation::SizeBased(RotationSize::MB(100)))  // Rotate at 100MB
    .max_keep_files(5)  // Keep only the 5 most recent files
    .build()
    .unwrap();

Implementations§

Source§

impl LogRollerBuilder

Source

pub fn new<P: AsRef<Path>>(directory: P, filename: P) -> Self

Create a new log roller builder.

§Arguments
  • directory - The directory where the log files are stored.
  • filename - The name of the log file.
Source

pub fn time_zone(self, time_zone: TimeZone) -> Self

Set the time zone for the log files.

Source

pub fn rotation(self, rotation: Rotation) -> Self

Set the rotation type for the log files.

Source

pub fn compression(self, compression: Compression) -> Self

Set the compression type for the log files.

Source

pub fn max_keep_files(self, max_keep_files: u64) -> Self

Set the maximum number of log files to keep.

Source

pub fn suffix(self, suffix: String) -> Self

Set the suffix for the log file.

Source

pub fn file_mode(self, mode: u32) -> Self

Set the file permissions for log files (Unix-like systems only). This sets the file mode bits in octal notation like when using chmod. For example, 0o644 for rw-r–r– permissions.

Source

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

Determines whether the application should attempt a graceful shutdown. When set to true, the application will perform cleanup operations and allow in-progress tasks (like file compression and old file cleanup) to complete before shutting down. If set to false, the application may terminate immediately without waiting for these ongoing tasks.

Compression Corruption Risk: If graceful_shutdown is false and the application exits while a compression thread is still writing a compressed file, the resulting file may be incomplete or corrupted. Setting this to true ensures that all compression operations finish, but may cause a slight delay during application shutdown.

By default, this is set to false.

Source

pub fn build(self) -> Result<LogRoller, LogRollerError>

Build the log roller.

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.