2021-09-13 05:05:46 +00:00
|
|
|
mod layer;
|
|
|
|
|
2021-07-03 06:14:10 +00:00
|
|
|
use log::LevelFilter;
|
2021-11-04 08:43:12 +00:00
|
|
|
|
2021-07-05 13:23:13 +00:00
|
|
|
use tracing::subscriber::set_global_default;
|
|
|
|
|
2021-09-13 05:05:46 +00:00
|
|
|
use crate::layer::*;
|
2021-11-03 07:28:34 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use std::sync::RwLock;
|
|
|
|
use tracing_appender::{non_blocking::WorkerGuard, rolling::RollingFileAppender};
|
2021-09-13 07:51:13 +00:00
|
|
|
use tracing_bunyan_formatter::JsonStorageLayer;
|
2021-06-29 05:45:32 +00:00
|
|
|
use tracing_log::LogTracer;
|
2021-11-04 08:43:12 +00:00
|
|
|
use tracing_subscriber::{layer::SubscriberExt, EnvFilter};
|
2021-11-03 07:28:34 +00:00
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref LOG_GUARD: RwLock<Option<WorkerGuard>> = RwLock::new(None);
|
|
|
|
}
|
2021-07-03 06:14:10 +00:00
|
|
|
|
2021-08-19 06:08:24 +00:00
|
|
|
pub struct Builder {
|
2021-11-08 02:57:33 +00:00
|
|
|
#[allow(dead_code)]
|
2021-07-03 06:14:10 +00:00
|
|
|
name: String,
|
|
|
|
env_filter: String,
|
2021-11-04 04:47:41 +00:00
|
|
|
file_appender: RollingFileAppender,
|
2021-07-03 06:14:10 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 06:08:24 +00:00
|
|
|
impl Builder {
|
2021-11-04 04:47:41 +00:00
|
|
|
pub fn new(name: &str, directory: &str) -> Self {
|
|
|
|
// let directory = directory.as_ref().to_str().unwrap().to_owned();
|
|
|
|
let local_file_name = format!("{}.log", name);
|
|
|
|
|
2021-08-19 06:08:24 +00:00
|
|
|
Builder {
|
2021-07-03 06:14:10 +00:00
|
|
|
name: name.to_owned(),
|
|
|
|
env_filter: "Info".to_owned(),
|
2021-11-04 04:47:41 +00:00
|
|
|
file_appender: tracing_appender::rolling::daily(directory, local_file_name),
|
2021-07-03 06:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn env_filter(mut self, env_filter: &str) -> Self {
|
|
|
|
self.env_filter = env_filter.to_owned();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build(self) -> std::result::Result<(), String> {
|
|
|
|
let env_filter = EnvFilter::new(self.env_filter);
|
2021-11-04 04:47:41 +00:00
|
|
|
|
|
|
|
let (non_blocking, guard) = tracing_appender::non_blocking(self.file_appender);
|
2021-07-05 13:23:13 +00:00
|
|
|
let subscriber = tracing_subscriber::fmt()
|
2021-09-13 05:05:46 +00:00
|
|
|
// .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
|
2021-11-03 07:28:34 +00:00
|
|
|
.with_ansi(false)
|
2021-09-14 08:22:44 +00:00
|
|
|
.with_target(false)
|
2021-07-03 06:14:10 +00:00
|
|
|
.with_max_level(tracing::Level::TRACE)
|
2021-07-03 13:40:13 +00:00
|
|
|
.with_writer(std::io::stderr)
|
2021-11-03 14:04:45 +00:00
|
|
|
.with_thread_ids(true)
|
|
|
|
// .with_writer(non_blocking)
|
2021-11-03 07:28:34 +00:00
|
|
|
.json()
|
2021-11-03 14:04:45 +00:00
|
|
|
.with_current_span(true)
|
|
|
|
.with_span_list(true)
|
2021-07-03 06:14:10 +00:00
|
|
|
.compact()
|
|
|
|
.finish()
|
2021-11-04 04:47:41 +00:00
|
|
|
.with(env_filter)
|
|
|
|
.with(JsonStorageLayer)
|
2021-11-03 14:04:45 +00:00
|
|
|
.with(FlowyFormattingLayer::new(std::io::stdout))
|
|
|
|
.with(FlowyFormattingLayer::new(non_blocking));
|
2021-07-03 13:40:13 +00:00
|
|
|
|
2021-09-13 05:05:46 +00:00
|
|
|
// if cfg!(feature = "use_bunyan") {
|
|
|
|
// let formatting_layer = BunyanFormattingLayer::new(self.name.clone(),
|
|
|
|
// std::io::stdout); let _ =
|
|
|
|
// set_global_default(subscriber.with(JsonStorageLayer).with(formatting_layer)).
|
|
|
|
// map_err(|e| format!("{:?}", e))?; } else {
|
|
|
|
// let _ = set_global_default(subscriber).map_err(|e| format!("{:?}", e))?;
|
|
|
|
// }
|
|
|
|
|
2021-11-03 07:28:34 +00:00
|
|
|
let _ = set_global_default(subscriber).map_err(|e| format!("{:?}", e))?;
|
2021-07-03 06:14:10 +00:00
|
|
|
let _ = LogTracer::builder()
|
|
|
|
.with_max_level(LevelFilter::Trace)
|
|
|
|
.init()
|
2022-01-08 10:21:13 +00:00
|
|
|
.map_err(|e| format!("{:?}", e))?;
|
2021-07-03 13:40:13 +00:00
|
|
|
|
2021-11-03 07:28:34 +00:00
|
|
|
*LOG_GUARD.write().unwrap() = Some(guard);
|
2021-07-03 06:14:10 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 05:45:32 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-08-19 06:08:24 +00:00
|
|
|
// run cargo test --features="use_bunyan" or cargo test
|
2021-06-29 05:45:32 +00:00
|
|
|
#[test]
|
|
|
|
fn test_log() {
|
2021-11-04 04:47:41 +00:00
|
|
|
let _ = Builder::new("flowy", ".").env_filter("debug").build().unwrap();
|
2021-11-03 07:28:34 +00:00
|
|
|
tracing::info!("😁 tracing::info call");
|
|
|
|
log::debug!("😁 log::debug call");
|
2021-07-03 06:14:10 +00:00
|
|
|
|
2021-11-03 07:28:34 +00:00
|
|
|
say("hello world");
|
|
|
|
}
|
2021-07-03 06:14:10 +00:00
|
|
|
|
2021-08-19 06:08:24 +00:00
|
|
|
#[tracing::instrument(name = "say")]
|
|
|
|
fn say(s: &str) {
|
2021-11-03 07:37:38 +00:00
|
|
|
tracing::info!("{}", s);
|
2021-06-29 05:45:32 +00:00
|
|
|
}
|
|
|
|
}
|