pub struct Fmt<W, F>{ /* private fields */ }Expand description
A thread-safe logging Service for string-based output destinations.
Fmt service implements the Service trait for types that satisfy std::fmt::Write.
It is ideal for in-memory logging, testing, or targets that do not use byte-oriented I/O.
§Thread Safety
The internal FmtWriteData is wrapped in a Mutex. This allows the Service
to be shared across threads (Send + Sync), while ensuring that only one thread
can perform a work operation at a time.
Implementations§
Source§impl<W, F> Fmt<W, F>
impl<W, F> Fmt<W, F>
Sourcepub fn new(writer: W) -> Box<Self>
pub fn new(writer: W) -> Box<Self>
Creates a new, heap-allocated Fmt service.
§Parameters
writer: An object implementingstd::fmt::Write.formatter: An object implementingMessageFormatter.
§Example
let service = FmtWrite::<String, StandardMessageFormatter>::new(String::new());
let logger = QueuedLogger::new(service, 3, 4); // 3 retries, 4 worker threads
let logger = Logger::new(logger);Sourcepub fn with_formatter(writer: W, formatter: F) -> Box<Self>
pub fn with_formatter(writer: W, formatter: F) -> Box<Self>
Creates a new, heap-allocated Fmt service with a custom formatter.
§Parameters
writer: An object implementingstd::fmt::Write.formatter: An object implementingMessageFormatter.
§Example
let service = FmtWrite::<String, StandardMessageFormatter>::new(String::new());
let logger = QueuedLogger::new(service, 3, 4); // 3 retries, 4 worker threads
let logger = Logger::new(logger);Sourcepub fn inspect_writer<R>(&self, f: impl FnOnce(&W) -> R) -> Option<R>
pub fn inspect_writer<R>(&self, f: impl FnOnce(&W) -> R) -> Option<R>
Allows safe, read-only access to the internal buffer without stopping the logger.
Use this to “peek” at logs while the application is still running—perfect for health-check endpoints that expose recent logs or for verifying state in tests.
§Thread Safety
This method acquires a mutex lock. While the closure f is executing, any
incoming logs from other threads will block until the closure returns.
Keep the logic inside the closure as fast as possible.
§Returns
Sourcepub fn recover_writer(self) -> Result<W, ServiceError>
pub fn recover_writer(self) -> Result<W, ServiceError>
Destroys the Service and reclaims ownership of the underlying buffer or writer.
Use this at the end of a program, a test case, or a lifecycle stage to extract
all recorded logs and free up the resources used by the Service.
§Ownership & Lifecycle
This method consumes self, meaning the Fmt service can no longer be
used after this call. This is the only way to get full, non-cloned ownership
of the internal writer (e.g., a String or Vec<u8>).
§Guarantees
Because this takes ownership of the Service, it is compile-time guaranteed
that no other threads can be writing to the buffer when this is called.
Sourcepub fn clear_writer(&self)where
W: Default,
pub fn clear_writer(&self)where
W: Default,
Clears the underlying writer if the type supports it (e.g., String).
Useful for reusing the Service in benchmarks or test suites.
Trait Implementations§
Source§impl<W, F> Service for Fmt<W, F>
impl<W, F> Service for Fmt<W, F>
Source§fn status(&self) -> LoggerStatus
fn status(&self) -> LoggerStatus
Returns the current operational status.
Currently always returns LoggerStatus::Running.
Source§fn work(&self, msg: &Message) -> Result<(), ServiceError>
fn work(&self, msg: &Message) -> Result<(), ServiceError>
Acquires a lock and writes a formatted Message to the internal writer.
This method uses MessageFormatter::format_fmt to perform the write.
§Errors
- Returns
ServiceError::LockPoisonedif the internal mutex is poisoned. - Forwards any
ServiceErrorreturned by the formatter.
Auto Trait Implementations§
impl<W, F> !Freeze for Fmt<W, F>
impl<W, F> RefUnwindSafe for Fmt<W, F>
impl<W, F> Send for Fmt<W, F>
impl<W, F> Sync for Fmt<W, F>
impl<W, F> Unpin for Fmt<W, F>
impl<W, F> UnwindSafe for Fmt<W, F>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more