logged-stream
Table of contents
Description
logged-stream is a Rust library that provides a wrapper LoggedStream for structures which implements std::io::Write and std::io::Read traits or their asynchronous analogues from tokio to enable logging of all read and write operations, errors and drop.
LoggedStream structure constructs from three parts:
- Underlying IO object, which implements
std::io::Writeandstd::io::Readtraits or their asynchronous analogues fromtokio:tokio::io::AsyncReadandtokio::io::AsyncWrite. - Buffer formatter, which implements
BufferFormattertrait provided by this library. This part ofLoggedStreamis responsible for the form you will see the input and output bytes. Currently this library provides the following implementations ofBufferFormattertrait:HexDecimalFormatter,DecimalFormatter,BinaryFormatterandOctalFormatter. AlsoBufferFormatteris public trait so you are free to construct your own implementation. - Logger, which implements
Loggertrait provided by this library. This part ofLoggedStreamis responsible for further work with constructed and formatter log record. For example, it can be outputted to console, written to the file, written to database, written to the memory for further use or sended by the channel. Currently this library provides the following implementations ofLoggertrait:ConsoleLogger,MemoryStorageLoggerandChannelLogger. AlsoLoggeris public trait and you are free to construct you own implementation.
Usage
Add this to your Cargo.toml:
[]
= "0.1"
or run command in your project root:
$ cargo add logged-stream@0.1
Example
This is a simple usage example of LoggedStream with underling std::net::TcpStream which connects to some echo-server, hex decimal formatter and console logger.
Output:
2023-04-12 20:06:04.756014 > 01:02:03:04
2023-04-12 20:06:04.756098 < 01:02:03:04
2023-04-12 20:06:04.756170 > 05:06:07:08
2023-04-12 20:06:04.756275 < 05:06:07:08
2023-04-12 20:06:04.756372 > 09:0a:0b:0c
2023-04-12 20:06:04.756514 < 09:0a:0b:0c
2023-04-12 20:06:04.756593 > 01:02:03:04
2023-04-12 20:06:04.756820 < 01:02:03:04
2023-04-12 20:06:04.756878 x Connection socket deallocated.
Full version of this example can be found there.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.