redis_logger

Crates.ioredis_logger
lib.rsredis_logger
version
sourcesrc
created_at2024-02-16 15:06:59.495908
updated_at2025-02-11 15:00:00.507647
descriptionA logger implementing the log::Log trait that writes log messages to a Redis pub/sub channel, a stream or both using the redis_rs crate
homepage
repositoryhttps://github.com/8192K/redis_logger
max_upload_size
id1142523
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
(8192K)

documentation

README

redis_logger

Crates.io Docs MIT/APACHE-2.0

This log::Log implementation allows to log to a Redis server. It supports writing to any number of pub/sub channels or streams or both.

You can specify custom encoders for pub/sub and stream log messages. Using the default_encoders feature default implementations for the encoders are available. This feature is disabled by default.

If you enable the shared_logger feature you can use the RedisLogger inside a simplelog::CombinedLogger.

Usage

Add the dependency to your Cargo.toml:

[dependencies]
log = "0.4"
redis_logger = "0.4"

How to use in your application:

Build a RedisLoggerConfig using the RedisLoggerConfigBuilder methods. Specify a connection and at least one pub/sub or stream channel. Use this configuration to either instantiate a RedisLogger instance with RedisLogger::new if you wish to use this logger with other loggers (like the parallel_logger crate or CombinedLogger logger from the simplelog crate) or use the RedisLogger::init method to initialize the logger as the only logger for the application.

A simple example using the default_encoders feature and setting the RedisLogger as the only logger would look like this:

fn main() {
    RedisLogger::init(
        LevelFilter::Debug,
        RedisLoggerConfigBuilder::with_pubsub_default(
            REDIS_URL.to_string(),
            vec!["logging".into()],
        ).build(),
    );
}

This broader example uses RedisLogger inside a ParallelLogger and encodes messages for pub/sub using the bincode crate and a custom PubSubEncoder:

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(),
            ),
        ],
    );
}

Roadmap

  • Support atomic pipelines when calling Redis.

License

Licensed under either of

Commit count: 22

cargo fmt