Skip to content

Commit 4404c7e

Browse files
authored
migrate to slog (#298)
* migrate to slog Signed-off-by: Shashwat Jaiswal <shashwatjaiswal2001@gmail.com> * added optional logger while creating client Signed-off-by: Shashwat Jaiswal <shashwatjaiswal2001@gmail.com> * simplified unwrap expression Signed-off-by: Shashwat Jaiswal <shashwatjaiswal2001@gmail.com> * fixed rustfmt Signed-off-by: Shashwat Jaiswal <shashwatjaiswal2001@gmail.com>
1 parent 24fa268 commit 4404c7e

17 files changed

+190
-122
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ rand = "0.8"
3131
regex = "1"
3232
serde = "1.0"
3333
serde_derive = "1.0"
34+
slog = { version = "2.3", features = ["max_level_trace", "release_max_level_debug"] }
35+
slog-term = { version = "2.4" }
3436
thiserror = "1"
3537
tokio = { version = "1.0", features = [ "sync", "time" ] }
3638

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Raw mode:
3333
```rust
3434
use tikv_client::RawClient;
3535

36-
let client = RawClient::new(vec!["127.0.0.1:2379"]).await?;
36+
let client = RawClient::new(vec!["127.0.0.1:2379"], None).await?;
3737
client.put("key".to_owned(), "value".to_owned()).await?;
3838
let value = client.get("key".to_owned()).await?;
3939
```
@@ -43,7 +43,7 @@ Transactional mode:
4343
```rust
4444
use tikv_client::TransactionClient;
4545

46-
let txn_client = TransactionClient::new(vec!["127.0.0.1:2379"]).await?;
46+
let txn_client = TransactionClient::new(vec!["127.0.0.1:2379"], None).await?;
4747
let mut txn = txn_client.begin_optimistic().await?;
4848
txn.put("key".to_owned(), "value".to_owned()).await?;
4949
let value = txn.get("key".to_owned()).await?;

examples/pessimistic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async fn main() {
2020
};
2121

2222
// init
23-
let client = Client::new_with_config(args.pd, config)
23+
let client = Client::new_with_config(args.pd, config, None)
2424
.await
2525
.expect("Could not connect to tikv");
2626

examples/raw.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async fn main() -> Result<()> {
2626

2727
// When we first create a client we receive a `Connect` structure which must be resolved before
2828
// the client is actually connected and usable.
29-
let client = Client::new_with_config(args.pd, config).await?;
29+
let client = Client::new_with_config(args.pd, config, None).await?;
3030

3131
// Requests are created from the connected client. These calls return structures which
3232
// implement `Future`. This means the `Future` must be resolved before the action ever takes

examples/transaction.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ async fn main() {
8181
Config::default()
8282
};
8383

84-
let txn = Client::new_with_config(args.pd, config)
84+
let txn = Client::new_with_config(args.pd, config, None)
8585
.await
8686
.expect("Could not connect to tikv");
8787

getting-started.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Raw mode:
2323
```rust
2424
use tikv_client::RawClient;
2525

26-
let client = RawClient::new(vec!["127.0.0.1:2379"]).await?;
26+
let client = RawClient::new(vec!["127.0.0.1:2379"], None).await?;
2727
client.put("key".to_owned(), "value".to_owned()).await?;
2828
let value = client.get("key".to_owned()).await?;
2929
```
@@ -33,7 +33,7 @@ Transactional mode:
3333
```rust
3434
use tikv_client::TransactionClient;
3535

36-
let txn_client = TransactionClient::new(vec!["127.0.0.1:2379"]).await?;
36+
let txn_client = TransactionClient::new(vec!["127.0.0.1:2379"], None).await?;
3737
let mut txn = txn_client.begin_optimistic().await?;
3838
txn.put("key".to_owned(), "value".to_owned()).await?;
3939
let value = txn.get("key".to_owned()).await?;
@@ -46,7 +46,7 @@ To make an example which builds and runs,
4646
use tikv_client::{TransactionClient, Error};
4747

4848
async fn run() -> Result<(), Error> {
49-
let txn_client = TransactionClient::new(vec!["127.0.0.1:2379"]).await?;
49+
let txn_client = TransactionClient::new(vec!["127.0.0.1:2379"], None).await?;
5050
let mut txn = txn_client.begin_optimistic().await?;
5151
txn.put("key".to_owned(), "value".to_owned()).await?;
5252
let value = txn.get("key".to_owned()).await?;

src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
//! # use futures::prelude::*;
6868
//! # fn main() -> Result<()> {
6969
//! # futures::executor::block_on(async {
70-
//! let client = RawClient::new(vec!["127.0.0.1:2379"]).await?;
70+
//! let client = RawClient::new(vec!["127.0.0.1:2379"], None).await?;
7171
//! client.put("key".to_owned(), "value".to_owned()).await?;
7272
//! let value = client.get("key".to_owned()).await?;
7373
//! # Ok(())
@@ -81,7 +81,7 @@
8181
//! # use futures::prelude::*;
8282
//! # fn main() -> Result<()> {
8383
//! # futures::executor::block_on(async {
84-
//! let txn_client = TransactionClient::new(vec!["127.0.0.1:2379"]).await?;
84+
//! let txn_client = TransactionClient::new(vec!["127.0.0.1:2379"], None).await?;
8585
//! let mut txn = txn_client.begin_optimistic().await?;
8686
//! txn.put("key".to_owned(), "value".to_owned()).await?;
8787
//! let value = txn.get("key".to_owned()).await?;
@@ -115,7 +115,8 @@ mod mock;
115115
mod proptests;
116116

117117
#[macro_use]
118-
extern crate log;
118+
extern crate slog;
119+
extern crate slog_term;
119120

120121
#[doc(inline)]
121122
pub use crate::backoff::Backoff;

src/pd/client.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::{
1111
use async_trait::async_trait;
1212
use futures::{prelude::*, stream::BoxStream};
1313
use grpcio::{EnvBuilder, Environment};
14+
use slog::{Drain, Logger};
1415
use std::{
1516
collections::HashMap,
1617
sync::{Arc, RwLock},
@@ -207,6 +208,7 @@ pub struct PdRpcClient<KvC: KvConnect + Send + Sync + 'static = TikvConnect, Cl
207208
kv_connect: KvC,
208209
kv_client_cache: Arc<RwLock<HashMap<String, KvC::KvClient>>>,
209210
enable_codec: bool,
211+
logger: Logger,
210212
}
211213

212214
#[async_trait]
@@ -284,6 +286,9 @@ impl<KvC: KvConnect + Send + Sync + 'static, Cl> PdRpcClient<KvC, Cl> {
284286
MakeKvC: FnOnce(Arc<Environment>, Arc<SecurityManager>) -> KvC,
285287
MakePd: FnOnce(Arc<Environment>, Arc<SecurityManager>) -> PdFut,
286288
{
289+
let plain = slog_term::PlainSyncDecorator::new(std::io::stdout());
290+
let logger = Logger::root(slog_term::FullFormat::new(plain).build().fuse(), o!());
291+
info!(logger, "Logging ready!");
287292
let env = Arc::new(
288293
EnvBuilder::new()
289294
.cq_count(CQ_COUNT)
@@ -307,14 +312,15 @@ impl<KvC: KvConnect + Send + Sync + 'static, Cl> PdRpcClient<KvC, Cl> {
307312
kv_client_cache,
308313
kv_connect: kv_connect(env, security_mgr),
309314
enable_codec,
315+
logger,
310316
})
311317
}
312318

313319
fn kv_client(&self, address: &str) -> Result<KvC::KvClient> {
314320
if let Some(client) = self.kv_client_cache.read().unwrap().get(address) {
315321
return Ok(client.clone());
316322
};
317-
info!("connect to tikv endpoint: {:?}", address);
323+
info!(self.logger, "connect to tikv endpoint: {:?}", address);
318324
self.kv_connect.connect(address).map(|client| {
319325
self.kv_client_cache
320326
.write()

0 commit comments

Comments
 (0)