Crate redis_logger

source ·
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() {
    let redis_client = redis::Client::open(REDIS_URL).unwrap();
    let redis_connection = redis_client.get_connection().unwrap();
 
    ParallelLogger::init(
        log::LevelFilter::Debug,
        ParallelMode::Sequential,
        vec![
            FileLogger::new(LevelFilter::Debug, "log_file.log"),
            TerminalLogger::new(LevelFilter::Info),
            RedisLogger::new(
                LevelFilter::Debug,
                RedisLoggerConfigBuilder::build_with_pubsub(
                    redis_connection,
                    vec!["logging".into()],
                    BincodeRedisEncoder {},
                ),
            ),
        ],
    );
}

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§

  • DefaultPubSubEncoderdefault_encoders
    DefaultPubSubEncoder is a default implementation of the PubSubEncoder trait. It encodes a log::Record into a JSON object, where each field in the Record becomes a key-value pair in the JSON object. The JSON object is then converted into a byte vector.
  • DefaultStreamEncoderdefault_encoders
    DefaultStreamEncoder is a default implementation of the StreamEncoder trait. It encodes a log::Record into a vector of tuples, where each tuple contains a field name from the Record and the corresponding value as a byte vector. If a field in the Record is None, the byte vector is empty.
  • A logger that logs messages to Redis.
  • Configuration for the Redis logger. Pass to RedisLogger to configure the logger.
  • RedisLoggerConfigBuilder is a builder for RedisLoggerConfig.

Traits§

  • Trait for encoding log messages to be published to a pub/sub channel.
  • Trait for encoding log messages to be added to a Redis stream.