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
impl FileLogger
Sourcepub fn new(file: File) -> Self
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.
Sourcepub fn open(path: impl AsRef<Path>) -> Result<Self>
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)?;Sourcepub fn with_prefix(self, prefix: impl Into<Cow<'static, str>>) -> Self
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] "));Sourcepub fn set_prefix(&mut self, prefix: impl Into<Cow<'static, str>>)
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.
Sourcepub fn clear_prefix(&mut self)
pub fn clear_prefix(&mut self)
Remove the configured prefix, so lines are written without any prefix again.