Skip to content

Commit

Permalink
RPPL-1708: Add log signature (#418)
Browse files Browse the repository at this point in the history
* RPPL-1708: Added Ripple version to logger and print every 100th time
  • Loading branch information
sakshihcst authored Feb 15, 2024
1 parent 36014a4 commit 473a024
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
4 changes: 2 additions & 2 deletions core/main/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::bootstrap::boot::boot;
use ripple_sdk::{
log::{error, info},
tokio,
utils::logger::init_logger,
utils::logger::init_and_configure_logger,
};
use state::bootstrap_state::BootstrapState;
pub mod bootstrap;
Expand All @@ -33,7 +33,7 @@ include!(concat!(env!("OUT_DIR"), "/version.rs"));
#[tokio::main(worker_threads = 2)]
async fn main() {
// Init logger
if let Err(e) = init_logger("gateway".into()) {
if let Err(e) = init_and_configure_logger(SEMVER_LIGHTWEIGHT, "gateway".into()) {
println!("{:?} logger init error", e);
return;
}
Expand Down
49 changes: 48 additions & 1 deletion core/sdk/src/utils/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
// SPDX-License-Identifier: Apache-2.0
//

use std::str::FromStr;
use std::{str::FromStr, sync::atomic::AtomicU32};

pub static LOG_COUNTER: AtomicU32 = AtomicU32::new(0);

pub fn init_logger(name: String) -> Result<(), fern::InitError> {
let log_string: String = std::env::var("RUST_LOG").unwrap_or_else(|_| "debug".into());
Expand Down Expand Up @@ -46,3 +48,48 @@ pub fn init_logger(name: String) -> Result<(), fern::InitError> {
.apply()?;
Ok(())
}

pub fn init_and_configure_logger(version: &str, name: String) -> Result<(), fern::InitError> {
let log_string: String = std::env::var("RUST_LOG").unwrap_or_else(|_| "debug".into());
println!("log level {}", log_string);
let _version_string = version.to_string();
let filter = log::LevelFilter::from_str(&log_string).unwrap_or(log::LevelFilter::Info);
fern::Dispatch::new()
.format(move |out, message, record| {
let _v = LOG_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
#[cfg(not(feature = "sysd"))]
return out.finish(format_args!(
"{}[{}][{}][{}][{}]-{}",
chrono::Local::now().format("%Y-%m-%d-%H:%M:%S.%3f"),
record.level(),
record.target(),
name,
_version_string,
message
));
#[cfg(feature = "sysd")]
if _v % 100 == 0 {
LOG_COUNTER.store(0, std::sync::atomic::Ordering::Relaxed);
return out.finish(format_args!(
"[{}][{}][{}][{}]-{}",
record.level(),
record.target(),
name,
_version_string,
message
));
} else {
return out.finish(format_args!(
"[{}][{}][{}]-{}",
record.level(),
record.target(),
name,
message
));
}
})
.level(filter)
.chain(std::io::stdout())
.apply()?;
Ok(())
}

0 comments on commit 473a024

Please sign in to comment.