Skip to content

Commit

Permalink
justknobs: migrate to tracing_slog_compat
Browse files Browse the repository at this point in the history
Summary:
We are migrating from `slog` to `tracing`.
Modify the `rust/shed/secure_utils` crate to accept `tracing_slog_compat` instances of `Logger`, as well as regular `slog::Logger` instances.

Differential Revision: D68857207

fbshipit-source-id: 58ce80f0855f7f54cc2bcaa6f9a54d92c2a38781
  • Loading branch information
lmvasquezg authored and facebook-github-bot committed Jan 31, 2025
1 parent 15d4715 commit 8305040
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions shed/justknobs_stub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ serde = { version = "1.0.185", features = ["derive", "rc"] }
serde_json = { version = "1.0.132", features = ["float_roundtrip", "unbounded_depth"] }
slog = { version = "2.7", features = ["max_level_trace", "nested-values"] }
tokio = { version = "1.41.0", features = ["full", "test-util", "tracing"] }
tracing_slog_compat = { version = "0.1.0", path = "../tracing_slog_compat" }

[dev-dependencies]
fbinit = { version = "0.2.0", path = "../fbinit" }
Expand Down
33 changes: 26 additions & 7 deletions shed/justknobs_stub/src/cached_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ use cached_config::ConfigHandle;
use just_knobs_struct::JustKnobs as JustKnobsStruct;
use serde::Deserialize;
use serde::Serialize;
use slog::debug;
use slog::error;
use slog::warn;
use slog::Logger;
use tokio::runtime::Handle;
use tracing_slog_compat::debug;
use tracing_slog_compat::error;
use tracing_slog_compat::warn;
use tracing_slog_compat::Logger;

use crate::JustKnobs;

Expand Down Expand Up @@ -102,10 +102,11 @@ fn log_just_knobs(just_knobs: &JustKnobsStruct) -> String {
}

pub fn init_just_knobs_worker(
logger: Logger,
logger: impl IntoLogger,
config_handle: ConfigHandle<JustKnobsStruct>,
runtime_handle: Handle,
) -> Result<()> {
let logger = logger.into_logger();
init_just_knobs(&logger, &config_handle)?;
if JUST_KNOBS_WORKER_STATE
.set(JustKnobsWorkerState {
Expand All @@ -122,12 +123,13 @@ pub fn init_just_knobs_worker(
}

pub fn init_just_knobs(
logger: &Logger,
logger: &(impl IntoLogger + Clone),
config_handle: &ConfigHandle<JustKnobsStruct>,
) -> Result<()> {
let just_knobs = config_handle.get();
let logger = logger.clone();
debug!(
logger,
logger.into_logger(),
"Initializing JustKnobs: {}",
log_just_knobs(&just_knobs)
);
Expand Down Expand Up @@ -181,6 +183,23 @@ fn update_just_knobs(new_just_knobs: Arc<JustKnobsStruct>) -> Result<()> {
Ok(())
}

#[doc(hidden)]
pub trait IntoLogger {
fn into_logger(self) -> tracing_slog_compat::Logger;
}

impl IntoLogger for slog::Logger {
fn into_logger(self) -> tracing_slog_compat::Logger {
tracing_slog_compat::Logger::Slog(self)
}
}

impl IntoLogger for tracing_slog_compat::Logger {
fn into_logger(self) -> tracing_slog_compat::Logger {
self
}
}

#[cfg(test)]
mod test {
use std::thread;
Expand Down

0 comments on commit 8305040

Please sign in to comment.