add logger

This commit is contained in:
appflowy 2021-06-29 13:45:32 +08:00
parent d867093bf5
commit 6fe196e97f
4 changed files with 51 additions and 0 deletions

View File

@ -8,6 +8,7 @@
<sourceFolder url="file://$MODULE_DIR$/rust-lib/flowy-sys/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/rust-lib/flowy-sys/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/rust-lib/flowy-sys/tests" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/rust-lib/flowy-sys/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/rust-lib/dart-ffi/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/rust-lib/dart-ffi/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/rust-lib/flowy-log/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/rust-lib/flowy-sdk/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/rust-lib/flowy-sdk/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/app_flowy/packages/af_protobuf/.pub" /> <excludeFolder url="file://$MODULE_DIR$/app_flowy/packages/af_protobuf/.pub" />
<excludeFolder url="file://$MODULE_DIR$/app_flowy/packages/af_protobuf/.dart_tool" /> <excludeFolder url="file://$MODULE_DIR$/app_flowy/packages/af_protobuf/.dart_tool" />

View File

@ -3,6 +3,7 @@ members = [
"flowy-sys", "flowy-sys",
"flowy-sdk", "flowy-sdk",
"dart-ffi", "dart-ffi",
"flowy-log",
] ]
[profile.dev] [profile.dev]

View File

@ -0,0 +1,14 @@
[package]
name = "flowy-log"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tracing = { version = "0.1", features = ["log"] }
tracing-log = "0.1.1"
tracing-futures = "0.2.4"
tracing-subscriber = { version = "0.2.12", features = ["registry", "env-filter"] }
tracing-bunyan-formatter = "0.2.2"
log = "0.4.14"

View File

@ -0,0 +1,35 @@
use log::SetLoggerError;
use tracing::subscriber::set_global_default;
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
use tracing_log::LogTracer;
use tracing_subscriber::{layer::SubscriberExt, EnvFilter};
pub fn init_log(name: &str, env_filter: &str) -> std::Result<(), SetLoggerError> {
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(env_filter.to_owned()));
let formatting_layer = BunyanFormattingLayer::new(name.to_owned(), std::io::stdout);
let subscriber = tracing_subscriber::fmt()
.with_target(false)
.with_thread_ids(false)
.with_target(false)
.compact()
.finish()
.with(env_filter)
.with(JsonStorageLayer)
.with(formatting_layer);
let _ = LogTracer::init()?;
let _ = set_global_default(subscriber)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_log() {
init_log("flowy-log", "info");
tracing::info!("😁 Tracing info log");
log::info!("😁 bridge 'log' to 'tracing'");
}
}