-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add dependencies * feat: implement logger * feat: add build info * feat: use logger in hermes main * fix: update logger to support only * fix: main log * fix: log error in ClockState * doc: add logger doc * fix: formatter * style: disable exit linter error * feat: add tracing to log level * fix: make bool param * fix: clean up main * fix: get env log level * feat: add serde json * feat: hermes api logger * feat: add logger to lib * fix: add entry * fix: refactor log api * fix: remove serde json * fix: clean up * fix: add context span * fix: implementt new logging api * fix: fomat * fix: remove level from api * fix: linter * fix: structure and format * fix: typo * feat: add derive more * fix: cleanup * fix: enable feat cargo toml * fix: comment out reactor wait * fix: add default feat for derive more * fix: cleanup * fix: syntax * fix: cargo toml * fix: cleanup and implement logger builder * fix: move build info log * fix: formatter * Update hermes/bin/src/main.rs Co-authored-by: Felipe Rosa <[email protected]> --------- Co-authored-by: Alex Pozhylenkov <[email protected]> Co-authored-by: Felipe Rosa <[email protected]>
- Loading branch information
1 parent
6234f29
commit 1807df6
Showing
12 changed files
with
339 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
//! Build | ||
fn main() { | ||
build_info_build::build_script(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
//! Setup for logging for the service. | ||
use std::str::FromStr; | ||
|
||
use clap::ValueEnum; | ||
extern crate derive_more; | ||
use derive_more::Display; | ||
use tracing::{level_filters::LevelFilter, subscriber::SetGlobalDefaultError}; | ||
use tracing_subscriber::{ | ||
fmt::{format::FmtSpan, time}, | ||
FmtSubscriber, | ||
}; | ||
|
||
use crate::runtime_extensions::bindings::hermes::logging::api::Level; | ||
|
||
/// All valid logging levels. | ||
#[derive(ValueEnum, Clone, Copy, Display, Default)] | ||
pub(crate) enum LogLevel { | ||
/// Errors | ||
#[display(fmt = "Error")] | ||
Error, | ||
/// Warnings | ||
#[display(fmt = "Warn")] | ||
Warn, | ||
/// Informational Messages | ||
#[display(fmt = "Info")] | ||
#[default] | ||
Info, | ||
/// Debug messages | ||
#[display(fmt = "Debug")] | ||
Debug, | ||
/// Tracing | ||
#[display(fmt = "Trace")] | ||
Trace, | ||
} | ||
|
||
impl FromStr for LogLevel { | ||
type Err = (); | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
// Error and Warn levels are force to Info level | ||
// as Info is the highest log level one can choose. | ||
match s { | ||
"error" | "warn" | "info" => Ok(LogLevel::Info), | ||
"debug" => Ok(LogLevel::Debug), | ||
"trace" => Ok(LogLevel::Trace), | ||
_ => Err(()), | ||
} | ||
} | ||
} | ||
|
||
impl From<Level> for LogLevel { | ||
fn from(level: Level) -> Self { | ||
// Error and Warn levels are force to Info level | ||
// as Info is the highest log level one can choose. | ||
match level { | ||
Level::Info | Level::Warn | Level::Error => LogLevel::Info, | ||
Level::Debug => LogLevel::Debug, | ||
Level::Trace => LogLevel::Trace, | ||
} | ||
} | ||
} | ||
|
||
/// Implements a conversion from `LogLevel` enum to the `tracing::Level`. | ||
impl From<LogLevel> for tracing::Level { | ||
fn from(val: LogLevel) -> Self { | ||
match val { | ||
LogLevel::Error => Self::ERROR, | ||
LogLevel::Warn => Self::WARN, | ||
LogLevel::Info => Self::INFO, | ||
LogLevel::Debug => Self::DEBUG, | ||
LogLevel::Trace => Self::TRACE, | ||
} | ||
} | ||
} | ||
|
||
/// Logger configuration. | ||
#[derive(Default)] | ||
pub(crate) struct LoggerConfig { | ||
/// Log level. | ||
log_level: LogLevel, | ||
/// Enable/disable thread logging. | ||
with_thread: bool, | ||
/// Enable/disable file logging. | ||
with_file: bool, | ||
/// Enable/disable line number logging. | ||
with_line_num: bool, | ||
} | ||
|
||
#[allow(dead_code)] | ||
impl LoggerConfig { | ||
/// Entry point to logger building. | ||
pub(crate) fn builder() -> LoggerConfigBuilder { | ||
LoggerConfigBuilder::default() | ||
} | ||
} | ||
|
||
/// Logger configuration builder. | ||
#[derive(Default)] | ||
pub(crate) struct LoggerConfigBuilder { | ||
/// Builder log level. | ||
log_level: Option<LogLevel>, | ||
/// Builder enable/disable thread logging. | ||
with_thread: Option<bool>, | ||
/// Builder enable/disable file logging. | ||
with_file: Option<bool>, | ||
/// Builder enable/disable line number logging. | ||
with_line_num: Option<bool>, | ||
} | ||
|
||
#[allow(dead_code)] | ||
impl LoggerConfigBuilder { | ||
/// Build the logger configuration. | ||
pub(crate) fn build(self) -> LoggerConfig { | ||
LoggerConfig { | ||
log_level: self.log_level.unwrap_or(LogLevel::Info), | ||
with_thread: self.with_thread.unwrap_or(false), | ||
with_file: self.with_file.unwrap_or(false), | ||
with_line_num: self.with_line_num.unwrap_or(false), | ||
} | ||
} | ||
|
||
/// Set log level. | ||
pub(crate) fn log_level(mut self, level: LogLevel) -> Self { | ||
self.log_level = Some(level); | ||
self | ||
} | ||
|
||
/// Enable/disable thread logging. | ||
pub(crate) fn with_thread(mut self, enable: bool) -> Self { | ||
self.with_thread = Some(enable); | ||
self | ||
} | ||
|
||
/// Enable/disable file logging. | ||
pub(crate) fn with_file(mut self, enable: bool) -> Self { | ||
self.with_file = Some(enable); | ||
self | ||
} | ||
|
||
/// Enable/disable line number logging. | ||
pub(crate) fn with_line_num(mut self, enable: bool) -> Self { | ||
self.with_line_num = Some(enable); | ||
self | ||
} | ||
} | ||
|
||
/// Initializes the subscriber for the logger with the following features. | ||
/// - JSON format | ||
/// - Display event level | ||
/// - Display thread names and ids | ||
/// - Display event's source code file path and line number | ||
/// - Display time in RFC 3339 format | ||
/// - Events emit when the span close | ||
/// - Maximum verbosity level | ||
#[allow(dead_code)] | ||
pub(crate) fn init(logger_config: &LoggerConfig) -> Result<(), SetGlobalDefaultError> { | ||
let subscriber = FmtSubscriber::builder() | ||
.json() | ||
.with_level(true) | ||
.with_thread_names(logger_config.with_thread) | ||
.with_thread_ids(logger_config.with_thread) | ||
.with_file(logger_config.with_file) | ||
.with_line_number(logger_config.with_line_num) | ||
.with_timer(time::UtcTime::rfc_3339()) | ||
.with_span_events(FmtSpan::CLOSE) | ||
.with_max_level(LevelFilter::from_level(logger_config.log_level.into())) | ||
.finish(); | ||
|
||
tracing::subscriber::set_global_default(subscriber) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
hermes/bin/src/runtime_extensions/hermes/logging/log_msg.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//! Implementation of logging API | ||
use tracing::info; | ||
|
||
use crate::logger::LogLevel; | ||
|
||
/// Log a message | ||
#[allow(clippy::too_many_arguments)] | ||
pub(crate) fn log_message( | ||
level: LogLevel, ctx: Option<String>, msg: &str, file: Option<String>, | ||
function: Option<String>, line: Option<u32>, col: Option<u32>, data: Option<String>, | ||
) { | ||
info!( | ||
level = level.to_string(), | ||
ctx = ctx.unwrap_or_default(), | ||
message = msg, | ||
file = file.unwrap_or_default(), | ||
function = function.unwrap_or_default(), | ||
line = line.unwrap_or_default(), | ||
column = col.unwrap_or_default(), | ||
data = data.unwrap_or_default(), | ||
); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests_log_msg { | ||
use super::*; | ||
use crate::{ | ||
logger::{init, LogLevel, LoggerConfig}, | ||
runtime_extensions::bindings::hermes::logging::api::Level, | ||
}; | ||
|
||
#[test] | ||
fn test_log_message() { | ||
let config = LoggerConfig::default(); | ||
|
||
if let Err(err) = init(&config) { | ||
println!("Error initializing logger: {err}"); | ||
} | ||
|
||
// Test with valid data | ||
let level = Level::Warn; | ||
let ctx = Some("Context".to_string()); | ||
let msg = "Test message"; | ||
let file = Some("test.rs".to_string()); | ||
let function = Some("test_log_message".to_string()); | ||
let line = Some(10); | ||
let col = Some(5); | ||
let data = Some("{\"bt\": [\"Array:1\", \"Array:2\", \"Array:3\"]}".to_string()); | ||
|
||
log_message( | ||
LogLevel::from(level), | ||
ctx.clone(), | ||
msg, | ||
file.clone(), | ||
function.clone(), | ||
line, | ||
col, | ||
data.clone(), | ||
); | ||
|
||
log_message( | ||
LogLevel::from(level), | ||
ctx, | ||
msg, | ||
file, | ||
function, | ||
line, | ||
col, | ||
None, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
//! Logging runtime extension implementation. | ||
mod host; | ||
mod log_msg; | ||
|
||
/// Advise Runtime Extensions of a new context | ||
pub(crate) fn new_context(_ctx: &crate::runtime_context::HermesRuntimeContext) {} |
Oops, something went wrong.