diff options
| author | tv <tv@krebsco.de> | 2026-10-23 11:26:01 +0200 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-10-23 11:26:01 +0200 |
| commit | a3d322a65c3bae76740be78dac167b1ce1fc0716 (patch) | |
| tree | 4c87588f4ef9899cc05ebde3347034efb330f834 /src/api | |
| parent | 942eb994e2e3d34deb8fb811d23571c40bf60818 (diff) | |
allow changing log filtering while node runs
This also changes the default to tracing-subscriber.
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/log.rs | 27 | ||||
| -rw-r--r-- | src/api/mod.rs | 2 |
2 files changed, 29 insertions, 0 deletions
diff --git a/src/api/log.rs b/src/api/log.rs new file mode 100644 index 0000000..4881b38 --- /dev/null +++ b/src/api/log.rs @@ -0,0 +1,27 @@ +//! /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<String, StatusCode> { + 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<String, (StatusCode, String)> { + 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") +} diff --git a/src/api/mod.rs b/src/api/mod.rs index ea3ad41..7328059 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -2,6 +2,7 @@ pub(crate) mod auth; mod debug; +mod log; mod peers; mod store; @@ -37,6 +38,7 @@ pub(crate) fn router(state: AppState, auth_state: AuthState, debug_api: bool) -> .route("/stream/{*key}", get(store::stream_store_entry)) .route("/peers", get(peers::list_peers)) .route("/peers/{peer}", delete(peers::delete_peer)) + .route("/admin/log", get(log::get_log_filter).put(log::put_log_filter)) .merge(crate::remake::router::<AppState>()); if debug_api { |
