Skip to content

Commit

Permalink
add l1_chain_id in log json format
Browse files Browse the repository at this point in the history
  • Loading branch information
kafeikui committed May 7, 2024
1 parent 3ec06b1 commit 9b1360b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
17 changes: 13 additions & 4 deletions crates/arpa-node/src/node_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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),
Expand Down Expand Up @@ -169,10 +175,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

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(),
Expand Down
15 changes: 11 additions & 4 deletions crates/core/src/log/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn to_checksum(addr: &Address, chain_id: Option<u8>) -> 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());
Expand Down

0 comments on commit 9b1360b

Please sign in to comment.