add custom logging subscriber framework
This commit is contained in:
parent
76766ace7a
commit
539971dd05
5 changed files with 92 additions and 16 deletions
22
packages/backend/Cargo.lock
generated
22
packages/backend/Cargo.lock
generated
|
@ -301,6 +301,10 @@ name = "logging"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"config",
|
"config",
|
||||||
|
"termcolor",
|
||||||
|
"thiserror",
|
||||||
|
"tracing",
|
||||||
|
"tracing-subscriber",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -610,6 +614,15 @@ version = "0.1.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160"
|
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]]
|
[[package]]
|
||||||
name = "thiserror"
|
name = "thiserror"
|
||||||
version = "1.0.40"
|
version = "1.0.40"
|
||||||
|
@ -854,6 +867,15 @@ version = "0.4.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
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]]
|
[[package]]
|
||||||
name = "winapi-x86_64-pc-windows-gnu"
|
name = "winapi-x86_64-pc-windows-gnu"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
|
|
|
@ -7,3 +7,7 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
config = { path = "../config" }
|
config = { path = "../config" }
|
||||||
|
termcolor = "1.2.0"
|
||||||
|
thiserror = "1.0.40"
|
||||||
|
tracing = "0.1.37"
|
||||||
|
tracing-subscriber = "0.3.17"
|
||||||
|
|
|
@ -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!()
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,7 @@
|
||||||
|
|
||||||
use axum::Router;
|
use axum::Router;
|
||||||
|
|
||||||
use tokio::runtime;
|
|
||||||
use config::get_config;
|
use config::get_config;
|
||||||
|
use tokio::runtime;
|
||||||
|
|
||||||
pub mod api {
|
pub mod api {
|
||||||
pub mod routes;
|
pub mod routes;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{
|
use std::{
|
||||||
env, fmt,
|
env, fmt,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf}, io::stdout,
|
||||||
};
|
};
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
|
@ -17,15 +17,13 @@ fn main() -> anyhow::Result<()> {
|
||||||
);
|
);
|
||||||
|
|
||||||
// logging
|
// logging
|
||||||
let subscriber = tracing_subscriber::fmt();
|
let subscriber = logging::Logger::new(if is_release!() {
|
||||||
if is_release!() {
|
tracing::Level::INFO
|
||||||
subscriber.with_max_level(tracing::Level::INFO).init();
|
|
||||||
} else {
|
} else {
|
||||||
subscriber
|
tracing::Level::DEBUG
|
||||||
.with_max_level(tracing::Level::DEBUG)
|
}, stdout());
|
||||||
.pretty()
|
|
||||||
.init();
|
tracing::subscriber::set_global_default(subscriber)?;
|
||||||
}
|
|
||||||
|
|
||||||
// bootstrap
|
// bootstrap
|
||||||
|
|
||||||
|
@ -39,7 +37,7 @@ fn main() -> anyhow::Result<()> {
|
||||||
debug!(target: "config", path = ?config_path, "Loading yaml file");
|
debug!(target: "config", path = ?config_path, "Loading yaml file");
|
||||||
config::init_config(config_path)?;
|
config::init_config(config_path)?;
|
||||||
|
|
||||||
eprintln!("{:?}", config::get_config()?);
|
debug!(config_file = ?config::get_config());
|
||||||
|
|
||||||
server::init()?;
|
server::init()?;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue