-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move all logging primitives in their own module
Signed-off-by: Eloi DEMOLIS <[email protected]>
- Loading branch information
Showing
11 changed files
with
259 additions
and
253 deletions.
There are no files selected for viewing
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
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,160 @@ | ||
use std::fmt; | ||
|
||
use crate::{ | ||
logging::{ | ||
EndpointRecord, FullTags, LogContext, LogDuration, LogLevel, LogMessage, LoggerBackend, | ||
Rfc3339Time, | ||
}, | ||
AsStr, AsString, | ||
}; | ||
|
||
impl LogLevel { | ||
pub const fn as_str(&self, access: bool, colored: bool) -> &'static str { | ||
match (self, access, colored) { | ||
(LogLevel::Error, false, false) => "ERROR", | ||
(LogLevel::Warn, false, false) => "WARN ", | ||
(LogLevel::Info, false, false) => "INFO ", | ||
(LogLevel::Debug, false, false) => "DEBUG", | ||
(LogLevel::Trace, false, false) => "TRACE", | ||
|
||
(LogLevel::Error, false, true) => "\x1b[;31;1mERROR", | ||
(LogLevel::Warn, false, true) => "\x1b[;33;1mWARN ", | ||
(LogLevel::Info, false, true) => "\x1b[;32;1mINFO ", | ||
(LogLevel::Debug, false, true) => "\x1b[;34mDEBUG", | ||
(LogLevel::Trace, false, true) => "\x1b[;90mTRACE", | ||
|
||
(LogLevel::Error, true, false) => "ERROR-ACCESS", | ||
(LogLevel::Info, true, false) => "INFO-ACCESS ", | ||
(_, true, false) => "???", | ||
|
||
(LogLevel::Error, true, true) => "\x1b[;35;1mERROR-ACCESS", | ||
(LogLevel::Info, true, true) => "\x1b[;35;1mINFO-ACCESS ", | ||
(_, true, true) => "\x1b[;35;1m???", | ||
} | ||
} | ||
} | ||
|
||
impl AsRef<str> for LoggerBackend { | ||
fn as_ref(&self) -> &str { | ||
match self { | ||
LoggerBackend::Stdout(_) => "stdout", | ||
LoggerBackend::Unix(_) => "UNIX socket", | ||
LoggerBackend::Udp(_, _) => "UDP socket", | ||
LoggerBackend::Tcp(_) => "TCP socket", | ||
LoggerBackend::File(_) => "file", | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for Rfc3339Time { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { | ||
let t = self.inner; | ||
write!( | ||
f, | ||
"{}-{:02}-{:02}T{:02}:{:02}:{:02}.{:06}Z", | ||
t.year(), | ||
t.month() as u8, | ||
t.day(), | ||
t.hour(), | ||
t.minute(), | ||
t.second(), | ||
t.microsecond() | ||
) | ||
} | ||
} | ||
|
||
impl fmt::Display for LogMessage<'_> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self.0 { | ||
Some(message) => write!(f, " | {message}"), | ||
None => Ok(()), | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for LogDuration { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self.0 { | ||
None => write!(f, "-"), | ||
Some(duration) => { | ||
let secs = duration.whole_seconds(); | ||
if secs >= 10 { | ||
return write!(f, "{secs}s"); | ||
} | ||
|
||
let ms = duration.whole_milliseconds(); | ||
if ms < 10 { | ||
let us = duration.whole_microseconds(); | ||
if us >= 10 { | ||
return write!(f, "{us}μs"); | ||
} | ||
|
||
let ns = duration.whole_nanoseconds(); | ||
return write!(f, "{ns}ns"); | ||
} | ||
|
||
write!(f, "{ms}ms") | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for LogContext<'_> { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!( | ||
f, | ||
"[{} {} {}]", | ||
self.request_id, | ||
self.cluster_id.unwrap_or("-"), | ||
self.backend_id.unwrap_or("-") | ||
) | ||
} | ||
} | ||
|
||
impl fmt::Display for EndpointRecord<'_> { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
Self::Http { | ||
method, | ||
authority, | ||
path, | ||
status, | ||
.. | ||
} => write!( | ||
f, | ||
"{} {} {} -> {}", | ||
authority.as_str_or("-"), | ||
method.as_str_or("-"), | ||
path.as_str_or("-"), | ||
status.as_string_or("-"), | ||
), | ||
Self::Tcp { context } => { | ||
write!(f, "{}", context.as_str_or("-")) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<'a> fmt::Display for FullTags<'a> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match (self.concatenated, self.user_agent) { | ||
(None, None) => write!(f, "-"), | ||
(Some(tags), None) => write!(f, "{tags}"), | ||
(Some(tags), Some(ua)) if !tags.is_empty() => { | ||
write!(f, "{tags}, user-agent={}", prepare_user_agent(ua)) | ||
} | ||
(_, Some(ua)) => write!(f, "user-agent={}", prepare_user_agent(ua)), | ||
} | ||
} | ||
} | ||
|
||
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) } | ||
} |
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,7 @@ | ||
pub mod access_logs; | ||
pub mod display; | ||
#[macro_use] | ||
pub mod logs; | ||
|
||
pub use crate::logging::access_logs::*; | ||
pub use crate::logging::logs::*; |
Oops, something went wrong.