Skip to content
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
4 changes: 1 addition & 3 deletions crates/cli/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,11 @@ async fn log_response_middleware(

let response = next.run(request).await;

let Some(log_context) = LogContext::current() else {
let Some(stats) = LogContext::maybe_with(LogContext::stats) else {
tracing::error!("Missing log context for request, this is a bug!");
return response;
};

let stats = log_context.stats();

let status_code = response.status();
match status_code.as_u16() {
100..=399 => tracing::info!(
Expand Down
7 changes: 4 additions & 3 deletions crates/context/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,14 @@ where
write!(&mut writer, "{} ", style.apply_to(metadata.name()))?;
}

if let Some(log_context) = LogContext::current() {
LogContext::maybe_with(|log_context| {
let log_context = Style::new()
.bold()
.force_styling(ansi)
.apply_to(log_context);
write!(&mut writer, "{log_context} - ")?;
}
write!(&mut writer, "{log_context} - ")
})
.transpose()?;

let field_fromatter = DefaultFields::new();
field_fromatter.format_fields(writer.by_ref(), event)?;
Expand Down
9 changes: 6 additions & 3 deletions crates/context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@ impl LogContext {
}
}

/// Get a copy of the current log context, if any
pub fn current() -> Option<Self> {
CURRENT_LOG_CONTEXT.try_with(Self::clone).ok()
/// Run a closure with the current log context, if any
pub fn maybe_with<F, R>(f: F) -> Option<R>
where
F: FnOnce(&Self) -> R,
{
CURRENT_LOG_CONTEXT.try_with(f).ok()
}

/// Run the async function `f` with the given log context. It will wrap the
Expand Down
6 changes: 3 additions & 3 deletions crates/tasks/src/new_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,14 +789,14 @@ impl JobTracker {
);
let result = job.run(&state, context.clone()).await;

let Some(log_context) = LogContext::current() else {
let Some(context_stats) =
LogContext::maybe_with(mas_context::LogContext::stats)
else {
// This should never happen, but if it does it's fine: we're recovering fine
// from panics in those tasks
panic!("Missing log context, this should never happen");
};

let context_stats = log_context.stats();

// We log the result here so that it's attached to the right span & log context
match &result {
Ok(()) => {
Expand Down
Loading