Skip to content

Commit

Permalink
feat: make tracing subscriber reloadable
Browse files Browse the repository at this point in the history
  • Loading branch information
saibatizoku committed May 8, 2024
1 parent 8869c69 commit 9a4e98c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion catalyst-gateway/bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ tokio-postgres = { workspace = true, features = [
] }
clap = { workspace = true, features = ["derive", "env"] }
tracing = { workspace = true, features = ["log"] }
tracing-subscriber = { workspace = true, features = ["fmt", "json", "time"] }
tracing-subscriber = { workspace = true, features = ["fmt", "json", "registry", "std", "time"] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["rt", "macros", "rt-multi-thread"] }
Expand Down
2 changes: 1 addition & 1 deletion catalyst-gateway/bin/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Cli {
pub(crate) async fn exec(self) -> anyhow::Result<()> {
match self {
Self::Run(settings) => {
logger::init(settings.log_level)?;
let logger_handle = logger::init(settings.log_level);

// Unique machine id
let machine_id = settings.follower_settings.machine_uid;
Expand Down
30 changes: 20 additions & 10 deletions catalyst-gateway/bin/src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
//! Setup for logging for the service.

use clap::ValueEnum;
use serde::Deserialize;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{
fmt::{format::FmtSpan, time},
FmtSubscriber,
fmt::{self, format::FmtSpan, time},
prelude::*,
reload::{self, Handle},
Registry,
};

/// Default log level
pub(crate) const LOG_LEVEL_DEFAULT: &str = "info";

/// All valid logging levels
#[derive(ValueEnum, Clone, Copy)]
#[derive(ValueEnum, Clone, Copy, Debug, Deserialize)]
#[serde(rename(deserialize = "lowercase"))]
pub(crate) enum LogLevel {
/// Debug messages
Debug,
Expand Down Expand Up @@ -45,10 +50,10 @@ impl From<LogLevel> for tracing::log::LevelFilter {
}

/// Initialize the tracing subscriber
pub(crate) fn init(log_level: LogLevel) -> anyhow::Result<()> {
let subscriber = FmtSubscriber::builder()
pub(crate) fn init(log_level: LogLevel) -> Handle<LevelFilter, Registry> {
// Create the formatting layer
let layer = fmt::layer()
.json()
.with_max_level(LevelFilter::from_level(log_level.into()))
.with_timer(time::UtcTime::rfc_3339())
.with_span_events(FmtSpan::CLOSE)
.with_target(true)
Expand All @@ -58,12 +63,17 @@ pub(crate) fn init(log_level: LogLevel) -> anyhow::Result<()> {
.with_thread_names(true)
.with_thread_ids(true)
.with_current_span(true)
.with_span_list(true)
.finish();
.with_span_list(true);
// Create a reloadable layer with the specified log_level
let filter = LevelFilter::from_level(log_level.into());
let (filter, reload_handle) = reload::Layer::new(filter);
tracing_subscriber::registry()
.with(filter)
.with(layer)
.init();

// Logging is globally disabled by default, so globally enable it to the required level.
tracing::log::set_max_level(log_level.into());

tracing::subscriber::set_global_default(subscriber)?;
Ok(())
reload_handle
}

0 comments on commit 9a4e98c

Please sign in to comment.