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
mod chunked; mod level; mod message; mod udp; extern crate chrono; extern crate flate2; extern crate rand; extern crate serde; extern crate serde_json; extern crate slog; use flate2::{write::GzEncoder, Compression}; use slog::{Drain, Key, OwnedKVList, Record, KV}; use std::io; use std::io::prelude::*; use chunked::ChunkSize; use message::Message; use udp::UdpDestination; static VERSION: &'static str = "1.1"; pub struct Gelf { source: String, destination: UdpDestination, } impl Gelf { pub fn new(source: &str, destination: &str) -> Result<Self, io::Error> { let destination = UdpDestination::new(destination, ChunkSize::LAN)?; Ok(Gelf { source: source.to_owned(), destination, }) } } pub struct KeyValueList(pub Vec<(Key, String)>); impl slog::Serializer for KeyValueList { fn emit_arguments(&mut self, key: Key, val: &std::fmt::Arguments) -> slog::Result { self.0.push((key, format!("{}", val))); Ok(()) } } impl Drain for Gelf { type Ok = (); type Err = io::Error; fn log(&self, record: &Record, values: &OwnedKVList) -> Result<Self::Ok, Self::Err> { let mut additional = KeyValueList(Vec::with_capacity(16)); record.kv().serialize(record, &mut additional)?; values.serialize(record, &mut additional)?; let message = Message { version: VERSION, host: &self.source, short_message: record.msg().to_string(), full_message: None, timestamp: Some(timestamp()), level: Some(record.level().into()), module: Some(record.location().module), file: Some(record.location().file), line: Some(record.location().line), column: None, additional: additional.0, }; let serialized = serde_json::to_vec(&message)?; let mut e = GzEncoder::new(Vec::new(), Compression::default()); e.write_all(&serialized)?; let compressed = e.finish()?; let _ = self.destination.log(compressed); Ok(()) } } fn timestamp() -> f64 { let now = chrono::Utc::now(); let milliseconds = (now.timestamp() as f64) + (now.timestamp_subsec_millis() as f64) / 1E3; milliseconds }