Skip to content

Commit

Permalink
Final access logs ASCII format, put logs-cache under feature
Browse files Browse the repository at this point in the history
Signed-off-by: Eloi DEMOLIS <[email protected]>
  • Loading branch information
Wonshtrum committed Feb 21, 2024
1 parent be0d06b commit 6296014
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 34 deletions.
6 changes: 3 additions & 3 deletions bin/src/command/server.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions command/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ x509-parser = "^0.15.1"
unstable = []
logs-debug = []
logs-trace = []
logs-cache = []

[badges]
travis-ci = { repository = "sozu-proxy/sozu" }
Expand Down
2 changes: 1 addition & 1 deletion command/src/logging/access_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<T> 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 {
Expand Down
16 changes: 6 additions & 10 deletions command/src/logging/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl AsRef<str> 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,
Expand Down Expand Up @@ -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,
"[{} {} {}]",
Expand Down Expand Up @@ -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(']', "}")
}
49 changes: 33 additions & 16 deletions command/src/logging/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,11 @@ impl InnerLogger {
log.tag,
],
standard: {
formats: ["{} {}->{} {}/{}/{}/{} {}->{} [{}] {} {}{}\n"],
formats: ["{} {} {} {}/{}/{}/{} {} {}\t[{}] {} {}{}\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),
Expand All @@ -287,7 +287,7 @@ impl InnerLogger {
]
},
colored: {
formats: ["\x1b[;1m{}\x1b[m {}->{} {}/{}/{}/{} {}->{} \x1b[2m[{}] \x1b[;1m{} {:#}\x1b[m{}\n"],
formats: ["\x1b[;1m{}\x1b[m {} {} {}/{}/{}/{} {} {}\t\x1b[2m[{}] \x1b[;1m{} {:#}\x1b[m{}\n"],
args: @,
}
},
Expand Down Expand Up @@ -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 },

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (false, beta)

mutable reference of mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (false, beta)

mutable reference of mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (false, beta)

mutable reference of mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (false, beta)

mutable reference of mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (false, beta)

mutable reference of mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (false, beta)

mutable reference of mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (false, beta)

mutable reference of mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (false, beta)

mutable reference of mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (false, beta)

mutable reference of mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (false, beta)

mutable reference of mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (false, beta)

mutable reference of mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (false, beta)

mutable reference of mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (false, beta)

mutable reference of mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (false, beta)

mutable reference of mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (false, beta)

mutable reference of mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (false, beta)

mutable reference of mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (false, beta)

mutable reference of mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (false, beta)

mutable reference of mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, true)

creating a mutable reference to mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, true)

creating a mutable reference to mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, true)

creating a mutable reference to mutable static is discouraged

Check warning on line 805 in command/src/logging/logs.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, true)

creating a mutable reference to mutable static is discouraged
$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();

Expand All @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion command/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions lib/src/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
borrow::ToOwned,
collections::{HashMap, HashSet},
convert::From,
fmt::Debug,
fmt,
io::BufReader,
str::FromStr,
sync::{Arc, Mutex},
Expand Down Expand Up @@ -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")
}
}
Expand Down

0 comments on commit 6296014

Please sign in to comment.