From ffb1b45159bad2d444f81b44ab57fae0dca16550 Mon Sep 17 00:00:00 2001 From: Eason Gao Date: Tue, 15 Sep 2020 16:48:59 +0800 Subject: [PATCH] feat(logger): add a json macro to generate json object (#455) * refactor: add a json macro * add an example * update doc * fix CI --- common/logger/Cargo.toml | 1 + common/logger/README.md | 2 +- common/logger/src/lib.rs | 37 ++++++++++++++++++++++++++++++++++++ core/consensus/src/engine.rs | 32 ++++++++++++++----------------- 4 files changed, 53 insertions(+), 19 deletions(-) diff --git a/common/logger/Cargo.toml b/common/logger/Cargo.toml index 8eb9206cc..9394926da 100644 --- a/common/logger/Cargo.toml +++ b/common/logger/Cargo.toml @@ -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. diff --git a/common/logger/README.md b/common/logger/README.md index 0c4a8b68a..64148ef60 100644 --- a/common/logger/README.md +++ b/common/logger/README.md @@ -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 diff --git a/common/logger/src/lib.rs b/common/logger/src/lib.rs index a45ae2287..ca49eed59 100644 --- a/common/logger/src/lib.rs +++ b/common/logger/src/lib.rs @@ -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( filter: String, log_to_console: bool, @@ -181,3 +197,24 @@ fn trace_context(ctx: &Context) -> Option { _ => 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); + } +} diff --git a/core/consensus/src/engine.rs b/core/consensus/src/engine.rs index 616cd20ec..ba59bf008 100644 --- a/core/consensus/src/engine.rs +++ b/core/consensus/src/engine.rs @@ -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; @@ -486,25 +486,21 @@ impl Engine 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}), + ); } }