Skip to content

Commit

Permalink
feat(tracing): get user to provide pkg name
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverNChalk committed Sep 24, 2024
1 parent ac7f6cd commit 9cf9698
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/tracing.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
use std::path::Path;

use const_format::formatcp;
use tracing::level_filters::LevelFilter;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{EnvFilter, Layer};

const DEFAULT_FILE_DIRECTIVE: &str = formatcp!("info,{}=debug", env!("CARGO_PKG_NAME"));
const LOG_FILE_NAME: &str = formatcp!("{}.log", env!("CARGO_PKG_NAME"));

#[must_use]
pub fn setup_tracing(log_directory: Option<&Path>) -> Option<WorkerGuard> {
pub fn setup_tracing(pkg_name: &str, log_directory: Option<&Path>) -> Option<WorkerGuard> {
// Setup stdout layer.
let stdout_filter = EnvFilter::builder()
.with_env_var("RUST_LOG")
Expand All @@ -25,12 +21,13 @@ pub fn setup_tracing(log_directory: Option<&Path>) -> Option<WorkerGuard> {
// Setup file layer (if requested).
let (file_layer, file_guard) = log_directory
.map(|directory| {
let file_appender = tracing_appender::rolling::hourly(directory, LOG_FILE_NAME);
let file_appender =
tracing_appender::rolling::hourly(directory, format!("{pkg_name}.log"));
let (file_writer, file_guard) = tracing_appender::non_blocking(file_appender);

// Load the user's file filter else fallback to the default.
let file_filter = std::env::var("RUST_FILE_LOG").ok();
let file_filter = file_filter.as_deref().unwrap_or(DEFAULT_FILE_DIRECTIVE);
let file_filter = file_filter.unwrap_or_else(|| format!("info,{pkg_name}=debug"));
let file_filter: EnvFilter = file_filter.parse().unwrap();

let file_layer = tracing_subscriber::fmt::layer()
Expand Down

0 comments on commit 9cf9698

Please sign in to comment.