diff --git a/Cargo.toml b/Cargo.toml index d4404f23..7bb948d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,10 +5,11 @@ members = [ "registry-zookeeper", "registry-nacos", "metadata", - "common", "config", "dubbo", "examples/echo", "examples/greeter", - "dubbo-build" + "dubbo-build", + "common/logger" ] + diff --git a/common/Cargo.toml b/common/logger/Cargo.toml similarity index 55% rename from common/Cargo.toml rename to common/logger/Cargo.toml index 19282c2e..14157bb1 100644 --- a/common/Cargo.toml +++ b/common/logger/Cargo.toml @@ -1,11 +1,10 @@ [package] -name = "common" +name = "logger" version = "0.3.0" edition = "2021" -license = "Apache-2.0" -description = "dubbo-rust-common" -repository = "https://github.com/apache/dubbo-rust.git" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +tracing = "0.1" +tracing-subscriber = "0.3" \ No newline at end of file diff --git a/common/LICENSE b/common/logger/LICENSE similarity index 100% rename from common/LICENSE rename to common/logger/LICENSE diff --git a/common/logger/README.md b/common/logger/README.md new file mode 100644 index 00000000..cdf70128 --- /dev/null +++ b/common/logger/README.md @@ -0,0 +1,3 @@ +ToDo: +1. 日志配置器与tower集成 https://github.com/tokio-rs/tracing/tree/master/tracing-tower +2. 与OpenTelemetry集成 https://github.com/tokio-rs/tracing/tree/master/tracing-opentelemetry \ No newline at end of file diff --git a/common/src/lib.rs b/common/logger/src/lib.rs similarity index 77% rename from common/src/lib.rs rename to common/logger/src/lib.rs index 3e01853c..27372747 100644 --- a/common/src/lib.rs +++ b/common/logger/src/lib.rs @@ -15,11 +15,22 @@ * limitations under the License. */ +pub use tracing::{self, Level}; + +mod tracing_configurer; + +// put on main method +pub fn init() { + tracing_configurer::default() +} + #[cfg(test)] mod tests { + use tracing::debug; + #[test] - fn it_works() { - let result = 2 + 2; - assert_eq!(result, 4); + fn test_print_info_log() { + super::init(); + debug!("hello rust"); } } diff --git a/common/logger/src/tracing_configurer.rs b/common/logger/src/tracing_configurer.rs new file mode 100644 index 00000000..40b1e844 --- /dev/null +++ b/common/logger/src/tracing_configurer.rs @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// https://github.com/tokio-rs/tracing/issues/971 + +use tracing::{debug, Level}; + +pub(crate) fn default() { + if let Some(true) = configured() { + parse_from_config() + } else { + tracing_subscriber::fmt() + .compact() + .with_line_number(true) + .with_max_level(Level::DEBUG) + // sets this to be the default, global collector for this application. + .try_init() + .expect("init err."); + } + debug!("Tracing configured.") +} + +pub(crate) fn parse_from_config() { + todo!() +} + +pub(crate) fn configured() -> Option { + None +} diff --git a/dubbo/src/framework.rs b/dubbo/src/framework.rs index 342ef6c2..71e11d2e 100644 --- a/dubbo/src/framework.rs +++ b/dubbo/src/framework.rs @@ -48,7 +48,6 @@ pub struct Dubbo { impl Dubbo { pub fn new() -> Dubbo { - tracing_subscriber::fmt::init(); Self { protocols: HashMap::new(), registries: None, diff --git a/examples/echo/Cargo.toml b/examples/echo/Cargo.toml index 1a842d2e..82d1554c 100644 --- a/examples/echo/Cargo.toml +++ b/examples/echo/Cargo.toml @@ -28,6 +28,7 @@ prost-derive = {version = "0.10", optional = true} prost = "0.10.4" async-trait = "0.1.56" tokio-stream = "0.1" +logger = {path="../../common/logger"} hyper = { version = "0.14.19", features = ["full"]} diff --git a/examples/echo/src/echo/client.rs b/examples/echo/src/echo/client.rs index 4d5bb3ad..b107e305 100644 --- a/examples/echo/src/echo/client.rs +++ b/examples/echo/src/echo/client.rs @@ -30,6 +30,7 @@ impl Filter for FakeFilter { #[tokio::main] async fn main() { + logger::init(); // let builder = ClientBuilder::new() // .with_connector("unix") // .with_host("unix://127.0.0.1:8888"); diff --git a/examples/echo/src/echo/server.rs b/examples/echo/src/echo/server.rs index 4a40d66b..1e10a2e8 100644 --- a/examples/echo/src/echo/server.rs +++ b/examples/echo/src/echo/server.rs @@ -46,6 +46,7 @@ impl Filter for FakeFilter { #[tokio::main] async fn main() { + logger::init(); register_server(EchoServerImpl { name: "echo".to_string(), }); diff --git a/examples/greeter/Cargo.toml b/examples/greeter/Cargo.toml index 4e56f4e4..491f4054 100644 --- a/examples/greeter/Cargo.toml +++ b/examples/greeter/Cargo.toml @@ -28,8 +28,7 @@ prost-derive = {version = "0.10", optional = true} prost = "0.10.4" async-trait = "0.1.56" tokio-stream = "0.1" -tracing = "0.1" -tracing-subscriber = "0.2.0" +logger = {path="../../common/logger"} dubbo = {path = "../../dubbo", version = "0.3.0" } dubbo-config = {path = "../../config", version = "0.3.0" } dubbo-registry-zookeeper = {path = "../../registry-zookeeper", version = "0.3.0" } diff --git a/examples/greeter/src/greeter/client.rs b/examples/greeter/src/greeter/client.rs index d6208434..e01bf929 100644 --- a/examples/greeter/src/greeter/client.rs +++ b/examples/greeter/src/greeter/client.rs @@ -24,21 +24,10 @@ use dubbo::codegen::*; use dubbo_registry_zookeeper::zookeeper_registry::ZookeeperRegistry; use futures_util::StreamExt; use protos::{greeter_client::GreeterClient, GreeterRequest}; -use tracing::Level; -use tracing_subscriber::FmtSubscriber; #[tokio::main] async fn main() { - // a builder for `FmtSubscriber`. - let subscriber = FmtSubscriber::builder() - // all spans/events with a level higher than TRACE (e.g, debug, info, warn, etc.) - // will be written to stdout. - .with_max_level(Level::INFO) - // completes the builder. - .finish(); - - tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed"); - + logger::init(); // let mut cli = GreeterClient::new(ClientBuilder::from_static(&"http://127.0.0.1:8888")); // Here is example for zk diff --git a/examples/greeter/src/greeter/server.rs b/examples/greeter/src/greeter/server.rs index 271a54b3..a275d6dc 100644 --- a/examples/greeter/src/greeter/server.rs +++ b/examples/greeter/src/greeter/server.rs @@ -21,11 +21,14 @@ use async_trait::async_trait; use futures_util::{Stream, StreamExt}; use tokio::sync::mpsc; use tokio_stream::wrappers::ReceiverStream; -use tracing::info; use dubbo::{codegen::*, Dubbo}; use dubbo_config::RootConfig; use dubbo_registry_zookeeper::zookeeper_registry::ZookeeperRegistry; +use logger::{ + tracing::{info, span}, + Level, +}; use protos::{ greeter_server::{register_server, Greeter}, GreeterReply, GreeterRequest, @@ -41,7 +44,7 @@ type ResponseStream = #[tokio::main] async fn main() { - use tracing::{span, Level}; + logger::init(); let span = span!(Level::DEBUG, "greeter.server"); let _enter = span.enter(); register_server(GreeterServerImpl { diff --git a/registry-nacos/src/nacos_registry.rs b/registry-nacos/src/nacos_registry.rs index 888382f0..2d0d6695 100644 --- a/registry-nacos/src/nacos_registry.rs +++ b/registry-nacos/src/nacos_registry.rs @@ -163,8 +163,7 @@ impl Registry for NacosRegistry { let nacos_service_instance = Self::create_nacos_service_instance(url); info!("register service: {}", nacos_service_name); - - let ret = self.nacos_naming_service.register_service( + let ret = self.nacos_naming_service.register_instance( nacos_service_name, group_name, nacos_service_instance,