Skip to content

Commit b68f046

Browse files
committed
fix: add log level gate to prevent debug/info writes when level is set higher
datamachine_log_message() was writing all log levels to the database unconditionally — the datamachine_log_level option was never checked. On the events site (level=error), this caused ~580K unnecessary INSERTs/day. Adds severity comparison before the INSERT: messages below the configured minimum level are silently discarded.
1 parent 32a712d commit b68f046

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

inc/Engine/Logger.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,32 @@ function datamachine_resolve_agent_id( array $context = array() ): ?int {
6464
* Resolves agent_id from context or PermissionHelper, resolves user_id,
6565
* and inserts into the datamachine_logs table via LogRepository.
6666
*
67+
* Respects the configured minimum log level (`datamachine_log_level` option).
68+
* Messages below the minimum severity are silently discarded.
69+
*
6770
* @param string $level Log level string (debug, info, warning, error, critical).
6871
* @param string|\Stringable $message Message to log.
6972
* @param array $context Optional context data.
7073
*/
7174
function datamachine_log_message( string $level, string|\Stringable $message, array $context = array() ): void {
7275
try {
76+
// Gate: discard messages below the configured minimum log level.
77+
$severity_map = array(
78+
'debug' => 0,
79+
'info' => 1,
80+
'warning' => 2,
81+
'error' => 3,
82+
'critical' => 4,
83+
);
84+
85+
$min_level = get_option( 'datamachine_log_level', 'info' );
86+
$min_severity = $severity_map[ $min_level ] ?? 1;
87+
$msg_severity = $severity_map[ $level ] ?? 0;
88+
89+
if ( $msg_severity < $min_severity ) {
90+
return;
91+
}
92+
7393
$repo = new LogRepository();
7494
$agent_id = datamachine_resolve_agent_id( $context );
7595

0 commit comments

Comments
 (0)