Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
feat(logger): add a json macro to generate json object (#455)
Browse files Browse the repository at this point in the history
* refactor: add a json macro

* add an example

* update doc

* fix CI
  • Loading branch information
Eason Gao authored Sep 15, 2020
1 parent 12c7121 commit ffb1b45
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
1 change: 1 addition & 0 deletions common/logger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ repository = "https://github.com/nervosnetwork/muta"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
env_logger = "0.7"
log = "0.4"
# Turn off gzip feature, it hurts performance. For more information, reference
# log4rs document.
Expand Down
2 changes: 1 addition & 1 deletion common/logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ to identify this event. `Context` is used to extract trace id. `msg` is `JsonVal
Useage example:

```rust
common_logger::log(Level::Info, "network", "netw0001", &ctx, common_logger::object!{"music": "beautiful world"});
common_logger::log(Level::Info, "network", "netw0001", &ctx, common_logger::json!({"music", "beautiful world"; "movie", "fury"}));
```

## Yaml File
Expand Down
37 changes: 37 additions & 0 deletions common/logger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ pub use json::array;
pub use json::object;
use log4rs::append::file::FileAppender;

// Example
// ```rust
// let json_obj = json!({
// "key_01", value_01;
// "key_02", value_02;
// });
// ```
#[macro_export]
macro_rules! json {
({$($key: expr, $value: expr); *}) => {{
let mut evt = JsonValue::new_object();
$(evt[$key] = $value.into();)*
evt
}};
}

pub fn init<S: ::std::hash::BuildHasher>(
filter: String,
log_to_console: bool,
Expand Down Expand Up @@ -181,3 +197,24 @@ fn trace_context(ctx: &Context) -> Option<TraceContext> {
_ => None,
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_json() {
env_logger::init();
let json = json!({"height", 1; "msg", "asset_01"; "is_connected", true});
log(
Level::Warn,
"logger",
"logg_001",
&Context::new(),
json.clone(),
);
assert_eq!(json["height"], 1);
assert_eq!(json["msg"], "asset_01");
assert_eq!(json["is_connected"], true);
}
}
32 changes: 14 additions & 18 deletions core/consensus/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use rlp::Encodable;

use common_apm::muta_apm;
use common_crypto::BlsPublicKey;
use common_logger::log;
use common_logger::{json, log};
use common_merkle::Merkle;

use protocol::fixed_codec::FixedCodec;
Expand Down Expand Up @@ -486,25 +486,21 @@ impl<Adapter: ConsensusAdapter + 'static> Engine<FixedPill> for ConsensusEngine<
}

fn report_view_change(&self, cx: Context, height: u64, round: u64, reason: ViewChangeReason) {
let mut evt = JsonValue::new_object();
evt["height"] = height.into();
evt["round"] = round.into();

match reason {
let view_change_reason = match reason {
ViewChangeReason::CheckBlockNotPass => {
let err = {
let e = self.last_check_block_fail_reason.read();
reason.to_string() + " " + e.as_str()
};

evt["reason"] = err.into();
log(log::Level::Warn, "consensus", "cons000", &cx, evt);
let e = self.last_check_block_fail_reason.read();
reason.to_string() + " " + e.as_str()
}
_ => {
evt["reason"] = reason.to_string().into();
log(log::Level::Warn, "consensus", "cons000", &cx, evt);
}
}
_ => reason.to_string(),
};

log(
log::Level::Warn,
"consensus",
"cons000",
&cx,
json!({"height", height; "round", round; "reason", view_change_reason}),
);
}
}

Expand Down

0 comments on commit ffb1b45

Please sign in to comment.