Struct fern::DateBased

source ·
pub struct DateBased { /* private fields */ }
Expand description

This is used to generate log file suffixed based on date, hour, and minute.

The log file will be rotated automatically when the date changes.

Implementations§

source§

impl DateBased

source

pub fn new<T, U>(file_prefix: T, file_suffix: U) -> Selfwhere T: AsRef<Path>, U: Into<Cow<'static, str>>,

Create new date-based file logger with the given file prefix and strftime-based suffix pattern.

On initialization, fern will create a file with the suffix formatted with the current time (either utc or local, see below). Each time a record is logged, the format is checked against the current time, and if the time has changed, the old file is closed and a new one opened.

file_suffix will be interpreted as an strftime format. See chrono::format::strftime for more information.

file_prefix may be a full file path, and will be prepended to the suffix to create the final file.

Note that no separator will be placed in between file_name and file_suffix_pattern. So if you call DateBased::new("hello", "%Y"), the result will be a filepath hello2019.

By default, this will use local time. For UTC time instead, use the .utc_time() method after creating.

By default, this will use \n as a line separator. For a custom separator, use the .line_sep method after creating.

Examples

Containing the date (year, month and day):

// logs/2019-10-23-my-program.log
let log = fern::DateBased::new("logs/", "%Y-%m-%d-my-program.log");

// program.log.23102019
let log = fern::DateBased::new("my-program.log.", "%d%m%Y");

Containing the hour:

// logs/2019-10-23 13 my-program.log
let log = fern::DateBased::new("logs/", "%Y-%m-%d %H my-program.log");

// program.log.2310201913
let log = fern::DateBased::new("my-program.log.", "%d%m%Y%H");

Containing the minute:

// logs/2019-10-23 13 my-program.log
let log = fern::DateBased::new("logs/", "%Y-%m-%d %H my-program.log");

// program.log.2310201913
let log = fern::DateBased::new("my-program.log.", "%d%m%Y%H");

UNIX time, or seconds since 00:00 Jan 1st 1970:

// logs/1571822854-my-program.log
let log = fern::DateBased::new("logs/", "%s-my-program.log");

// program.log.1571822854
let log = fern::DateBased::new("my-program.log.", "%s");

Hourly, using UTC time:

// logs/2019-10-23 23 my-program.log
let log = fern::DateBased::new("logs/", "%Y-%m-%d %H my-program.log").utc_time();

// program.log.2310201923
let log = fern::DateBased::new("my-program.log.", "%d%m%Y%H").utc_time();
source

pub fn line_sep<T>(self, line_sep: T) -> Selfwhere T: Into<Cow<'static, str>>,

Changes the line separator this logger will use.

The default line separator is \n.

Examples

Using a windows line separator:

let log = fern::DateBased::new("logs", "%s.log").line_sep("\r\n");
source

pub fn utc_time(self) -> Self

Orients this log file suffix formatting to use UTC time.

The default is local time.

Examples

This will use UTC time to determine the date:

// program.log.2310201923
let log = fern::DateBased::new("my-program.log.", "%d%m%Y%H").utc_time();
source

pub fn local_time(self) -> Self

Orients this log file suffix formatting to use local time.

This is the default option.

Examples

This log file will use local time - the latter method call overrides the former.

// program.log.2310201923
let log = fern::DateBased::new("my-program.log.", "%d%m%Y%H")
    .utc_time()
    .local_time();

Trait Implementations§

source§

impl Debug for DateBased

source§

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

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

impl From<DateBased> for Output

source§

fn from(config: DateBased) -> Self

Create an output logger which defers to the given date-based logger. Use configuration methods on DateBased to set line separator and filename.

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.