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

impl: logger added to playground #1064

Merged
merged 6 commits into from
Dec 13, 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
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
Loading