add custom logging subscriber framework

This commit is contained in:
s1idewhist1e 2023-05-16 23:04:02 -07:00
parent 76766ace7a
commit 539971dd05
No known key found for this signature in database
GPG key ID: 9CC756BB9B325062
5 changed files with 92 additions and 16 deletions

View file

@ -301,6 +301,10 @@ name = "logging"
version = "0.1.0"
dependencies = [
"config",
"termcolor",
"thiserror",
"tracing",
"tracing-subscriber",
]
[[package]]
@ -610,6 +614,15 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160"
[[package]]
name = "termcolor"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6"
dependencies = [
"winapi-util",
]
[[package]]
name = "thiserror"
version = "1.0.40"
@ -854,6 +867,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

View file

@ -7,3 +7,7 @@ edition = "2021"
[dependencies]
config = { path = "../config" }
termcolor = "1.2.0"
thiserror = "1.0.40"
tracing = "0.1.37"
tracing-subscriber = "0.3.17"

View file

@ -0,0 +1,53 @@
use std::{
io::{stdout, Stdout, Write},
sync::{Arc, RwLock},
};
use tracing::{span, Level, Subscriber};
#[derive(thiserror::Error, Debug)]
pub struct Logger<T: Write> {
log_level: Level,
writer: Arc<RwLock<T>>,
}
impl<T: Write> Logger<T> {
pub fn new(log_level: Level, writer: T) -> Self {
Self {
log_level,
writer: RwLock::new(writer).into(),
}
}
}
impl<T: Write + 'static> Subscriber for Logger<T> {
fn enabled(&self, metadata: &tracing::Metadata<'_>) -> bool {
&self.log_level <= metadata.level()
}
fn new_span(&self, span: &span::Attributes<'_>) -> span::Id {
todo!()
}
fn record(&self, span: &span::Id, values: &span::Record<'_>) {
todo!()
}
fn record_follows_from(&self, span: &span::Id, follows: &span::Id) {
todo!()
}
fn event(&self, event: &tracing::Event<'_>) {
let mut out_buffer = self.writer.write().unwrap();
writeln!(out_buffer, "{:#?}", event.metadata()).unwrap();
}
fn enter(&self, span: &span::Id) {
todo!()
}
fn exit(&self, span: &span::Id) {
todo!()
}
}

View file

@ -1,8 +1,7 @@
use axum::Router;
use tokio::runtime;
use config::get_config;
use tokio::runtime;
pub mod api {
pub mod routes;
@ -38,8 +37,8 @@ pub fn init() -> anyhow::Result<()> {
#[cfg(test)]
mod tests {
// #[test]
// fn test() {
// macros::setup_test_config!();
// }
// #[test]
// fn test() {
// macros::setup_test_config!();
// }
}

View file

@ -1,6 +1,6 @@
use std::{
env, fmt,
path::{Path, PathBuf},
path::{Path, PathBuf}, io::stdout,
};
use tracing::debug;
@ -17,15 +17,13 @@ fn main() -> anyhow::Result<()> {
);
// logging
let subscriber = tracing_subscriber::fmt();
if is_release!() {
subscriber.with_max_level(tracing::Level::INFO).init();
let subscriber = logging::Logger::new(if is_release!() {
tracing::Level::INFO
} else {
subscriber
.with_max_level(tracing::Level::DEBUG)
.pretty()
.init();
}
tracing::Level::DEBUG
}, stdout());
tracing::subscriber::set_global_default(subscriber)?;
// bootstrap
@ -39,7 +37,7 @@ fn main() -> anyhow::Result<()> {
debug!(target: "config", path = ?config_path, "Loading yaml file");
config::init_config(config_path)?;
eprintln!("{:?}", config::get_config()?);
debug!(config_file = ?config::get_config());
server::init()?;