From 9b1360bed0b8dead8e397c9e0e79b339da2994aa Mon Sep 17 00:00:00 2001 From: kafeikui Date: Tue, 7 May 2024 21:40:02 +0800 Subject: [PATCH] add l1_chain_id in log json format --- crates/arpa-node/src/node_client.rs | 17 +++++++++++++---- crates/core/src/log/encoder.rs | 15 +++++++++++---- crates/core/src/utils/mod.rs | 2 +- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/crates/arpa-node/src/node_client.rs b/crates/arpa-node/src/node_client.rs index 0fce4ef3..f7439aff 100644 --- a/crates/arpa-node/src/node_client.rs +++ b/crates/arpa-node/src/node_client.rs @@ -52,16 +52,22 @@ pub struct Opt { config_path: PathBuf, } -fn init_logger(node_id: &str, context_logging: bool, log_file_path: &str, rolling_file_size: u64) { +fn init_logger( + node_id: &str, + l1_chain_id: usize, + context_logging: bool, + log_file_path: &str, + rolling_file_size: u64, +) { let stdout = ConsoleAppender::builder() .encoder(Box::new( - JsonEncoder::new(node_id.to_string()).context_logging(context_logging), + JsonEncoder::new(node_id.to_string(), l1_chain_id).context_logging(context_logging), )) .build(); let rolling_file = RollingFileAppender::builder() .encoder(Box::new( - JsonEncoder::new(node_id.to_string()).context_logging(context_logging), + JsonEncoder::new(node_id.to_string(), l1_chain_id).context_logging(context_logging), )) .build( format!( @@ -81,7 +87,7 @@ fn init_logger(node_id: &str, context_logging: bool, log_file_path: &str, rollin let rolling_err_file = RollingFileAppender::builder() .encoder(Box::new( - JsonEncoder::new(node_id.to_string()).context_logging(context_logging), + JsonEncoder::new(node_id.to_string(), l1_chain_id).context_logging(context_logging), )) .build( format!("{}/node_err.log", log_file_path), @@ -169,10 +175,13 @@ async fn main() -> Result<(), Box> { let id_address = wallet.address(); + let l1_chain_id = config.get_main_chain_id(); + let logger_descriptor = config.get_logger_descriptor(); init_logger( &address_to_string(id_address), + l1_chain_id, logger_descriptor.get_context_logging(), logger_descriptor.get_log_file_path(), logger_descriptor.get_rolling_file_size(), diff --git a/crates/core/src/log/encoder.rs b/crates/core/src/log/encoder.rs index 187c72bc..d792f063 100644 --- a/crates/core/src/log/encoder.rs +++ b/crates/core/src/log/encoder.rs @@ -49,14 +49,16 @@ lazy_static! { #[derive(Clone, Debug, Default)] pub struct JsonEncoder { node_id: String, + l1_chain_id: usize, show_context: bool, } impl JsonEncoder { /// Returns a new `JsonEncoder` with a default configuration. - pub fn new(node_id: String) -> Self { + pub fn new(node_id: String, l1_chain_id: usize) -> Self { JsonEncoder { node_id, + l1_chain_id, show_context: false, } } @@ -97,6 +99,7 @@ impl JsonEncoder { thread: thread.name(), thread_id: thread_id::get(), node_id: &self.node_id, + l1_chain_id: self.l1_chain_id, mdc: Mdc, node_info: &node_info, group_info: &group_info, @@ -130,6 +133,7 @@ struct Message<'a> { thread: Option<&'a str>, thread_id: usize, node_id: &'a str, + l1_chain_id: usize, mdc: Mdc, node_info: &'a str, group_info: &'a str, @@ -182,9 +186,10 @@ mod test { let message = "message"; let thread = "log::encoder::test::default"; let node_id = "test"; + let l1_chain_id = 1; log_mdc::insert("foo", "bar"); - let encoder = JsonEncoder::new(node_id.to_string()); + let encoder = JsonEncoder::new(node_id.to_string(), l1_chain_id); let mut buf = vec![]; encoder @@ -205,7 +210,8 @@ mod test { let expected = format!( "{{\"time\":\"{}\",\"message\":\"{}\",\"module_path\":\"{}\",\ \"file\":\"{}\",\"line\":{},\"level\":\"{}\",\"target\":\"{}\",\ - \"thread\":\"{}\",\"thread_id\":{},\"node_id\":\"{}\",\"mdc\":{{\"foo\":\"bar\"}},\ + \"thread\":\"{}\",\"thread_id\":{},\"node_id\":\"{}\",\"l1_chain_id\":{},\ + \"mdc\":{{\"foo\":\"bar\"}},\ \"node_info\":\"\",\"group_info\":\"\"}}", time.to_rfc3339(), message, @@ -216,7 +222,8 @@ mod test { target, thread, thread_id::get(), - node_id + node_id, + l1_chain_id ); assert_eq!(expected, String::from_utf8(buf).unwrap().trim()); } diff --git a/crates/core/src/utils/mod.rs b/crates/core/src/utils/mod.rs index 200e875c..fcfdd469 100644 --- a/crates/core/src/utils/mod.rs +++ b/crates/core/src/utils/mod.rs @@ -60,7 +60,7 @@ pub fn to_checksum(addr: &Address, chain_id: Option) -> String { Some(chain_id) => format!("{}0x{:x}", chain_id, addr), None => format!("{:x}", addr), }; - let hash = hex::encode(keccak256(&prefixed_addr)); + let hash = hex::encode(keccak256(prefixed_addr)); let hash = hash.as_bytes(); let addr_hex = hex::encode(addr.as_bytes());