This is an automated email from the ASF dual-hosted git repository.

liujun pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/dubbo-rust.git


The following commit(s) were added to refs/heads/main by this push:
     new 56c6a14  Ftr: add common/logger sub package (#120)
56c6a14 is described below

commit 56c6a14b1a6be74510e15219b282a7fa3e52c5f7
Author: 墨舟 <[email protected]>
AuthorDate: Tue Feb 28 21:05:32 2023 +0800

    Ftr: add common/logger sub package (#120)
---
 Cargo.toml                                         |  5 ++--
 common/{ => logger}/Cargo.toml                     |  7 +++--
 common/{ => logger}/LICENSE                        |  0
 common/logger/README.md                            |  3 +++
 common/{ => logger}/src/lib.rs                     | 17 +++++++++---
 .../lib.rs => logger/src/tracing_configurer.rs}    | 30 +++++++++++++++++-----
 dubbo/src/framework.rs                             |  1 -
 examples/echo/Cargo.toml                           |  1 +
 examples/echo/src/echo/client.rs                   |  1 +
 examples/echo/src/echo/server.rs                   |  1 +
 examples/greeter/Cargo.toml                        |  3 +--
 examples/greeter/src/greeter/client.rs             | 13 +---------
 examples/greeter/src/greeter/server.rs             |  7 +++--
 registry-nacos/src/nacos_registry.rs               |  3 +--
 14 files changed, 58 insertions(+), 34 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index d4404f2..7bb948d 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 19282c2..14157bb 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 0000000..cdf7012
--- /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%
copy from common/src/lib.rs
copy to common/logger/src/lib.rs
index 3e01853..2737274 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/src/lib.rs b/common/logger/src/tracing_configurer.rs
similarity index 57%
rename from common/src/lib.rs
rename to common/logger/src/tracing_configurer.rs
index 3e01853..40b1e84 100644
--- a/common/src/lib.rs
+++ b/common/logger/src/tracing_configurer.rs
@@ -15,11 +15,29 @@
  * limitations under the License.
  */
 
-#[cfg(test)]
-mod tests {
-    #[test]
-    fn it_works() {
-        let result = 2 + 2;
-        assert_eq!(result, 4);
+// 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<bool> {
+    None
 }
diff --git a/dubbo/src/framework.rs b/dubbo/src/framework.rs
index 342ef6c..71e11d2 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 1a842d2..82d1554 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 4d5bb3a..b107e30 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 4a40d66..1e10a2e 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 4e56f4e..491f405 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 d620843..e01bf92 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 271a54b..a275d6d 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 888382f..2d0d669 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,

Reply via email to