Skip to main content

FileSink

Struct FileSink 

Source
pub struct FileSink<F: Format> { /* private fields */ }
Available on crate feature 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:

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 returning

Implementations§

Source§

impl<F: Format> FileSink<F>

Source

pub fn append<P: AsRef<Path>>(path: P, format: F) -> Result<Self>

Open path for appending (creating it if needed) with default options.

§Errors

Returns Error::Io if the file cannot be opened or created.

Source

pub fn create<P: AsRef<Path>>(path: P, format: F) -> Result<Self>

Open path for writing, truncating any existing content.

The file is truncated only after the checks described under FileSinkBuilder::follow_symlinks pass, so a symbolic link at path never truncates the file it points to.

§Errors

Returns Error::Io if the file cannot be opened or created, or if path is a symbolic link or not a regular file.

Source

pub fn builder<P: AsRef<Path>>(path: P, format: F) -> FileSinkBuilder<F>

Start configuring a file sink. Defaults: append, 8 KiB buffer, no fsync, mode 0o600 for a new file (Unix), symbolic links refused.

Trait Implementations§

Source§

impl<F: Format + Debug> Debug for FileSink<F>

Source§

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

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

impl<F: Format> Sink for FileSink<F>

Source§

fn write_record(&self, record: &Record<'_>) -> Result<()>

Serialize and deliver one record. Read more
Source§

fn flush(&self) -> Result<()>

Push any buffered bytes to the destination (for files, to the operating system; see the sink’s own docs for fsync). Read more
Source§

fn request_flush(&self) -> Result<()>

Flush triggered by a record at or above the logger’s flush level (see crate::LoggerBuilder::flush_on). Read more
Source§

fn shutdown(&self) -> Result<()>

Flush and release background resources. Called by crate::Logger::shutdown, by a crate::FlushGuard on drop, and when the last clone of a crate::Logger is dropped. Read more

Auto Trait Implementations§

§

impl<F> !Freeze for FileSink<F>

§

impl<F> Forget for FileSink<F>

§

impl<F> Move for FileSink<F>

§

impl<F> RefUnwindSafe for FileSink<F>
where F: RefUnwindSafe,

§

impl<F> Send for FileSink<F>

§

impl<F> Sync for FileSink<F>

§

impl<F> Unpin for FileSink<F>
where F: Unpin,

§

impl<F> UnsafeUnpin for FileSink<F>
where F: UnsafeUnpin,

§

impl<F> UnwindSafe for FileSink<F>
where F: UnwindSafe,

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.