Use log_enabled! in loggers.rs
-use itertools::Itertools;
-
+//! To enable all logs, add to RUST_LOG env variable:
+//! actix_web=info,server::api_server::loggers=info
+//!
+//! You can also cherry-pick, like print only logs for websocket handshake
+//! and not for http rpc or api_server:
+//! server::api_server::loggers::ws_rpc=info,
+
+/// We print cf-connecting-ip and cf-ipcountry in every request.
+/// This is the list of headers that are not too much relevant,
+/// but still will be printed at the end of log message.
+/// You can add headers to that constant, reorder them, delete them.
const OTHER_HEADERS: [&str; 19] = [
"user-agent",
"cf-request-id",
@@ -23,7 +32,8 @@ const OTHER_HEADERS: [&str; 19] = [
];
pub mod rest {
- use super::*;
+ use super::OTHER_HEADERS;
+ use itertools::Itertools;
pub fn get_logger_format() -> String {
let other_headers_formatted = OTHER_HEADERS
@@ -31,6 +41,8 @@ pub mod rest {
.map(|&h| format!("{}: \"%{{{}}}i\"", h, h))
.join(", ");
+ // Looks like
+ // req { "GET /endpoint" from 192.168.0.1, US }, resp { code: 200, bytes: 10, duration: 0.01ms }, req headers { name: "value", name: "value" }
format!(
"req {{ \"%r\" from %{{cf-connecting-ip}}i, %{{cf-ipcountry}}i }}, \
resp {{ code: %s, bytes: %b, duration: %Dms }}, req headers {{ {} }}",
@@ -40,12 +52,15 @@ pub mod rest {
}
pub mod http_rpc {
- use super::*;
+ use super::OTHER_HEADERS;
+ use itertools::Itertools;
use jsonrpc_http_server::hyper;
use jsonrpc_http_server::hyper::http::HeaderValue;
use jsonrpc_http_server::RequestMiddlewareAction;
+ use log::Level;
pub fn request_middleware(request: hyper::Request<hyper::Body>) -> RequestMiddlewareAction {
+ if log_enabled!(Level::Info) {
let get_header = |header| {
request
.headers()
@@ -67,19 +82,22 @@ pub mod http_rpc {
get_header("cf-ipcountry"),
other_headers_formatted,
);
+ }
request.into()
}
}
pub mod ws_rpc {
- use super::*;
- use jsonrpc_ws_server::ws::Request;
- use jsonrpc_ws_server::ws::Response;
+ use super::OTHER_HEADERS;
+ use itertools::Itertools;
+ use jsonrpc_ws_server::ws::{Request, Response};
+ use log::Level;
use std::collections::HashMap;
use std::ops::Deref;
pub fn request_middleware(request: &Request) -> Option<Response> {
+ if log_enabled!(Level::Info) {
let mut headers = HashMap::with_capacity(request.headers().len());
for (k, v) in request.headers() {
@@ -100,6 +118,7 @@ pub mod ws_rpc {
get_header("cf-ipcountry"),
other_headers_formatted,
);
+ }
None
}
