Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fix #13

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
A library that supports five logging levels: ERROR, WARNING, INFO, TRACING,
and DEBUG. The log broker forwards these logs to either a Redis server,
to a local log file, or both. When forwarded to a a Redis server, it will
be formatted to a JSON whilst when forwarded to a log file it will be formated
be formatted to a JSON whilst when forwarded to a log file it will be formatted
as a log from the tracing library.

## Usage

```rs
use log_broker::LogLocation;

// To initialize tracing to a local file when given a valid path
// and package name
// Note: cannot be `_` otherwise the WokerGuard will immediately
// Note: cannot be `_` otherwise the WorkerGuard will immediately
// be consumed
let _guard = init_tracing(path, pkg_name)?;

Expand All @@ -30,7 +32,7 @@ as a log from the tracing library.
trace!(LogLocation::Local, "{}", trace_log);

// Debug log logs to Redis server initialized above
debug!(LogLocation::Local, "{}", debug_log);
debug!(LogLocation::Redis, "{}", debug_log);

// Info log logs to both local and Redis server
info!(LogLocation::Both, "{}", info_log);
Expand Down
90 changes: 56 additions & 34 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(test)]
mod test;

use anyhow::{anyhow, bail, Ok, Result};
use bb8_redis::{
bb8::{CustomizeConnection, ErrorSink, ManageConnection, NopErrorSink, Pool, QueueStrategy},
Expand All @@ -7,20 +10,18 @@ use bb8_redis::{
use lazy_static::lazy_static;
use std::{fs::File, net::IpAddr, path::Path, sync::Arc, time::Duration};
use tokio::sync::RwLock;
pub use tracing;
use tracing::metadata::LevelFilter;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::{
fmt, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer,
};

#[cfg(test)]
mod test;

/// trace! macro
///
/// Constructs a new message at the trace level with the given format string and
/// a desired log location of either to a Redis connection, log file or both.
/// This functions similary to the trace! macro.
/// This functions similarly to the trace! macro.
///
/// # Examples
///
Expand All @@ -34,16 +35,20 @@ macro_rules! trace {
($location:expr,$($arg:tt)*) => {
match $location {
LogLocation::Local => {
tracing::trace!($($arg)*);
$crate::tracing::trace!($($arg)*);
}
LogLocation::Redis => {
let log = format!($($arg)*);
let _ = log_to_redis(&log).await;
tokio::spawn(async move {
BLYKIM marked this conversation as resolved.
Show resolved Hide resolved
let _ = $crate::log_to_redis(&log).await;
});
}
LogLocation::Both => {
tracing::trace!($($arg)*);
$crate::tracing::trace!($($arg)*);
let log = format!($($arg)*);
let _ = log_to_redis(&log).await;
tokio::spawn(async move {
let _ = $crate::log_to_redis(&log).await;
});
}
}
};
Expand All @@ -53,7 +58,7 @@ macro_rules! trace {
///
/// Constructs a new message at the debug level with the given format string and
/// a desired log location of either to a Redis connection, log file or both.
/// This functions similary to the debug! macro.
/// This functions similarly to the debug! macro.
///
/// # Examples
///
Expand All @@ -67,16 +72,20 @@ macro_rules! debug {
($location:expr,$($arg:tt)*) => {
match $location {
LogLocation::Local => {
tracing::debug!($($arg)*);
$crate::tracing::debug!($($arg)*);
}
LogLocation::Redis => {
let log = format!($($arg)*);
let _ = log_to_redis(&log).await;
tokio::spawn(async move {
let _ = $crate::log_to_redis(&log).await;
});
}
LogLocation::Both => {
tracing::debug!($($arg)*);
$crate::tracing::debug!($($arg)*);
let log = format!($($arg)*);
let _ = log_to_redis(&log).await;
tokio::spawn(async move {
let _ = $crate::log_to_redis(&log).await;
});
}
}
};
Expand All @@ -86,7 +95,7 @@ macro_rules! debug {
///
/// Constructs a new message at the info level with the given format string and
/// a desired log location of either to a Redis connection, log file or both.
/// This functions similary to the info! macro.
/// This functions similarly to the info! macro.
///
/// # Examples
///
Expand All @@ -100,27 +109,30 @@ macro_rules! info {
($location:expr,$($arg:tt)*) => {
match $location {
LogLocation::Local => {
tracing::info!($($arg)*);
$crate::tracing::info!($($arg)*);
}
LogLocation::Redis => {
let log = format!($($arg)*);
let _ = log_to_redis(&log).await;
tokio::spawn(async move {
let _ = $crate::log_to_redis(&log).await;
});
}
LogLocation::Both => {
tracing::info!($($arg)*);
$crate::tracing::info!($($arg)*);
let log = format!($($arg)*);
let _ = log_to_redis(&log).await;
tokio::spawn(async move {
let _ = $crate::log_to_redis(&log).await;
});
}
}
};

}

/// warn! macro
///
/// Constructs a new message at the warn level with the given format string and
/// a desired log location of either to a Redis connection, log file or both.
/// This functions similary to the warn! macro.
/// This functions similarly to the warn! macro.
///
/// # Examples
///
Expand All @@ -134,27 +146,30 @@ macro_rules! warn {
($location:expr,$($arg:tt)*) => {
match $location {
LogLocation::Local => {
tracing::warn!($($arg)*);
$crate::tracing::warn!($($arg)*);
}
LogLocation::Redis => {
let log = format!($($arg)*);
let _ = log_to_redis(&log).await;
tokio::spawn(async move {
let _ = $crate::log_to_redis(&log).await;
});
}
LogLocation::Both => {
tracing::warn!($($arg)*);
$crate::tracing::warn!($($arg)*);
let log = format!($($arg)*);
let _ = log_to_redis(&log).await;
tokio::spawn(async move {
let _ = $crate::log_to_redis(&log).await;
});
}
}
};

}

/// error! macro
///
/// Constructs a new message at the error level with the given format string and
/// a desired log location of either to a Redis connection, log file or both.
/// This functions similary to the error! macro.
/// This functions similarly to the error! macro.
///
/// # Examples
///
Expand All @@ -168,16 +183,20 @@ macro_rules! error {
($location:expr,$($arg:tt)*) => {
match $location {
LogLocation::Local => {
tracing::error!($($arg)*);
$crate::tracing::error!($($arg)*);
}
LogLocation::Redis => {
let log = format!($($arg)*);
let _ = log_to_redis(&log).await;
tokio::spawn(async move {
let _ = $crate::log_to_redis(&log).await;
});
}
LogLocation::Both => {
tracing::error!($($arg)*);
$crate::tracing::error!($($arg)*);
let log = format!($($arg)*);
let _ = log_to_redis(&log).await;
tokio::spawn(async move {
let _ = $crate::log_to_redis(&log).await;
});
}
}
};
Expand Down Expand Up @@ -252,7 +271,7 @@ impl<M: ManageConnection> RedisConnPoolOptions<M> {
}

#[must_use]
pub fn max_lifetimeidle_timeout(mut self, idle_timeout: Option<Duration>) -> Self {
pub fn idle_timeout(mut self, idle_timeout: Option<Duration>) -> Self {
self.idle_timeout = idle_timeout;
self
}
Expand Down Expand Up @@ -382,7 +401,6 @@ pub async fn init_redis_connection_with_options(
}
}

#[allow(dead_code)]
async fn build_pool_with_options<M: ManageConnection>(
manager: M,
pool_options: Option<RedisConnPoolOptions<M>>,
Expand All @@ -407,8 +425,12 @@ async fn build_pool_with_options<M: ManageConnection>(
builder.build(manager).await
}

#[allow(dead_code)]
async fn log_to_redis(log: &str) -> anyhow::Result<()> {
/// Expanded macro calls this function.
///
/// # Errors
///
/// * Redis error
pub async fn log_to_redis(log: &str) -> anyhow::Result<()> {
let pool = REDIS_POOL.read().await;

match &*pool {
Expand Down