pub struct FileSink<F: Format> { /* private fields */ }std only.Expand description
Writes records to a file through a write buffer.
§Durability
Records are buffered in memory (8 KiB by default) and reach the operating system when the buffer fills or the sink is flushed. A flush happens:
- after every record at or above the logger’s flush level (by
default
Level::Error, seeLoggerBuilder::flush_on); - on
crate::Logger::flushandcrate::Logger::shutdown; - when a
crate::FlushGuardis dropped; - when the last clone of the
crate::Loggeris dropped.
Anything still buffered is lost if the process ends without
one of those happening: std::process::abort, panic = "abort"
without crate::global::install_panic_hook, a fatal signal,
std::process::exit (which skips destructors), or simply returning
from main while the logger lives in a static (statics are never
dropped). Keep the crate::FlushGuard returned by
crate::init alive in main, call crate::Logger::shutdown
before process::exit, or build the sink with
FileSinkBuilder::buffer_capacity(0) to write every record
through immediately.
Flushing hands data to the OS, which survives a crash of this
process but not a power loss or kernel crash. Enable
FileSinkBuilder::sync_on_flush to also fsync (sync_data)
on every flush.
§Examples
use log_io::format::JsonFormat;
use log_io::sink::FileSink;
use log_io::{Level, Logger};
let sink = FileSink::builder("app.jsonl", JsonFormat::new())
.append(true)
.sync_on_flush(true)
.open()?;
let logger = Logger::builder().level(Level::Info).with_sink(sink).build();
log_io::error!(logger, "disk almost full"); // flushed and synced before returningImplementations§
Source§impl<F: Format> FileSink<F>
impl<F: Format> FileSink<F>
Trait Implementations§
Source§impl<F: Format> Sink for FileSink<F>
impl<F: Format> Sink for FileSink<F>
Source§fn write_record(&self, record: &Record<'_>) -> Result<()>
fn write_record(&self, record: &Record<'_>) -> Result<()>
Source§fn flush(&self) -> Result<()>
fn flush(&self) -> Result<()>
fsync). Read moreSource§fn request_flush(&self) -> Result<()>
fn request_flush(&self) -> Result<()>
crate::LoggerBuilder::flush_on). Read moreSource§fn shutdown(&self) -> Result<()>
fn shutdown(&self) -> Result<()>
crate::Logger::shutdown, by a crate::FlushGuard on drop,
and when the last clone of a crate::Logger is dropped. Read more