From 539971dd05191c3249e0e3b461bd818e48f1a9b4 Mon Sep 17 00:00:00 2001 From: s1idewhist1e Date: Tue, 16 May 2023 23:04:02 -0700 Subject: [PATCH] add custom logging subscriber framework --- packages/backend/Cargo.lock | 22 +++++++++ packages/backend/crates/logging/Cargo.toml | 4 ++ packages/backend/crates/logging/src/lib.rs | 53 ++++++++++++++++++++++ packages/backend/crates/server/src/lib.rs | 11 ++--- packages/backend/src/main.rs | 18 ++++---- 5 files changed, 92 insertions(+), 16 deletions(-) diff --git a/packages/backend/Cargo.lock b/packages/backend/Cargo.lock index 26cc46f78e..e3b73eeb0e 100644 --- a/packages/backend/Cargo.lock +++ b/packages/backend/Cargo.lock @@ -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" diff --git a/packages/backend/crates/logging/Cargo.toml b/packages/backend/crates/logging/Cargo.toml index 50bc5b1d25..c3b8bd7c69 100644 --- a/packages/backend/crates/logging/Cargo.toml +++ b/packages/backend/crates/logging/Cargo.toml @@ -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" diff --git a/packages/backend/crates/logging/src/lib.rs b/packages/backend/crates/logging/src/lib.rs index e69de29bb2..03949c6542 100644 --- a/packages/backend/crates/logging/src/lib.rs +++ b/packages/backend/crates/logging/src/lib.rs @@ -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 { + log_level: Level, + writer: Arc>, +} + +impl Logger { + pub fn new(log_level: Level, writer: T) -> Self { + Self { + log_level, + writer: RwLock::new(writer).into(), + } + } +} + +impl Subscriber for Logger { + 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!() + } +} diff --git a/packages/backend/crates/server/src/lib.rs b/packages/backend/crates/server/src/lib.rs index c09eac9dfb..3dfe84592b 100644 --- a/packages/backend/crates/server/src/lib.rs +++ b/packages/backend/crates/server/src/lib.rs @@ -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!(); + // } } diff --git a/packages/backend/src/main.rs b/packages/backend/src/main.rs index 5d9a9d6a3d..f9d869f53b 100644 --- a/packages/backend/src/main.rs +++ b/packages/backend/src/main.rs @@ -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()?;