Expand description
§Redis Logger
This module provides a logger implementation that logs messages to Redis using the log
crate’s Log
trait.
§RedisLogger
RedisLogger
is the main struct in this module. It implements the Log
trait, which allows it to be used as a logger in
applications that use the log
crate. It logs messages to Redis,
either by publishing them to any number of pub/sub channels or by adding them to streams or both.
§RedisLoggerConfig
RedisLoggerConfig
is a struct that holds the configuration for a RedisLogger
.
It includes a Redis connection, a list of pub/sub channels and/or a list of streams to log to,
along with encoders for the messages.
§RedisLoggerConfigBuilder
RedisLoggerConfigBuilder
is a builder for RedisLoggerConfig
. It provides a fluent interface for building a RedisLoggerConfig
.
§PubSubEncoder
and StreamEncoder
PubSubEncoder
and StreamEncoder
are traits for encoding log messages.
They are used by RedisLogger
to encode the messages before sending them to Redis.
The module provides default implementations of these traits when the feature default_encoders
is enabled,
but users can also provide their own implementations.
§Usage
To use this logger, you need to create a RedisLoggerConfig
(using RedisLoggerConfigBuilder
), create a RedisLogger
with the config,
either by calling ::new
or ::init
, the latter of which also sets the logger as the global logger.
We recommend using this logger with the parallel_logger
crate to avoid blocking the main thread when logging to Redis.
§Example
This example shows how to implement a PubSubEncoder
that encodes log messages as a byte vector using the bincode
crate. It also
shows how to configure RedisLogger
to use this encoder while being part of multiple loggers that run on a separate thread using parallel_logger
.
struct BincodeRedisEncoder;
impl PubSubEncoder for BincodeRedisEncoder {
fn encode(&self, record: &log::Record) -> Vec<u8> {
let mut slice = [0u8; 2000];
let message = SerializableLogRecord::from(record);
let size = bincode::encode_into_slice(message, &mut slice, BINCODE_CONFIG).unwrap();
let slice = &slice[..size];
slice.to_vec()
}
}
fn main() {
ParallelLogger::init(
log::LevelFilter::Debug,
ParallelMode::Sequential,
vec![
FileLogger::new(LevelFilter::Debug, "log_file.log"),
TerminalLogger::new(LevelFilter::Info),
RedisLogger::new(
LevelFilter::Debug,
RedisLoggerConfigBuilder::with_pubsub(
REDIS_URL.to_string(),
vec!["logging".into()],
BincodeRedisEncoder {},
).build(),
),
],
);
}
Using RedisLogger::init
insted of RedisLogger::new
would allow the logger to be used as the only global logger.
§Features
This module has a feature flag default_encoders
that, when enabled, provides default implementations
of PubSubEncoder
and StreamEncoder
that encode the log messages as JSON or as a vector of tuples, respectively.
Another feature flag shared_logger
implements the simplelog::SharedLogger
trait for RedisLogger
. This enables use in a simplelog::CombinedLogger
.
Structs§
- Default
PubSub Encoder default_encoders
DefaultPubSubEncoder
is a default implementation of thePubSubEncoder
trait. It encodes alog::Record
into a JSON object, where each field in theRecord
becomes a key-value pair in the JSON object. The JSON object is then converted into a byte vector.- Default
Stream Encoder default_encoders
DefaultStreamEncoder
is a default implementation of theStreamEncoder
trait. It encodes alog::Record
into a vector of tuples, where each tuple contains a field name from theRecord
and the corresponding value as a byte vector. If a field in theRecord
isNone
, the byte vector is empty.- Redis
Logger - A logger that logs messages to Redis.
- Redis
Logger Config - Configuration for the Redis logger. Pass to
RedisLogger
to configure the logger. - Redis
Logger Config Builder RedisLoggerConfigBuilder
is a builder forRedisLoggerConfig
.- Redis
Logger Config Temp
Traits§
- PubSub
Encoder - Trait for encoding log messages to be published to a pub/sub channel.
- Stream
Encoder - Trait for encoding log messages to be added to a Redis stream.