Skip to main content

Crate logged_stream

Crate logged_stream 

Source
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. LoggedStream implements the same IO trait S does, so it slots in wherever S was 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.

FormatterRenders each byte as
LowercaseHexadecimalFormatterlowercase hexadecimal — 0a:ff
UppercaseHexadecimalFormatteruppercase hexadecimal — 0A:FF
DecimalFormatterdecimal — 10:255
OctalFormatteroctal — 012:377
BinaryFormatterbinary — 00001010:11111111

§Filters (RecordFilter)

Decide which records reach the logger.

FilterBehavior
DefaultFilterAccepts every record.
RecordKindFilterAccepts only the record kinds in an allow-list given at construction.
AllFilterAND — a record passes only if every child filter accepts it (an empty list accepts everything).
AnyFilterOR — a record passes if any child filter accepts it (an empty list rejects everything).

§Loggers (Logger)

Consume each accepted record.

LoggerDestination
ConsoleLoggerEmits records through the log facade.
FileLoggerWrites records to a file.
MemoryStorageLoggerRetains recent records in a bounded in-memory buffer.
ChannelLoggerSends records over an mpsc channel for handling elsewhere.

Structs§

AllFilter
Implementation of RecordFilter that combines multiple filters with AND logic.
AnyFilter
Implementation of RecordFilter that combines multiple filters with OR logic.
BinaryFormatter
This implementation of BufferFormatter trait formats provided bytes buffer in binary number system.
ChannelLogger
Logger implementation that sends log records via an asynchronous channel.
ConsoleLogger
Logger implementation that writes log records to the console.
DecimalFormatter
This implementation of BufferFormatter trait formats provided bytes buffer in decimal number system.
DefaultFilter
This is default implementation of RecordFilter trait which check method always return true. It should be constructed using Default::default method.
FileLogger
This implementation of Logger trait writes log records (Record) into provided file.
LoggedStream
Wrapper for an IO object that logs every read, write, error, shutdown and drop that passes through it.
LowercaseHexadecimalFormatter
This implementation of BufferFormatter trait formats provided bytes buffer in hexadecimal number system.
MemoryStorageLogger
Logger implementation that writes log records to an inner VecDeque collection.
OctalFormatter
This implementation of BufferFormatter trait 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).
RecordKindFilter
Implementation of RecordFilter that accepts allowed RecordKind array.
UppercaseHexadecimalFormatter
This implementation of BufferFormatter trait formats provided bytes buffer in hexadecimal number system.

Enums§

RecordKind
This enumeration represents log record kind. It is contained inside Record and helps to determine how to work with log record message content which is different for each log record kind.

Traits§

BufferFormatter
This trait allows to format bytes buffer using format_buffer method. It should be implemented for structures which are going to be used as formatting part inside LoggedStream.
Logger
Trait for processing log records in LoggedStream.
RecordFilter
Trait for filtering log records in LoggedStream.