-
Notifications
You must be signed in to change notification settings - Fork 79
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
- Loading branch information
1 parent
be53aec
commit c637672
Showing
5 changed files
with
109 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use colored::Colorize; | ||
use itertools::Itertools; | ||
use jsonrpc_core::{Middleware, Metadata, Request, Response, middleware, FutureResponse, Call, Params}; | ||
use futures::future::Either; | ||
use futures::Future; | ||
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, | ||
{ | ||
match &request { | ||
Request::Single(call) => { | ||
match call { | ||
Call::MethodCall(method_call) => { | ||
match self.log_level_filter { | ||
LevelFilter::Debug => { | ||
let full_params = match &method_call.params { | ||
Params::Array(values) => { | ||
if values.len() == 0 { | ||
String::default() | ||
} else { | ||
format!("with [{}]", values.iter().join(", ")) | ||
} | ||
|
||
}, | ||
_ => String::default() | ||
}; | ||
|
||
log::debug!( | ||
"{} 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.len() == 0 { | ||
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))) | ||
} | ||
} |
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