Expand description
logged-stream provides a single wrapper type, LoggedStream, that wraps any underlying IO
object and logs every read, write, error, shutdown and drop that passes through it. The wrapper
re-implements the same IO trait it wraps — Read / Write, or the tokio asynchronous
analogues AsyncRead / AsyncWrite — so it is a drop-in replacement for the stream it
decorates and works transparently in both synchronous and asynchronous code.
§Architecture
LoggedStream is generic over four independent, pluggable parts. Each logged event flows
through them in order: event -> Formatter -> Filter -> Logger.
- The inner IO object (
S). The stream you are wrapping.LoggedStreamimplements the same IO traitSdoes, so it slots in whereverSwas used. - Formatter (
BufferFormatter). Turns the read and written byte buffers into the display strings you see in the log. - Filter (
RecordFilter). Decides which records are logged. It runs on every record kind, including shutdown and drop. - Logger (
Logger). The sink that consumes accepted records.
All three of BufferFormatter, RecordFilter and Logger are public, Send + 'static
and object-safe, with blanket implementations for Box<...> (and Arc<T> where T: Sync for
BufferFormatter). You are free to supply your own implementation of any part.
§Provided implementations
§Formatters (BufferFormatter)
Control how byte buffers are rendered. Each formatter stores a separator (default :) and
exposes parallel constructors: new, new_static, new_owned and new_default.
| Formatter | Renders each byte as |
|---|---|
LowercaseHexadecimalFormatter | lowercase hexadecimal — 0a:ff |
UppercaseHexadecimalFormatter | uppercase hexadecimal — 0A:FF |
DecimalFormatter | decimal — 10:255 |
OctalFormatter | octal — 012:377 |
BinaryFormatter | binary — 00001010:11111111 |
§Filters (RecordFilter)
Decide which records reach the logger.
| Filter | Behavior |
|---|---|
DefaultFilter | Accepts every record. |
RecordKindFilter | Accepts only the record kinds in an allow-list given at construction. |
AllFilter | AND — a record passes only if every child filter accepts it (an empty list accepts everything). |
AnyFilter | OR — a record passes if any child filter accepts it (an empty list rejects everything). |
§Loggers (Logger)
Consume each accepted record.
| Logger | Destination |
|---|---|
ConsoleLogger | Emits records through the log facade. |
FileLogger | Writes records to a file. |
MemoryStorageLogger | Retains recent records in a bounded in-memory buffer. |
ChannelLogger | Sends records over an mpsc channel for handling elsewhere. |
Structs§
- AllFilter
- Implementation of
RecordFilterthat combines multiple filters with AND logic. - AnyFilter
- Implementation of
RecordFilterthat combines multiple filters with OR logic. - Binary
Formatter - This implementation of
BufferFormattertrait formats provided bytes buffer in binary number system. - Channel
Logger - Logger implementation that sends log records via an asynchronous channel.
- Console
Logger - Logger implementation that writes log records to the console.
- Decimal
Formatter - This implementation of
BufferFormattertrait formats provided bytes buffer in decimal number system. - Default
Filter - This is default implementation of
RecordFiltertrait whichcheckmethod always returntrue. It should be constructed usingDefault::defaultmethod. - File
Logger - This implementation of
Loggertrait writes log records (Record) into provided file. - Logged
Stream - Wrapper for an IO object that logs every read, write, error, shutdown and drop that passes through it.
- Lowercase
Hexadecimal Formatter - This implementation of
BufferFormattertrait formats provided bytes buffer in hexadecimal number system. - Memory
Storage Logger - Logger implementation that writes log records to an inner
VecDequecollection. - Octal
Formatter - This implementation of
BufferFormattertrait formats provided bytes buffer in octal number system. - Record
- This structure represents a log record and contains message string, creation timestamp (
DateTime<Utc>) and record kind (RecordKind). - Record
Kind Filter - Implementation of
RecordFilterthat accepts allowedRecordKindarray. - Uppercase
Hexadecimal Formatter - This implementation of
BufferFormattertrait formats provided bytes buffer in hexadecimal number system.
Enums§
- Record
Kind - This enumeration represents log record kind. It is contained inside
Recordand helps to determine how to work with log record message content which is different for each log record kind.
Traits§
- Buffer
Formatter - This trait allows to format bytes buffer using
format_buffermethod. It should be implemented for structures which are going to be used as formatting part insideLoggedStream. - Logger
- Trait for processing log records in
LoggedStream. - Record
Filter - Trait for filtering log records in
LoggedStream.