Skip to content

Commit b041046

Browse files
committed
refactor:sdk日志单独打印
1 parent 3e71e04 commit b041046

File tree

25 files changed

+418
-145
lines changed

25 files changed

+418
-145
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ tonic = {version = "0.11.0"}
5959

6060
# logging
6161
tracing = {version = "0.1.36"}
62+
tracing-appender = {version = "0.2.3"}
63+
tracing-subscriber = {version = "0.3", features = ["default"]}
6264

6365
# crypto
6466
aes = {version = "0.7.4"}
@@ -69,7 +71,6 @@ rand = {version = "0.8.4"}
6971
rsa = {version = "0.9.6"}
7072

7173
[dev-dependencies]
72-
tracing-subscriber = {version = "0.3", features = ["default"]}
7374

7475
[[example]]
7576
name = "discover"

examples/config.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,19 @@
1515

1616
use std::{collections::HashMap, sync::Arc, time::Duration};
1717

18-
use polaris_rust::{
19-
config::{
20-
api::{new_config_file_api_by_context, ConfigFileAPI},
21-
req::{
22-
CreateConfigFileRequest, PublishConfigFileRequest, UpdateConfigFileRequest,
23-
UpsertAndPublishConfigFileRequest, WatchConfigFileRequest,
24-
},
18+
use polaris_rust::{config::{
19+
api::{new_config_file_api_by_context, ConfigFileAPI},
20+
req::{
21+
CreateConfigFileRequest, PublishConfigFileRequest, UpdateConfigFileRequest,
22+
UpsertAndPublishConfigFileRequest, WatchConfigFileRequest,
2523
},
26-
core::{
27-
context::SDKContext,
28-
model::{
29-
config::{ConfigFile, ConfigFileRelease},
30-
error::PolarisError,
31-
},
24+
}, core::{
25+
context::SDKContext,
26+
model::{
27+
config::{ConfigFile, ConfigFileRelease},
28+
error::PolarisError,
3229
},
33-
};
30+
}, info};
3431
use tracing::level_filters::LevelFilter;
3532

3633
#[tokio::main]
@@ -163,7 +160,7 @@ async fn main() -> Result<(), PolarisError> {
163160
group: "rust".to_string(),
164161
file: "rust.toml".to_string(),
165162
call_back: Arc::new(|event| {
166-
tracing::info!("receive config change event: {:?}", event);
163+
info!("receive config change event: {:?}", event);
167164
}),
168165
})
169166
.await;

examples/discover.rs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,16 @@
1515

1616
use std::{collections::HashMap, sync::Arc, time::Duration};
1717

18-
use polaris_rust::{
19-
core::{
20-
context::SDKContext,
21-
model::{error::PolarisError, naming::Location},
18+
use polaris_rust::{core::{
19+
context::SDKContext,
20+
model::{error::PolarisError, naming::Location},
21+
}, discovery::{
22+
api::{new_consumer_api_by_context, new_provider_api_by_context, ConsumerAPI, ProviderAPI},
23+
req::{
24+
GetAllInstanceRequest, InstanceDeregisterRequest, InstanceRegisterRequest,
25+
WatchInstanceRequest,
2226
},
23-
discovery::{
24-
api::{new_consumer_api_by_context, new_provider_api_by_context, ConsumerAPI, ProviderAPI},
25-
req::{
26-
GetAllInstanceRequest, InstanceDeregisterRequest, InstanceRegisterRequest,
27-
WatchInstanceRequest,
28-
},
29-
},
30-
};
27+
}, error, info};
3128
use tracing::level_filters::LevelFilter;
3229

3330
#[tokio::main]
@@ -48,7 +45,7 @@ async fn main() -> Result<(), PolarisError> {
4845

4946
let sdk_context_ret = SDKContext::default();
5047
if sdk_context_ret.is_err() {
51-
tracing::error!(
48+
error!(
5249
"create sdk context fail: {}",
5350
sdk_context_ret.err().unwrap()
5451
);
@@ -61,7 +58,7 @@ async fn main() -> Result<(), PolarisError> {
6158

6259
let provider_ret = new_provider_api_by_context(arc_ctx.clone());
6360
if provider_ret.is_err() {
64-
tracing::error!("create provider fail: {}", provider_ret.err().unwrap());
61+
error!("create provider fail: {}", provider_ret.err().unwrap());
6562
return Err(PolarisError::new(
6663
polaris_rust::core::model::error::ErrorCode::UnknownServerError,
6764
"".to_string(),
@@ -70,7 +67,7 @@ async fn main() -> Result<(), PolarisError> {
7067

7168
let consumer_ret = new_consumer_api_by_context(arc_ctx);
7269
if consumer_ret.is_err() {
73-
tracing::error!("create consumer fail: {}", consumer_ret.err().unwrap());
70+
error!("create consumer fail: {}", consumer_ret.err().unwrap());
7471
return Err(PolarisError::new(
7572
polaris_rust::core::model::error::ErrorCode::UnknownServerError,
7673
"".to_string(),
@@ -80,7 +77,7 @@ async fn main() -> Result<(), PolarisError> {
8077
let provider = provider_ret.unwrap();
8178
let consumer = consumer_ret.unwrap();
8279

83-
tracing::info!(
80+
info!(
8481
"create discovery api client cost: {:?}",
8582
start_time.elapsed()
8683
);
@@ -114,25 +111,25 @@ async fn main() -> Result<(), PolarisError> {
114111
let _ret = provider.register(req).await;
115112
match _ret {
116113
Err(err) => {
117-
tracing::error!("register fail: {}", err.to_string());
114+
error!("register fail: {}", err.to_string());
118115
}
119116
Ok(_) => {}
120117
}
121118

122-
tracing::info!("begin do watch service_instances change");
119+
info!("begin do watch service_instances change");
123120
let watch_rsp = consumer
124121
.watch_instance(WatchInstanceRequest {
125122
namespace: "rust-demo".to_string(),
126123
service: "polaris-rust-provider".to_string(),
127124
call_back: Arc::new(|instances| {
128-
tracing::info!("watch instance: {:?}", instances.instances);
125+
info!("watch instance: {:?}", instances.instances);
129126
}),
130127
})
131128
.await;
132129

133130
match watch_rsp {
134131
Err(err) => {
135-
tracing::error!("watch instance fail: {}", err.to_string());
132+
error!("watch instance fail: {}", err.to_string());
136133
}
137134
Ok(_) => {}
138135
}
@@ -148,10 +145,10 @@ async fn main() -> Result<(), PolarisError> {
148145

149146
match instances_ret {
150147
Err(err) => {
151-
tracing::error!("get all instance fail: {}", err.to_string());
148+
error!("get all instance fail: {}", err.to_string());
152149
}
153150
Ok(instances) => {
154-
tracing::info!("get all instance: {:?}", instances);
151+
info!("get all instance: {:?}", instances);
155152
}
156153
}
157154

@@ -173,7 +170,7 @@ async fn main() -> Result<(), PolarisError> {
173170
let _ret = provider.deregister(deregister_req).await;
174171
match _ret {
175172
Err(err) => {
176-
tracing::error!("deregister fail: {}", err.to_string());
173+
error!("deregister fail: {}", err.to_string());
177174
}
178175
Ok(_) => {}
179176
}

src/config/api.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@
1515

1616
use std::sync::Arc;
1717

18-
use crate::{
19-
config::default::{DefaultConfigFileAPI, DefaultConfigGroupAPI},
20-
core::{
21-
context::SDKContext,
22-
model::{
23-
config::{ConfigFile, ConfigGroup},
24-
error::PolarisError,
25-
},
18+
use crate::{config::default::{DefaultConfigFileAPI, DefaultConfigGroupAPI}, core::{
19+
context::SDKContext,
20+
model::{
21+
config::{ConfigFile, ConfigGroup},
22+
error::PolarisError,
2623
},
27-
};
24+
}, info};
2825

2926
use super::req::{
3027
CreateConfigFileRequest, GetConfigFileRequest, GetConfigGroupRequest, PublishConfigFileRequest,
@@ -36,7 +33,7 @@ use super::req::{
3633
pub fn new_config_file_api() -> Result<impl ConfigFileAPI, PolarisError> {
3734
let start_time = std::time::Instant::now();
3835
let context_ret = SDKContext::default();
39-
tracing::info!("create sdk context cost: {:?}", start_time.elapsed());
36+
info!("create sdk context cost: {:?}", start_time.elapsed());
4037
if context_ret.is_err() {
4138
return Err(context_ret.err().unwrap());
4239
}
@@ -88,7 +85,7 @@ where
8885
pub fn new_config_group_api() -> Result<impl ConfigGroupAPI, PolarisError> {
8986
let start_time = std::time::Instant::now();
9087
let context_ret = SDKContext::default();
91-
tracing::info!("create sdk context cost: {:?}", start_time.elapsed());
88+
info!("create sdk context cost: {:?}", start_time.elapsed());
9289
if context_ret.is_err() {
9390
return Err(context_ret.err().unwrap());
9491
}

src/core/config/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::core::config::provider::ProviderConfig;
2020
use serde::Deserialize;
2121
use std::path::Path;
2222
use std::{env, fs, io};
23+
use crate::info;
2324

2425
#[derive(Deserialize, Debug)]
2526
#[serde(rename_all = "camelCase", deny_unknown_fields)]
@@ -38,7 +39,7 @@ pub fn load_default<'a>() -> Result<Configuration, io::Error> {
3839
}
3940
if env::var("POLARIS_RUST_CONFIG").is_ok() {
4041
let custom_conf_path = env::var("POLARIS_RUST_CONFIG").unwrap();
41-
tracing::info!("load config from env: {}", custom_conf_path);
42+
info!("load config from env: {}", custom_conf_path);
4243
return load(env::var("POLARIS_RUST_CONFIG").unwrap());
4344
}
4445
load(path)

src/core/context.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
// specific language governing permissions and limitations under the License.
1515

1616
use std::sync::Arc;
17-
17+
use tracing::log::{log, logger};
1818
use crate::core::config::config::{load_default, Configuration};
1919
use crate::core::engine::Engine;
20+
use crate::core::logger;
2021
use crate::core::model::error::{ErrorCode, PolarisError};
22+
use crate::info;
2123

2224
pub struct SDKContext {
2325
pub conf: Arc<Configuration>,
@@ -59,7 +61,8 @@ impl SDKContext {
5961
let start_time = std::time::Instant::now();
6062
let cfg = Arc::new(cfg);
6163
let ret = Engine::new(cfg.clone());
62-
tracing::info!("create engine cost: {:?}", start_time.elapsed());
64+
info!("create engine cost: {:?}", start_time.elapsed());
65+
info!("create engine cost: {:?}", start_time.elapsed());
6366
if ret.is_err() {
6467
return Err(ret.err().unwrap());
6568
}

src/core/flow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl ClientFlow {
9393
};
9494
let ret = server_connector.report_client(req).await;
9595
if let Err(e) = ret {
96-
tracing::error!("report client failed: {:?}", e);
96+
crate::error!("report client failed: {:?}", e);
9797
}
9898
}
9999

0 commit comments

Comments
 (0)