Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ftr: add common/logger sub package #120

Merged
merged 7 commits into from
Feb 28, 2023
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ members = [
"registry-zookeeper",
"registry-nacos",
"metadata",
"common",
"config",
"dubbo",
"examples/echo",
"examples/greeter",
"dubbo-build"
"dubbo-build",
"common/logger"
]

7 changes: 3 additions & 4 deletions common/Cargo.toml → common/logger/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
File renamed without changes.
3 changes: 3 additions & 0 deletions common/logger/README.md
Original file line number Diff line number Diff line change
@@ -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
17 changes: 14 additions & 3 deletions common/src/lib.rs → common/logger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
43 changes: 43 additions & 0 deletions common/logger/src/tracing_configurer.rs
Original file line number Diff line number Diff line change
@@ -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<bool> {
None
}
1 change: 0 additions & 1 deletion dubbo/src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ pub struct Dubbo {

impl Dubbo {
pub fn new() -> Dubbo {
tracing_subscriber::fmt::init();
Self {
protocols: HashMap::new(),
registries: None,
Expand Down
1 change: 1 addition & 0 deletions examples/echo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}

Expand Down
1 change: 1 addition & 0 deletions examples/echo/src/echo/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions examples/echo/src/echo/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl Filter for FakeFilter {

#[tokio::main]
async fn main() {
logger::init();
register_server(EchoServerImpl {
name: "echo".to_string(),
});
Expand Down
3 changes: 1 addition & 2 deletions examples/greeter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
13 changes: 1 addition & 12 deletions examples/greeter/src/greeter/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions examples/greeter/src/greeter/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions registry-nacos/src/nacos_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down