1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// {{{ Crate docs
//! MongoDB `Drain` for `slog-rs`
//!
//! ```
//! use slog::*;
//!
//! 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!");
//! ```
// }}}

extern crate mongodb;
extern crate bson;
extern crate serde;
extern crate slog;
extern crate chrono;

mod slog_serde;

use std::{io, time};
use std::cell::RefCell;
use std::time::Instant;

use slog::{FnValue, PushFnValue};
use slog::{OwnedKVList, KV, SendSyncRefUnwindSafeKV};
use slog::{Record, o};

use slog_serde::{SerdeSerializer};

use mongodb::{Collection, options::InsertManyOptions};


/// MongoDB `Drain` for `slog-rs`.
/// Buffers incoming log messages and stores them at a configurable time interval.
///
/// ```
/// use slog::*;
///
/// 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, "Logging ready!");
// ```
pub struct MongoDBDrain {
    values: Vec<OwnedKVList>,
    collection: Collection,
    buffer: RefCell<Vec<bson::Document>>,
    drain_interval: time::Duration,
    last_drained: RefCell<time::Instant>
}

impl MongoDBDrain {
    pub fn new(collection: Collection, drain_interval: time::Duration)
        -> MongoDBDrain  {
        MongoDBDrainBuilder::new(collection, drain_interval).with_default_keys().build()
    }
}

impl slog::Drain for MongoDBDrain  {
    type Ok = ();
    type Err = io::Error;

    fn log(&self, rinfo: &Record, logger_values: &OwnedKVList) -> io::Result<()> {
        let encoder = bson::Encoder::new();
        let mut serializer = SerdeSerializer::start(encoder, None)?;

        for kv in &self.values {
            kv.serialize(rinfo, &mut serializer)?;
        }

        logger_values.serialize(rinfo, &mut serializer)?;
        rinfo.kv().serialize(rinfo, &mut serializer)?;

        let res = serializer.end().map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;

        if let bson::Bson::Document(log) = res {
            self.buffer.borrow_mut().push(log);
            if time::Instant::now().saturating_duration_since(*self.last_drained.borrow()) >= self.drain_interval
            {
                self.collection.insert_many(self.buffer.borrow_mut().drain(..),
                    Some(InsertManyOptions::builder().ordered(false).build())
                ).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
                self.last_drained.replace(Instant::now());
            }
            Ok(())
        } else {
            Err(io::Error::new(io::ErrorKind::Other, "Can only store BSON documents (Bson::Document)"))
        }
    }
}

/// MongoDB `Drain` builder
///
/// ```
/// use slog::*;
///
/// 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::MongoDBDrainBuilder::new(logs, std::time::Duration::from_secs(5))
///                 .add_key_value(o!("key" => "value")).build().fuse();
/// let drain = slog_async::Async::new(drain).build().fuse();
/// // ...
/// ```
pub struct MongoDBDrainBuilder {
    values: Vec<OwnedKVList>,
    collection: Collection,
    drain_interval: time::Duration,
}

impl MongoDBDrainBuilder {
    pub fn new(collection: Collection, drain_interval: time::Duration) -> Self {
        MongoDBDrainBuilder {
            values: vec![],
            collection,
            drain_interval
        }
    }

    /// Build `MongoDBDrain` `Drain`
    pub fn build(self) -> MongoDBDrain {
        MongoDBDrain {
            values: self.values,
            collection: self.collection,
            buffer: RefCell::new(Vec::with_capacity(50)),
            drain_interval: self.drain_interval,
            last_drained: RefCell::new(Instant::now())
        }
    }

    /// Add custom values to be printed with this formatter
    pub fn add_key_value<T>(mut self, value: slog::OwnedKV<T>) -> Self
        where T: SendSyncRefUnwindSafeKV + 'static
    {
        self.values.push(value.into());
        self
    }

    /// Add default key-values:
    ///
    /// * `ts` - timestamp
    /// * `level` - record logging level name
    /// * `leveli` - record logging level integer, "Critical is the smallest and Trace the biggest value" - slog::Level, docs.rs/slog
    /// * `msg` - msg - formatted logging message
    pub fn with_default_keys(self) -> Self {
        self.add_key_value(o!(
            "ts" => PushFnValue(move |_, ser| ser.emit(chrono::Local::now().to_rfc3339())),
            "level" => FnValue(move |rinfo| rinfo.level().as_short_str()),
            "leveli" => FnValue(move |rinfo| rinfo.level().as_usize()),
            "msg" => PushFnValue(move |record, ser| ser.emit(record.msg())),
        ))
    }
}