From 8c367daddfeefa39f139265f63117252b959a67c Mon Sep 17 00:00:00 2001 From: Eloi DEMOLIS Date: Wed, 21 Feb 2024 15:11:46 +0100 Subject: [PATCH] Edit access logs ASCII format, put logs-cache under feature Signed-off-by: Eloi DEMOLIS --- bin/src/command/server.rs | 6 ++-- command/Cargo.toml | 1 + command/src/logging/access_logs.rs | 2 +- command/src/logging/display.rs | 16 ++++------ command/src/logging/logs.rs | 49 ++++++++++++++++++++---------- command/src/response.rs | 2 +- lib/src/tls.rs | 6 ++-- 7 files changed, 48 insertions(+), 34 deletions(-) diff --git a/bin/src/command/server.rs b/bin/src/command/server.rs index 818d08f06..a20f70c34 100644 --- a/bin/src/command/server.rs +++ b/bin/src/command/server.rs @@ -1,6 +1,6 @@ use std::{ collections::{HashMap, HashSet}, - fmt::Debug, + fmt::{self, Debug}, io::Error as IoError, ops::{Deref, DerefMut}, os::fd::{AsRawFd, FromRawFd}, @@ -858,8 +858,8 @@ impl Server { } } -impl std::fmt::Debug for Server { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl Debug for Server { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Server") .field("config", &self.config) .field("event_subscribers", &self.event_subscribers) diff --git a/command/Cargo.toml b/command/Cargo.toml index b5a63d361..b99edce3c 100644 --- a/command/Cargo.toml +++ b/command/Cargo.toml @@ -53,6 +53,7 @@ x509-parser = "^0.15.1" unstable = [] logs-debug = [] logs-trace = [] +logs-cache = [] [badges] travis-ci = { repository = "sozu-proxy/sozu" } diff --git a/command/src/logging/access_logs.rs b/command/src/logging/access_logs.rs index 68f435d70..1ec8f4bb4 100644 --- a/command/src/logging/access_logs.rs +++ b/command/src/logging/access_logs.rs @@ -31,7 +31,7 @@ impl DuplicateOwnership for &T { impl<'a, T> DuplicateOwnership for Option<&'a T> where T: ?Sized, - &'a T: DuplicateOwnership + 'a, + &'a T: DuplicateOwnership, { type Target = Option<<&'a T as DuplicateOwnership>::Target>; unsafe fn duplicate(self) -> Self::Target { diff --git a/command/src/logging/display.rs b/command/src/logging/display.rs index a812995af..287df6669 100644 --- a/command/src/logging/display.rs +++ b/command/src/logging/display.rs @@ -47,7 +47,7 @@ impl AsRef for LoggerBackend { } impl fmt::Display for Rfc3339Time { - fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { let t = self.inner; write!( f, @@ -100,7 +100,7 @@ impl fmt::Display for LogDuration { } impl fmt::Display for LogContext<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( f, "[{} {} {}]", @@ -160,12 +160,8 @@ impl<'a> fmt::Display for FullTags<'a> { } fn prepare_user_agent(user_agent: &str) -> String { - let mut user_agent = user_agent.replace(' ', "_"); - let mut ua_bytes = std::mem::take(&mut user_agent).into_bytes(); - if let Some(last) = ua_bytes.last_mut() { - if *last == b',' { - *last = b'!' - } - } - unsafe { String::from_utf8_unchecked(ua_bytes) } + user_agent + .replace(' ', "_") + .replace('[', "{") + .replace(']', "}") } diff --git a/command/src/logging/logs.rs b/command/src/logging/logs.rs index 80567e846..2545f0523 100644 --- a/command/src/logging/logs.rs +++ b/command/src/logging/logs.rs @@ -269,11 +269,11 @@ impl InnerLogger { log.tag, ], standard: { - formats: ["{} {}->{} {}/{}/{}/{} {}->{} [{}] {} {}{}\n"], + formats: ["{} {} {} {}/{}/{}/{} {} {} [{}] {} {}{}\n"], args: [ log.context, - log.session_address.as_string_or("X"), - log.backend_address.as_string_or("X"), + log.session_address.as_string_or("-"), + log.backend_address.as_string_or("-"), LogDuration(Some(log.response_time)), LogDuration(Some(log.service_time)), LogDuration(log.client_rtt), @@ -287,7 +287,7 @@ impl InnerLogger { ] }, colored: { - formats: ["\x1b[;1m{}\x1b[m {}->{} {}/{}/{}/{} {}->{} \x1b[2m[{}] \x1b[;1m{} {:#}\x1b[m{}\n"], + formats: ["\x1b[;1m{}\x1b[m {} {} {}/{}/{}/{} {} {} \x1b[2m[{}] \x1b[;1m{} {:#}\x1b[m{}\n"], args: @, } }, @@ -794,16 +794,38 @@ impl LogLineCachedState { } } +#[macro_export] +macro_rules! log_enabled { + ($logger:expr, $lvl:expr) => {{ + let logger = $logger.borrow_mut(); + let enable = if cfg!(feature = "logs-cache") { + static mut LOG_LINE_CACHED_STATE: $crate::logging::LogLineCachedState = + $crate::logging::LogLineCachedState::new(); + logger.cached_enabled( + unsafe { &mut LOG_LINE_CACHED_STATE }, + $crate::logging::Metadata { + level: $lvl, + target: module_path!(), + }, + ) + } else { + logger.enabled($crate::logging::Metadata { + level: $lvl, + target: module_path!(), + }) + }; + if !enable { + return; + } + logger + }}; +} + #[macro_export] macro_rules! log { ($lvl:expr, $format:expr $(, $args:expr)*) => {{ - static mut LOG_LINE_CACHED_STATE: $crate::logging::LogLineCachedState = $crate::logging::LogLineCachedState::new(); $crate::logging::LOGGER.with(|logger| { - let mut logger = logger.borrow_mut(); - if !logger.cached_enabled( - unsafe { &mut LOG_LINE_CACHED_STATE }, - $crate::logging::Metadata { level: $lvl, target: module_path!() } - ) { return; } + let mut logger = $crate::log_enabled!(logger, $lvl); let (pid, tag, inner) = logger.split(); let (now, precise_time) = $crate::logging::now(); @@ -824,13 +846,8 @@ macro_rules! log { #[macro_export] macro_rules! log_access { ($lvl:expr, $($request_record_fields:tt)*) => {{ - static mut LOG_LINE_CACHED_STATE: $crate::logging::LogLineCachedState = $crate::logging::LogLineCachedState::new(); $crate::logging::LOGGER.with(|logger| { - let mut logger = logger.borrow_mut(); - if !logger.cached_enabled( - unsafe { &mut LOG_LINE_CACHED_STATE }, - $crate::logging::Metadata { level: $lvl, target: module_path!() } - ) { return; } + let mut logger = $crate::log_enabled!(logger, $lvl); let (pid, tag, inner) = logger.split(); let (now, precise_time) = $crate::logging::now(); diff --git a/command/src/response.rs b/command/src/response.rs index 04677817c..7efd3eb27 100644 --- a/command/src/response.rs +++ b/command/src/response.rs @@ -130,7 +130,7 @@ pub fn is_default_path_rule(p: &PathRule) -> bool { PathRuleKind::try_from(p.kind) == Ok(PathRuleKind::Prefix) && p.value.is_empty() } -impl std::fmt::Display for PathRule { +impl fmt::Display for PathRule { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match PathRuleKind::try_from(self.kind) { Ok(PathRuleKind::Prefix) => write!(f, "prefix '{}'", self.value), diff --git a/lib/src/tls.rs b/lib/src/tls.rs index e061c3d05..15a91a825 100644 --- a/lib/src/tls.rs +++ b/lib/src/tls.rs @@ -7,7 +7,7 @@ use std::{ borrow::ToOwned, collections::{HashMap, HashSet}, convert::From, - fmt::Debug, + fmt, io::BufReader, str::FromStr, sync::{Arc, Mutex}, @@ -468,8 +468,8 @@ impl ResolvesServerCert for MutexWrappedCertificateResolver { } } -impl Debug for MutexWrappedCertificateResolver { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl fmt::Debug for MutexWrappedCertificateResolver { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("MutexWrappedCertificateResolver") } }