From a242b9f0367811a88b33e34af8234fb61af8097e Mon Sep 17 00:00:00 2001 From: appflowy Date: Sat, 3 Jul 2021 21:40:13 +0800 Subject: [PATCH] config log --- .../lib/startup/tasks/rust_sdk_init_task.dart | 10 +++---- rust-lib/.gitignore | 1 + rust-lib/dart-ffi/Cargo.toml | 2 +- rust-lib/dart-ffi/src/lib.rs | 2 +- rust-lib/flowy-log/Cargo.toml | 10 ++++--- rust-lib/flowy-log/src/lib.rs | 30 +++++++++++-------- rust-lib/flowy-sdk/Cargo.toml | 2 +- rust-lib/flowy-sys/src/dispatch.rs | 8 +++++ rust-lib/flowy-sys/src/module/module.rs | 2 -- rust-lib/flowy-sys/src/response/response.rs | 2 -- 10 files changed, 40 insertions(+), 29 deletions(-) diff --git a/app_flowy/lib/startup/tasks/rust_sdk_init_task.dart b/app_flowy/lib/startup/tasks/rust_sdk_init_task.dart index f65a4d73bb..3d98f01bd3 100644 --- a/app_flowy/lib/startup/tasks/rust_sdk_init_task.dart +++ b/app_flowy/lib/startup/tasks/rust_sdk_init_task.dart @@ -17,15 +17,15 @@ class RustSDKInitTask extends LaunchTask { Bloc.observer = ApplicationBlocObserver(); + Directory directory = await getApplicationDocumentsDirectory(); + final documentPath = directory.path; + final flowySandbox = Directory('$documentPath/flowy'); switch (context.env) { case IntegrationEnv.dev: - await context.getIt().init(Directory('./temp/flowy_dev')); + // await context.getIt().init(Directory('./temp/flowy_dev')); + await context.getIt().init(flowySandbox); break; case IntegrationEnv.pro: - Directory directory = await getApplicationDocumentsDirectory(); - final documentPath = directory.path; - - final flowySandbox = Directory('$documentPath/flowy'); await context.getIt().init(flowySandbox); break; default: diff --git a/rust-lib/.gitignore b/rust-lib/.gitignore index 088ba6ba7d..08ffdffa38 100644 --- a/rust-lib/.gitignore +++ b/rust-lib/.gitignore @@ -8,3 +8,4 @@ Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk +**/**/*.log* \ No newline at end of file diff --git a/rust-lib/dart-ffi/Cargo.toml b/rust-lib/dart-ffi/Cargo.toml index 42b0f6ce86..a1d81f0f01 100644 --- a/rust-lib/dart-ffi/Cargo.toml +++ b/rust-lib/dart-ffi/Cargo.toml @@ -27,7 +27,7 @@ serde_json = {version = "1.0"} flowy-sys = {path = "../flowy-sys"} flowy-sdk = {path = "../flowy-sdk"} -flowy-log = {path = "../flowy-log"} + #[features] #use_serde = ["bincode"] diff --git a/rust-lib/dart-ffi/src/lib.rs b/rust-lib/dart-ffi/src/lib.rs index 95ce9aa534..290826fe0a 100644 --- a/rust-lib/dart-ffi/src/lib.rs +++ b/rust-lib/dart-ffi/src/lib.rs @@ -82,7 +82,7 @@ where }, Err(e) => { if let Some(msg) = e.downcast_ref::<&str>() { - log::error!("[FFI]: ❌ {:?}", msg); + log::error!("[FFI]: {:?}", msg); } else { log::error!("[FFI]: allo_isolate post panic"); } diff --git a/rust-lib/flowy-log/Cargo.toml b/rust-lib/flowy-log/Cargo.toml index fed23fba70..cd598d3142 100644 --- a/rust-lib/flowy-log/Cargo.toml +++ b/rust-lib/flowy-log/Cargo.toml @@ -6,11 +6,13 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -#tracing = { version = "0.1" } -tracing = { version = "0.1", features = ["max_level_debug", "release_max_level_warn"] } +tracing = { version = "0.1" } tracing-log = { version = "0.1.1"} tracing-futures = "0.2.4" -tracing-subscriber = { version = "0.2.12", features = ["registry", "env-filter"] } +tracing-subscriber = { version = "0.2.12", features = ["registry", "env-filter", "ansi", "json"] } tracing-bunyan-formatter = "0.2.2" tracing-appender = "0.1" -log = "0.4.14" \ No newline at end of file +log = "0.4.14" + +[features] +use_bunyan = [] \ No newline at end of file diff --git a/rust-lib/flowy-log/src/lib.rs b/rust-lib/flowy-log/src/lib.rs index 04e54cf9ba..7fff5d8055 100644 --- a/rust-lib/flowy-log/src/lib.rs +++ b/rust-lib/flowy-log/src/lib.rs @@ -13,19 +13,16 @@ pub struct FlowyLogBuilder { name: String, env_filter: String, directory: String, - file_appender: RollingFileAppender, } impl FlowyLogBuilder { pub fn new(name: &str, directory: impl AsRef) -> Self { let directory = directory.as_ref().to_str().unwrap().to_owned(); - let local_file_name = format!("{}.log", name); - let file_appender = tracing_appender::rolling::hourly(directory.clone(), local_file_name); + FlowyLogBuilder { name: name.to_owned(), env_filter: "Info".to_owned(), directory, - file_appender, } } @@ -37,29 +34,36 @@ impl FlowyLogBuilder { pub fn build(self) -> std::result::Result<(), String> { let env_filter = EnvFilter::new(self.env_filter); - let (non_blocking, _guard) = tracing_appender::non_blocking(self.file_appender); - - let formatting_layer = BunyanFormattingLayer::new(self.name, std::io::stdout); - let mut subscriber = tracing_subscriber::fmt() .with_target(false) .with_max_level(tracing::Level::TRACE) - .with_writer(std::io::stdout) + .with_writer(std::io::stderr) .with_thread_ids(false) .with_target(false) // .with_writer(non_blocking) .compact() .finish() - .with(env_filter) - .with(JsonStorageLayer) - .with(formatting_layer); + .with(env_filter); + if cfg!(feature = "use_bunyan") { + let formatting_layer = BunyanFormattingLayer::new(self.name.clone(), std::io::stdout); + + let local_file_name = format!("{}.log", &self.name); + let file_appender = + tracing_appender::rolling::daily(self.directory.clone(), local_file_name); + let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender); + + 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))?; + } let _ = LogTracer::builder() .with_max_level(LevelFilter::Trace) .init() .map_err(|e| format!("{:?}", e)) .unwrap(); - let _ = set_global_default(subscriber).map_err(|e| format!("{:?}", e))?; + Ok(()) } } diff --git a/rust-lib/flowy-sdk/Cargo.toml b/rust-lib/flowy-sdk/Cargo.toml index aebaa1df66..c07ffd19c3 100644 --- a/rust-lib/flowy-sdk/Cargo.toml +++ b/rust-lib/flowy-sdk/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" [dependencies] flowy-sys = { path = "../flowy-sys", features = ["use_tracing"]} -flowy-log = { path = "../flowy-log" } +flowy-log = { path = "../flowy-log", features = ["use_bunyan"] } flowy-user = { path = "../flowy-user" } tracing = { version = "0.1" } log = "0.4.14" diff --git a/rust-lib/flowy-sys/src/dispatch.rs b/rust-lib/flowy-sys/src/dispatch.rs index 3cbc043f25..46bb79297a 100644 --- a/rust-lib/flowy-sys/src/dispatch.rs +++ b/rust-lib/flowy-sys/src/dispatch.rs @@ -162,6 +162,14 @@ impl Service for DispatchService { type Error = SystemError; type Future = BoxFuture<'static, Result>; + #[cfg_attr( + feature = "use_tracing", + tracing::instrument( + name = "DispatchService", + level = "debug", + skip(self, dispatch_request) + ) + )] fn call(&self, dispatch_request: DispatchRequest) -> Self::Future { let module_map = self.module_map.clone(); let (request, callback) = dispatch_request.into_parts(); diff --git a/rust-lib/flowy-sys/src/module/module.rs b/rust-lib/flowy-sys/src/module/module.rs index 3b7e017e49..57127d32c4 100644 --- a/rust-lib/flowy-sys/src/module/module.rs +++ b/rust-lib/flowy-sys/src/module/module.rs @@ -164,8 +164,6 @@ impl Service for ModuleService { type Error = SystemError; type Future = BoxFuture<'static, Result>; - // #[cfg_attr(feature = "use_tracing", xxx)] - #[tracing::instrument(name = "Module Service", level = "debug", skip(self))] fn call(&self, request: ModuleRequest) -> Self::Future { match self.service_map.get(&request.event()) { Some(factory) => { diff --git a/rust-lib/flowy-sys/src/response/response.rs b/rust-lib/flowy-sys/src/response/response.rs index 60766f71e2..5b0f10f225 100644 --- a/rust-lib/flowy-sys/src/response/response.rs +++ b/rust-lib/flowy-sys/src/response/response.rs @@ -67,8 +67,6 @@ where } } -// #[cfg_attr(feature = "use_serde", #[serde(serialize_with = -// "serialize_data")])] #[cfg(feature = "use_serde")] fn serialize_data(data: &ResponseData, serializer: S) -> Result where