#[macro_export]
macro_rules! log {
(target: $target:expr, $lvl:expr, $($arg:tt)+) => {{
$crate::tracing::event!(target: $target, $lvl, $($arg)+)
}};
($lvl:expr, $($arg:tt)+) => {{
$crate::tracing::event!($lvl, $($arg)+)
}};
}
#[macro_export]
macro_rules! error {
(target: $target:expr, $($arg:tt)+) => ({
$crate::log!(target: $target, $crate::tracing::Level::ERROR, $($arg)+)
});
($e:expr; target: $target:expr, $($arg:tt)+) => ({
$crate::log!(
target: $target,
$crate::tracing::Level::ERROR,
err = ?$e,
$($arg)+
)
});
(%$e:expr; target: $target:expr, $($arg:tt)+) => ({
$crate::log!(
target: $target,
$crate::tracing::Level::ERROR,
err = %$e,
$($arg)+
)
});
($e:expr; $($arg:tt)+) => ({
$crate::log!(
$crate::tracing::Level::ERROR,
err = ?$e,
$($arg)+
)
});
(%$e:expr; $($arg:tt)+) => ({
$crate::log!(
$crate::tracing::Level::ERROR,
err = %$e,
$($arg)+
)
});
($($arg:tt)+) => ({
$crate::log!($crate::tracing::Level::ERROR, $($arg)+)
});
}
#[macro_export]
macro_rules! warn {
(target: $target:expr, $($arg:tt)+) => {
$crate::log!(target: $target, $crate::tracing::Level::WARN, $($arg)+)
};
($e:expr; $($arg:tt)+) => ({
$crate::log!(
$crate::tracing::Level::WARN,
err = ?$e,
$($arg)+
)
});
(%$e:expr; $($arg:tt)+) => ({
$crate::log!(
$crate::tracing::Level::WARN,
err = %$e,
$($arg)+
)
});
($($arg:tt)+) => {
$crate::log!($crate::tracing::Level::WARN, $($arg)+)
};
}
#[macro_export]
macro_rules! info {
(target: $target:expr, $($arg:tt)+) => {
$crate::log!(target: $target, $crate::tracing::Level::INFO, $($arg)+)
};
($($arg:tt)+) => {
$crate::log!($crate::tracing::Level::INFO, $($arg)+)
};
}
#[macro_export]
macro_rules! debug {
(target: $target:expr, $($arg:tt)+) => {
$crate::log!(target: $target, $crate::tracing::Level::DEBUG, $($arg)+)
};
($($arg:tt)+) => {
$crate::log!($crate::tracing::Level::DEBUG, $($arg)+)
};
}
#[macro_export]
macro_rules! trace {
(target: $target:expr, $($arg:tt)+) => {
$crate::log!(target: $target, $crate::tracing::Level::TRACE, $($arg)+)
};
($($arg:tt)+) => {
$crate::log!($crate::tracing::Level::TRACE, $($arg)+)
};
}
#[macro_export]
macro_rules! slow {
(target: $target:expr, $($arg:tt)+) => {
$crate::log!(target: $target, slow = true, $crate::tracing::Level::INFO, $($arg)+)
};
($($arg:tt)+) => {
$crate::log!($crate::tracing::Level::INFO, slow = true, $($arg)+)
};
}
#[cfg(test)]
mod tests {
use common_error::mock::MockError;
use common_error::status_code::StatusCode;
use tracing::Level;
macro_rules! all_log_macros {
($($arg:tt)*) => {
trace!($($arg)*);
debug!($($arg)*);
info!($($arg)*);
warn!($($arg)*);
error!($($arg)*);
};
}
#[test]
fn test_log_args() {
log!(target: "my_target", Level::TRACE, "foo");
log!(target: "my_target", Level::DEBUG, "foo",);
log!(target: "my_target", Level::INFO, "foo: {}", 3);
log!(target: "my_target", Level::WARN, "foo: {}", 3,);
log!(target: "my_target", Level::ERROR, "hello {world}", world = "world");
log!(target: "my_target", Level::DEBUG, "hello {world}", world = "world",);
all_log_macros!(target: "my_target", "foo");
all_log_macros!(target: "my_target", "foo",);
all_log_macros!(target: "my_target", "foo: {}", 3);
all_log_macros!(target: "my_target", "foo: {}", 3,);
all_log_macros!(target: "my_target", "hello {world}", world = "world");
all_log_macros!(target: "my_target", "hello {world}", world = "world",);
}
#[test]
fn test_log_no_target() {
log!(Level::DEBUG, "foo");
log!(Level::DEBUG, "foo: {}", 3);
all_log_macros!("foo");
all_log_macros!("foo: {}", 3);
}
#[test]
fn test_log_ref_scope_args() {
let bar = 35;
let world = "world";
log!(target: "my_target", Level::DEBUG, "bar: {bar}");
log!(target: "my_target", Level::DEBUG, "bar: {bar}, hello {}", world);
log!(target: "my_target", Level::DEBUG, "bar: {bar}, hello {world}",);
all_log_macros!(target: "my_target", "bar: {bar}");
all_log_macros!(target: "my_target", "bar: {bar}, hello {}", world);
all_log_macros!(target: "my_target", "bar: {bar}, hello {world}",);
}
#[test]
fn test_log_error() {
crate::init_default_ut_logging();
let err = MockError::new(StatusCode::Unknown);
let err_ref = &err;
let err_ref2 = &err_ref;
error!(target: "my_target", "hello {}", "world");
error!(err; target: "my_target", "hello {}", "world");
error!(%err; target: "my_target", "hello {}", "world");
error!(err_ref; target: "my_target", "hello {}", "world");
error!(err_ref2; "hello {}", "world");
error!(%err_ref2; "hello {}", "world");
error!("hello {}", "world");
let root_err = MockError::with_source(err);
error!(root_err; "Error with source hello {}", "world");
}
}