From 1acd998742b9b15ce754650915ccefd7f6e64939 Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 23 Oct 2026 11:26:01 +0200 Subject: allow changing log filtering while node runs This also changes the default to tracing-subscriber. --- src/api/log.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/api/log.rs (limited to 'src/api/log.rs') diff --git a/src/api/log.rs b/src/api/log.rs new file mode 100644 index 0000000..5d44926 --- /dev/null +++ b/src/api/log.rs @@ -0,0 +1,32 @@ +//! /admin/log: read and replace the log filter of the running node. + +use axum::{body::Bytes, http::StatusCode}; +use tracing::{info, warn}; + +use crate::logging; + +/// `GET /admin/log`: the filter in effect, as plain text in `RUST_LOG` syntax. +pub(crate) async fn get_log_filter() -> Result { + logging::filter().map_err(|error| { + warn!(error = %format!("{error:#}"), "Cannot read the log filter"); + StatusCode::INTERNAL_SERVER_ERROR + }) +} + +/// `PUT /admin/log`: replace the filter; the body is plain text in `RUST_LOG` syntax, +/// e.g. `info,continuum::swarm::sync=trace`. Answers with the filter now in effect; +/// 400 (with the reason) if the body doesn't parse, and the old filter stays. +pub(crate) async fn put_log_filter(body: Bytes) -> Result { + let directives = std::str::from_utf8(&body) + .map_err(|_| { + ( + StatusCode::BAD_REQUEST, + "The filter must be UTF-8\n".to_owned(), + ) + })? + .trim(); + let applied = logging::set_filter(directives) + .map_err(|error| (StatusCode::BAD_REQUEST, format!("{error:#}\n")))?; + info!(filter = %applied, "Log filter replaced"); + Ok(applied + "\n") +} -- cgit v1.3.1