Skip to content

Commit

Permalink
impl: logger added to playground (#1064)
Browse files Browse the repository at this point in the history
* impl: logger added to playground

* fix: logger init returns result

---------

Co-authored-by: Elad Kaplan <[email protected]>
  • Loading branch information
jondot and kaplanelad authored Dec 13, 2024
1 parent 7c4702e commit 56ada7b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,12 @@ pub async fn playground<H: Hooks>() -> crate::Result<AppContext> {
let cli = Playground::parse();
let environment: Environment = cli.environment.unwrap_or_else(resolve_from_env).into();

let config = environment.load()?;

if !H::init_logger(&config, &environment)? {
logger::init::<H>(&config.logger)?;
}

let app_context = create_context::<H>(&environment).await?;
Ok(app_context)
}
Expand Down Expand Up @@ -536,7 +542,7 @@ pub async fn main<H: Hooks, M: MigratorTrait>() -> crate::Result<()> {
let config = environment.load()?;

if !H::init_logger(&config, &environment)? {
logger::init::<H>(&config.logger);
logger::init::<H>(&config.logger)?;
}

let task_span = create_root_span(&environment);
Expand Down Expand Up @@ -682,7 +688,7 @@ pub async fn main<H: Hooks>() -> crate::Result<()> {
let config = environment.load()?;

if !H::init_logger(&config, &environment)? {
logger::init::<H>(&config.logger);
logger::init::<H>(&config.logger)?;
}

let task_span = create_root_span(&environment);
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mod env_vars;
pub mod environment;
pub mod errors;
pub mod hash;
mod logger;
pub mod logger;
pub mod mailer;
pub mod scheduler;
pub mod task;
Expand Down
16 changes: 12 additions & 4 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tracing_subscriber::{
fmt, fmt::MakeWriter, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer, Registry,
};

use crate::{app::Hooks, config};
use crate::{app::Hooks, config, Error, Result};

// Define an enumeration for log levels
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
Expand Down Expand Up @@ -75,6 +75,7 @@ const MODULE_WHITELIST: &[&str] = &[
"tower_http",
"sqlx::query",
"sidekiq",
"playground",
];

// Keep nonblocking file appender work guard
Expand All @@ -96,7 +97,11 @@ static NONBLOCKING_WORK_GUARD_KEEP: OnceLock<WorkerGuard> = OnceLock::new();
/// use via PR)
/// 3. regardless of (1) and (2) operators in production, or elsewhere can
/// always use `RUST_LOG` to quickly diagnose a service
pub fn init<H: Hooks>(config: &config::Logger) {
///
/// # Errors
/// Fails if cannot initialize logger or set up an appender (in case the option
/// is enabled)
pub fn init<H: Hooks>(config: &config::Logger) -> Result<()> {
let mut layers: Vec<Box<dyn Layer<Registry> + Sync + Send>> = Vec::new();

if let Some(file_appender_config) = config.file_appender.as_ref() {
Expand Down Expand Up @@ -138,12 +143,14 @@ pub fn init<H: Hooks>(config: &config::Logger) {
.map_or_else(String::new, ToString::to_string),
)
.build(dir)
.expect("logger file appender initialization failed");
.map_err(Error::msg)?;

let file_appender_layer = if file_appender_config.non_blocking {
let (non_blocking_file_appender, work_guard) =
tracing_appender::non_blocking(file_appender);
NONBLOCKING_WORK_GUARD_KEEP.set(work_guard).unwrap();
NONBLOCKING_WORK_GUARD_KEEP
.set(work_guard)
.map_err(|_| Error::string("cannot lock for appender"))?;
init_layer(
non_blocking_file_appender,
&file_appender_config.format,
Expand All @@ -168,6 +175,7 @@ pub fn init<H: Hooks>(config: &config::Logger) {
.with(env_filter)
.init();
}
Ok(())
}

fn init_env_filter<H: Hooks>(override_filter: Option<&String>, level: &LogLevel) -> EnvFilter {
Expand Down

0 comments on commit 56ada7b

Please sign in to comment.