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
impl DateBased
sourcepub fn new<T, U>(file_prefix: T, file_suffix: U) -> Selfwhere
T: AsRef<Path>,
U: Into<Cow<'static, str>>,
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();
sourcepub fn line_sep<T>(self, line_sep: T) -> Selfwhere
T: Into<Cow<'static, str>>,
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");
sourcepub fn utc_time(self) -> Self
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();
sourcepub fn local_time(self) -> Self
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();