pub struct LogBufFile {
pub level: Level,
pub format: LogFormat,
pub file_path: Box<Path>,
pub flush_millis: usize,
pub rotation: Option<Rotation>,
pub flush_size: usize,
}Expand description
Config for buffered file sink which merged I/O and delay flush. Optional log rotation can be configured.
Used when you don’t have a SSD and the log is massive.
When your program shutting down, should call flush to ensure the log is written to disk.
log::logger().flush();On panic, our panic hook will call flush() explicitly.
flush size default to be 4k to prevent line breaks on program (graceful) restart.
§Example
Source of crate::recipe::buffered_file_logger_custom()
use captains_log::{*, rotation::*};
use std::path::{self, Path, PathBuf};
pub fn buffered_file_logger_custom<P: Into<PathBuf>>(
file_path: P, max_level: Level, time_fmt: &'static str, format_func: FormatFunc,
flush_millis: usize, rotate: Option<Rotation>,
) -> Builder {
let format = LogFormat::new(time_fmt, format_func);
let _file_path = file_path.into();
let p = path::absolute(&_file_path).expect("path convert to absolute");
let dir = p.parent().unwrap();
let file_name = Path::new(p.file_name().unwrap());
let mut file = LogBufFile::new(dir, file_name, max_level, format, flush_millis);
if let Some(ro) = rotate {
file = file.rotation(ro);
}
return Builder::default().signal(signal_hook::consts::SIGUSR1).add_sink(file);
}Fields§
§level: Levelmax log level in this file
format: LogFormat§file_path: Box<Path>path: dir/name
flush_millis: usizedefault to 0, means always flush when no more message to write.
when larger than zero, will wait for new message when timeout occur.
Max value is 1000 (1 sec).
rotation: Option<Rotation>Rotation config
flush_size: usizeAuto flush when buffer size is reached, default to be 4KB, so that during reload or graceful restart, the line will not be break.
Implementations§
Source§impl LogBufFile
impl LogBufFile
Sourcepub fn new<P1, P2>(
dir: P1,
file_name: P2,
level: Level,
format: LogFormat,
flush_millis: usize,
) -> Self
pub fn new<P1, P2>( dir: P1, file_name: P2, level: Level, format: LogFormat, flush_millis: usize, ) -> Self
Construct config for file sink with buffer.
Will try to create dir if not exists. Periodic flush if flush_millis is zero, or buffer size reaching 4096. will ensure a complete line write to the log file.
§Arguments:
The type of dir and file_name can be &str / String / &OsStr / OsString / Path / PathBuf. They can be of
different types.
-
flush_millis:-
default to 0, means always flush when no more message to write.
-
when larger than zero, will wait for new message when timeout occur. The max value is 1000 (1 sec).
-