add partial envfilter support for admin log capture; conf item

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-08-17 03:40:05 +00:00
parent 4d0fc41222
commit 50e66a2976
2 changed files with 31 additions and 11 deletions

View File

@ -30,6 +30,7 @@ use service::{
Services,
};
use tracing::Level;
use tracing_subscriber::{filter::LevelFilter, EnvFilter};
use crate::{admin, admin::AdminCommand, Command};
@ -87,17 +88,7 @@ fn reply(mut content: RoomMessageEventContent, reply_id: Option<OwnedEventId>) -
// Parse and process a message from the admin room
async fn process(context: &Command<'_>, command: AdminCommand, args: &[String]) -> CommandOutput {
let filter: &capture::Filter =
&|data| data.level() <= Level::DEBUG && data.our_modules() && data.scope.contains(&"admin");
let logs = Arc::new(Mutex::new(
collect_stream(|s| markdown_table_head(s)).expect("markdown table header"),
));
let capture = Capture::new(
&context.services.server.log.capture,
Some(filter),
capture::fmt(markdown_table, logs.clone()),
);
let (capture, logs) = capture_create(context);
let capture_scope = capture.start();
let result = Box::pin(admin::process(command, context)).await;
@ -130,6 +121,30 @@ async fn process(context: &Command<'_>, command: AdminCommand, args: &[String])
Some(RoomMessageEventContent::notice_markdown(output))
}
fn capture_create(context: &Command<'_>) -> (Arc<Capture>, Arc<Mutex<String>>) {
let env_config = &context.services.server.config.admin_log_capture;
let env_filter = EnvFilter::try_new(env_config).unwrap_or_else(|_| "debug".into());
let log_level = env_filter
.max_level_hint()
.and_then(LevelFilter::into_level)
.unwrap_or(Level::DEBUG);
let filter =
move |data: capture::Data<'_>| data.level() <= log_level && data.our_modules() && data.scope.contains(&"admin");
let logs = Arc::new(Mutex::new(
collect_stream(|s| markdown_table_head(s)).expect("markdown table header"),
));
let capture = Capture::new(
&context.services.server.log.capture,
Some(filter),
capture::fmt(markdown_table, logs.clone()),
);
(capture, logs)
}
// Parse chat messages from the admin room into an AdminCommand object
fn parse<'a>(
services: &Arc<Services>, input: &'a CommandInput,

View File

@ -338,6 +338,8 @@ pub struct Config {
pub admin_console_automatic: bool,
#[serde(default)]
pub admin_execute: Vec<String>,
#[serde(default = "default_admin_log_capture")]
pub admin_log_capture: String,
#[serde(default)]
pub sentry: bool,
@ -596,6 +598,7 @@ impl fmt::Display for Config {
&self.admin_console_automatic.to_string(),
);
line("Execute admin commands after startup", &self.admin_execute.join(", "));
line("Filter for admin command log capture", &self.admin_log_capture);
line("Allow outgoing federated typing", &self.allow_outgoing_typing.to_string());
line("Allow incoming federated typing", &self.allow_incoming_typing.to_string());
line(
@ -1054,3 +1057,5 @@ fn default_sentry_traces_sample_rate() -> f32 { 0.15 }
fn default_sentry_filter() -> String { "info".to_owned() }
fn default_startup_netburst_keep() -> i64 { 50 }
fn default_admin_log_capture() -> String { "debug".to_owned() }