use crate::{collector_serializer::PlainKVSerializer, http_log_client::HttpLogClient};
use serde_json::json;
use slog::{Drain, OwnedKVList, Record, KV};
use slog_scope::error;
use std::{
error::Error,
time::{SystemTime, UNIX_EPOCH},
};
#[derive(Debug)]
pub struct HttpLocalSlogDrain {
client: HttpLogClient,
}
impl Drain for HttpLocalSlogDrain {
type Ok = ();
type Err = slog::Never;
fn log(&self, record: &Record, values: &OwnedKVList) -> Result<Self::Ok, Self::Err> {
let ret = self.log_impl(record, values);
if let Some(e) = ret.err() {
error!("Error sending log using http client: {}", e);
}
Ok(())
}
}
impl HttpLocalSlogDrain {
pub fn new(client: HttpLogClient) -> Self {
HttpLocalSlogDrain { client }
}
fn log_impl(&self, record: &Record, values: &OwnedKVList) -> Result<(), Box<dyn Error>> {
let mut serializer = PlainKVSerializer::new();
values.serialize(record, &mut serializer)?;
record.kv().serialize(record, &mut serializer)?;
let mut kvs = serializer.into_inner();
kvs.insert("msg", format!("{}", record.msg()));
kvs.insert("level", record.level().as_str().to_string());
kvs.insert("current_mod", module_path!().to_string());
kvs.insert(
"time",
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Current timestamp is earlier than UNIX epoch")
.as_secs()
.to_string(),
);
self.client.send_log(json!(kvs).to_string())?;
Ok(())
}
}