-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add logs for incoming API requests (#118)
* feat: add logs for incoming API requests * chore: update unimplemented method log to warning * chore: add trace LogLevel. Update incoming request logs with full params to Trace. Fix unit test. * chore: add Trace log for API responses
- Loading branch information
1 parent
599758f
commit 19b7cca
Showing
7 changed files
with
111 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,6 @@ | ||
[package] | ||
name = "era_test_node" | ||
version = "0.1.0" | ||
version = "0.1.0-alpha.3" | ||
edition = "2018" | ||
authors = ["The Matter Labs Team <[email protected]>"] | ||
homepage = "https://zksync.io/" | ||
|
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,93 @@ | ||
use colored::Colorize; | ||
use futures::Future; | ||
use futures::{future::Either, FutureExt}; | ||
use itertools::Itertools; | ||
use jsonrpc_core::{ | ||
middleware, Call, FutureResponse, Metadata, Middleware, Params, Request, Response, | ||
}; | ||
use log::LevelFilter; | ||
|
||
#[derive(Clone, Debug, Default)] | ||
pub struct Meta(); | ||
impl Metadata for Meta {} | ||
|
||
pub struct LoggingMiddleware { | ||
log_level_filter: LevelFilter, | ||
} | ||
|
||
impl LoggingMiddleware { | ||
pub fn new(log_level_filter: LevelFilter) -> Self { | ||
Self { log_level_filter } | ||
} | ||
} | ||
|
||
/// Logging Middleware for all in-bound requests | ||
/// Logs out incoming requests and their parameters | ||
/// Useful for debugging applications that are pointed at this service | ||
impl Middleware<Meta> for LoggingMiddleware { | ||
type Future = FutureResponse; | ||
type CallFuture = middleware::NoopCallFuture; | ||
|
||
fn on_request<F, X>(&self, request: Request, meta: Meta, next: F) -> Either<Self::Future, X> | ||
where | ||
F: FnOnce(Request, Meta) -> X + Send, | ||
X: Future<Output = Option<Response>> + Send + 'static, | ||
{ | ||
if let Request::Single(Call::MethodCall(method_call)) = &request { | ||
match self.log_level_filter { | ||
LevelFilter::Trace => { | ||
let full_params = match &method_call.params { | ||
Params::Array(values) => { | ||
if values.is_empty() { | ||
String::default() | ||
} else { | ||
format!("with [{}]", values.iter().join(", ")) | ||
} | ||
} | ||
_ => String::default(), | ||
}; | ||
|
||
log::trace!("{} was called {}", method_call.method.cyan(), full_params); | ||
} | ||
_ => { | ||
// Generate truncated params for requests with massive payloads | ||
let truncated_params = match &method_call.params { | ||
Params::Array(values) => { | ||
if values.is_empty() { | ||
String::default() | ||
} else { | ||
format!( | ||
"with [{}]", | ||
values | ||
.iter() | ||
.map(|s| { | ||
let s_str = s.to_string(); | ||
if s_str.len() > 70 { | ||
format!("{:.67}...", s_str) | ||
} else { | ||
s_str | ||
} | ||
}) | ||
.collect::<Vec<String>>() | ||
.join(", ") | ||
) | ||
} | ||
} | ||
_ => String::default(), | ||
}; | ||
|
||
log::info!( | ||
"{} was called {}", | ||
method_call.method.cyan(), | ||
truncated_params | ||
); | ||
} | ||
} | ||
}; | ||
|
||
Either::Left(Box::pin(next(request, meta).map(move |res| { | ||
log::trace!("API response => {:?}", res); | ||
res | ||
}))) | ||
} | ||
} |
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