Skip to main content

FileLogger

Struct FileLogger 

Source
pub struct FileLogger { /* private fields */ }
Expand description

Logger implementation that writes log records into the provided file.

This implementation of the Logger trait writes log records (Record) into a file, one line per record, in the form [timestamp] {kind} {message}.

Optionally, a prefix can be configured via with_prefix or set_prefix. When set, it is written verbatim immediately before the record kind character — that is, after the timestamp — which mirrors how ConsoleLogger renders its prefix relative to the timestamp emitted by the logging backend. This is useful to disambiguate output when several LoggedStreams (for example one per connection) write to the same file. No prefix is configured by default.

§Sharing one file between several loggers

Each record is rendered up front and written with a single write_all call, so concurrent loggers never interleave parts of a line. For that to hold, every logger must write to a file opened in append mode — either construct them with open, or share one handle with fs::File::try_clone. Handing several loggers independently opened non-append files (for example from fs::File::create) gives each of them its own starting offset, and they will silently overwrite each other’s records.

Implementations§

Source§

impl FileLogger

Source

pub fn new(file: File) -> Self

Construct a new instance of FileLogger using the provided file. The constructed logger has no prefix; use with_prefix or set_prefix to add one.

If the same file is going to be written by several loggers, it must be opened in append mode; prefer open, which does that for you.

Source

pub fn open(path: impl AsRef<Path>) -> Result<Self>

Construct a new instance of FileLogger writing to the file at the provided path, creating the file if it does not exist and opening it in append mode.

Append mode is what makes it safe for several loggers — for example one per connection, each with its own prefix — to write to the same file concurrently without overwriting each other. Returns an Err if the file could not be opened.

§Examples
use logged_stream::FileLogger;

let path = std::env::temp_dir().join("logged-stream-open-doctest.log");
let logger = FileLogger::open(&path)?;
Source

pub fn with_prefix(self, prefix: impl Into<Cow<'static, str>>) -> Self

Set a prefix that will be written before the record kind character of every line produced by this logger, and return the modified logger. This is a chainable builder method.

The prefix is written verbatim between the timestamp and the record kind character — no separator is inserted between the prefix and the kind — so include any trailing separator you want yourself (for example a trailing space or brackets). An empty prefix therefore produces the same output as no prefix at all.

§Examples
use logged_stream::FileLogger;

let path = std::env::temp_dir().join("logged-stream-with-prefix-doctest.log");
let logger = FileLogger::open(&path)?.with_prefix("[conn 5] ");
assert_eq!(logger.prefix(), Some("[conn 5] "));
Source

pub fn set_prefix(&mut self, prefix: impl Into<Cow<'static, str>>)

Set or replace the prefix written before the record kind character of every line produced by this logger, in place. See with_prefix for details on how the prefix is rendered.

Source

pub fn clear_prefix(&mut self)

Remove the configured prefix, so lines are written without any prefix again.

Source

pub fn prefix(&self) -> Option<&str>

Return the currently configured prefix, or None if no prefix is set.

Trait Implementations§

Source§

impl Debug for FileLogger

Source§

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

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

impl Logger for FileLogger

Source§

fn log(&mut self, record: Record)

Source§

impl Logger for Box<FileLogger>

Source§

fn log(&mut self, record: Record)

Auto Trait Implementations§

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.