Skip to content

Commit

Permalink
Ftr: add common/utils subpackage (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
mozhou-tech authored Mar 2, 2023
1 parent 78148be commit abfc747
Show file tree
Hide file tree
Showing 24 changed files with 535 additions and 146 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,5 @@ jobs:
../../target/debug/greeter-client
env:
ZOOKEEPER_SERVERS: 127.0.0.1:2181
DUBBO_CONFIG_PATH: ./dubbo.yaml
DUBBO_CONFIG_PATH: ./application.yaml
working-directory: examples/greeter
10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[workspace]
members = [
"common/logger",
"common/utils",
"xds",
"registry/zookeeper",
"registry/nacos",
Expand All @@ -24,12 +26,16 @@ async-trait = "0.1"
dashmap = "5"
lazy_static = "1"
futures = "0.3"
tracing = "0.1"
tracing-subscriber = "0.3.15"
serde = "1"
serde_json = "1"
urlencoding = "2.1.2"
logger = {path="./common/logger"}
utils = {path="./common/utils"}
anyhow = "1.0.66"
dubbo = { path = "./dubbo/" }
bb8 = "0.8.0" # A connecton pool based on tokio
serde_yaml = "0.9.4" # yaml file parser
once_cell = "1.16.0"
itertools = "0.10.1"


19 changes: 19 additions & 0 deletions application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
logging:
level: debug
dubbo:
protocols:
triple:
ip: 0.0.0.0
port: '8888'
name: tri
registries:
demoZK:
protocol: zookeeper
address: 0.0.0.0:2181
provider:
services:
GreeterProvider:
version: 1.0.0
group: test
protocol: triple
interface: org.apache.dubbo.sample.tri.Greeter
4 changes: 3 additions & 1 deletion common/logger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ edition = "2021"

[dependencies]
tracing = "0.1"
tracing-subscriber = "0.3"
tracing-subscriber = "0.3"
once_cell.workspace = true
utils.workspace = true
39 changes: 39 additions & 0 deletions common/logger/src/level.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.
*/
use crate::Level;

pub(crate) struct LevelWrapper {
pub(crate) inner: Level,
}
impl LevelWrapper {
pub fn new(level: Level) -> Self {
LevelWrapper { inner: level }
}
}

impl From<Option<String>> for LevelWrapper {
fn from(s: Option<String>) -> Self {
match s.unwrap().to_lowercase().as_str().trim() {
"error" => LevelWrapper::new(Level::ERROR),
"warn" => LevelWrapper::new(Level::WARN),
"info" => LevelWrapper::new(Level::INFO),
"debug" => LevelWrapper::new(Level::DEBUG),
"trace" => LevelWrapper::new(Level::TRACE),
_ => LevelWrapper::new(Level::INFO),
}
}
}
16 changes: 13 additions & 3 deletions common/logger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,32 @@
* limitations under the License.
*/

use std::sync::atomic::{AtomicBool, Ordering};

pub use tracing::{self, Level};

// flag for the sake of avoiding multiple initialization
static TRACING_CONFIGURED: AtomicBool = AtomicBool::new(false);

mod level;
mod tracing_configurer;

// put on main method
pub fn init() {
tracing_configurer::default()
if !TRACING_CONFIGURED.load(Ordering::SeqCst) {
tracing_configurer::default()
}
}

#[cfg(test)]
mod tests {
use tracing::debug;
use tracing::{debug, info, trace};

#[test]
fn test_print_info_log() {
super::init();
debug!("hello rust");
debug!("hello rust:debug");
trace!("hello rust:trace");
info!("hello rust:info");
}
}
37 changes: 17 additions & 20 deletions common/logger/src/tracing_configurer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,24 @@

// https://github.com/tokio-rs/tracing/issues/971

use tracing::{debug, Level};
use crate::level::LevelWrapper;
use std::path::PathBuf;
use tracing::debug;
use utils::{path_util, yaml_util};

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.");
}
let path_buf = PathBuf::new()
.join(path_util::app_root_dir())
.join("application.yaml");
let level: LevelWrapper = yaml_util::yaml_key_reader(path_buf, "logging.level")
.unwrap()
.into();
tracing_subscriber::fmt()
.compact()
.with_line_number(true)
.with_max_level(level.inner)
// 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
}
13 changes: 13 additions & 0 deletions common/utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "utils"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde_yaml.workspace = true
serde = { workspace = true, features = ["derive"] }
project-root = "0.2.2"
anyhow.workspace=true
once_cell.workspace = true
Loading

0 comments on commit abfc747

Please sign in to comment.