slog_mongodb

Crates.ioslog_mongodb
lib.rsslog_mongodb
version1.0.0
sourcesrc
created_at2020-03-29 15:14:16.873289
updated_at2020-03-29 15:14:16.873289
descriptionMongoDB drain for slog-rs
homepage
repositoryhttps://github.com/jwillbold/slog_mongodb
max_upload_size
id224092
size17,784
Johannes Willbold (jwillbold)

documentation

README

Build Status codecov

slog_mongodb

MongoDB extension for Rust's logging library slog-rs. Serializes slog messages to BSON documents and stores them in a MongoDB collection. To reduce the stress on the database, the logged messages are buffered and only sent in configurable time intervals.

Usage

use slog::*;

fn main() {
    let client = mongodb::Client::with_uri_str("mongodb://localhost:27017/").unwrap();
    let db = client.database("some_db");
    let logs = db.collection("logs");

    let drain = slog_mongodb::MongoDBDrain::new(logs, std::time::Duration::from_secs(5)).fuse();
    let drain = slog_async::Async::new(drain).build().fuse();

    let log = Logger::root(drain, o!());
    info!(log, "Hello MongoDB!");
}

Notes

By default, the logged messages contain the following values:

  • "ts": RFC3339 timestamp
  • "level": "TRCE", "INFO", "WARN", "ERRO" or "CRIT"
  • "leveli": 4 - 0
  • "msg": the log message

This behavior can be changed by constructing the drain as follows:

use slog::*;

fn main() {
    let client = mongodb::Client::with_uri_str("mongodb://localhost:27017/").unwrap();
    let db = client.database("some_db");
    let logs = db.collection("logs");

    let drain = MongoDBDrainBuilder::new(logs, std::time::Duration::from_secs(5))
            .add_key_value(o!("key" => "value")).build();
    let drain = slog_async::Async::new(drain).build().fuse();

    let log = Logger::root(drain, o!());
    info!(log, "Hello MongoDB!");
}

Credits

The serde serialization as well as the overall design is copied from slog-json.

Commit count: 14

cargo fmt