Initial commit
This commit is contained in:
10
snitchlib/Cargo.toml
Normal file
10
snitchlib/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "snitchlib"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
log = "0.4"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
reqwest = { version = "0.11.12", default-features = false, features = ["rustls-tls", "serde_json", "blocking", "json"] }
|
||||
29
snitchlib/examples/log.rs
Normal file
29
snitchlib/examples/log.rs
Normal file
@ -0,0 +1,29 @@
|
||||
use log::{Level, LevelFilter, Log, Metadata, Record};
|
||||
use snitchlib::SnitchLogger;
|
||||
|
||||
struct SimpleLogger;
|
||||
|
||||
impl log::Log for SimpleLogger {
|
||||
fn enabled(&self, metadata: &Metadata) -> bool {
|
||||
metadata.level() <= Level::Info
|
||||
}
|
||||
|
||||
fn log(&self, record: &Record) {
|
||||
if self.enabled(record.metadata()) {
|
||||
eprintln!("{} - {}", record.level(), record.args());
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&self) {}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let logger = SnitchLogger::new(SimpleLogger, "http://localhost:8000", "snitchlib_example");
|
||||
let logger = Box::leak(Box::new(logger));
|
||||
log::set_logger(logger).expect("set logger");
|
||||
log::set_max_level(LevelFilter::Info);
|
||||
|
||||
log::info!("Everything should work, let's try it!");
|
||||
log::warn!("This is your last warning!");
|
||||
log::error!("Error! Error!");
|
||||
}
|
||||
61
snitchlib/src/lib.rs
Normal file
61
snitchlib/src/lib.rs
Normal file
@ -0,0 +1,61 @@
|
||||
use log::{Level, Log, Metadata, Record};
|
||||
use reqwest::blocking;
|
||||
|
||||
mod message;
|
||||
pub use message::{LogMsg, Severity};
|
||||
|
||||
pub struct SnitchLogger<L: Log> {
|
||||
wrapped: L,
|
||||
url: String,
|
||||
service_name: String,
|
||||
}
|
||||
|
||||
impl<L: Log> SnitchLogger<L> {
|
||||
pub fn new(wrapped: L, url: impl Into<String>, service_name: impl Into<String>) -> Self {
|
||||
SnitchLogger {
|
||||
wrapped,
|
||||
url: url.into(),
|
||||
service_name: service_name.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<L: Log> Log for SnitchLogger<L> {
|
||||
fn enabled(&self, metadata: &Metadata) -> bool {
|
||||
self.wrapped.enabled(metadata) || metadata.level() <= Level::Warn
|
||||
}
|
||||
|
||||
fn log(&self, record: &Record) {
|
||||
eprintln!("log called");
|
||||
self.wrapped.log(record);
|
||||
|
||||
let severity = match record.metadata().level() {
|
||||
Level::Error => Severity::Error,
|
||||
Level::Warn => Severity::Warning,
|
||||
_ => return,
|
||||
};
|
||||
|
||||
let record = LogMsg {
|
||||
severity,
|
||||
file: record.file().map(String::from),
|
||||
line: record.line(),
|
||||
..LogMsg::new(self.service_name.clone(), record.args().to_string())
|
||||
};
|
||||
|
||||
let client = blocking::Client::new();
|
||||
if let Err(e) = client.post(&self.url).json(&record).send() {
|
||||
// TODO: log error (without sending it)
|
||||
eprintln!("failed to send log record: {e:?}");
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&self) {
|
||||
self.wrapped.flush();
|
||||
}
|
||||
}
|
||||
|
||||
impl<L: Log> Drop for SnitchLogger<L> {
|
||||
fn drop(&mut self) {
|
||||
self.flush();
|
||||
}
|
||||
}
|
||||
39
snitchlib/src/message.rs
Normal file
39
snitchlib/src/message.rs
Normal file
@ -0,0 +1,39 @@
|
||||
use chrono::{DateTime, Local};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub enum Severity {
|
||||
Fatal,
|
||||
Error,
|
||||
Warning,
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub struct LogMsg {
|
||||
/// The time that the error was reported.
|
||||
pub time: DateTime<Local>,
|
||||
|
||||
pub severity: Severity,
|
||||
|
||||
/// The service which reported the error.
|
||||
pub service: String,
|
||||
|
||||
/// The log message.
|
||||
pub message: String,
|
||||
|
||||
pub file: Option<String>,
|
||||
pub line: Option<u32>,
|
||||
}
|
||||
|
||||
impl LogMsg {
|
||||
pub fn new(service: String, message: String) -> Self {
|
||||
Self {
|
||||
time: Local::now(),
|
||||
severity: Severity::Error,
|
||||
service,
|
||||
message,
|
||||
file: None,
|
||||
line: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user