fix(2meet-data-optimizer): log info and notice events only when debug logging is on

- TMDO_Logger::info()/notice() write to the PHP error log only with
  WP_DEBUG_LOG (filter tmdo_log_info); every CLI run and production request
  printed [WPDO][INFO] lines
- debug() keeps its WP_DEBUG gate; warning() and error() are unchanged
- Verify: LoggerTest (2 new), unit 700, integration 577, phpcs, stan
- PR: PR-095
This commit is contained in:
2026-09-27 16:35:13 +08:00
parent 22b46ad175
commit 1bdd51d9a3
2 changed files with 52 additions and 2 deletions
+18 -2
View File
@@ -48,7 +48,7 @@ class TMDO_Logger {
/**
* Log an INFO-level event (event-based signature, used by v2.0.0 engine code).
*
* Writes only to PHP error_log; does NOT touch wpdo_errors (which is reserved
* Writes only to PHP error_log, and only when debug logging is on; does NOT touch wpdo_errors (which is reserved
* for actual errors). Mode changes / cache flushes / audit prunes call here
* many times per request — keeping them out of the DB error table.
*
@@ -57,6 +57,22 @@ class TMDO_Logger {
* @return void
*/
public static function info( string $event, array $context = array() ): void {
// Routine events: only with WP_DEBUG_LOG (filter `tmdo_log_info`), or every
// CLI run and production request would flood the log.
if ( ! apply_filters( 'tmdo_log_info', defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) ) {
return;
}
self::write_info( $event, $context );
}
/**
* Write an INFO line to the PHP error log.
*
* @param string $event Event name.
* @param array<string, mixed> $context Structured context.
* @return void
*/
private static function write_info( string $event, array $context ): void {
error_log( sprintf( '[WPDO][INFO][%s] %s', $event, $context ? wp_json_encode( $context, JSON_UNESCAPED_UNICODE ) : '' ) );
}
@@ -97,7 +113,7 @@ class TMDO_Logger {
*/
public static function debug( string $event, array $context = array() ): void {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
self::info( $event, $context );
self::write_info( $event, $context );
}
}
+34
View File
@@ -130,4 +130,38 @@ class LoggerTest extends TestCase {
$deleted = WPDO_Logger::purge( 30 );
$this->assertSame( 5, $deleted );
}
/** error_log() output of $fn. */
private function logged( callable $fn ): string {
$file = tempnam( sys_get_temp_dir(), 'tmdo-log' );
$old = ini_set( 'error_log', $file );
try {
$fn();
} finally {
ini_set( 'error_log', (string) $old );
}
$out = (string) file_get_contents( $file );
unlink( $file );
return $out;
}
public function test_info_and_notice_stay_out_of_the_log_unless_debug_logging_is_on(): void {
$GLOBALS['_wp_filter_callbacks']['tmdo_log_info'] = array();
$this->assertSame( '', $this->logged( static function () {
WPDO_Logger::info( 'cache_flushed' );
WPDO_Logger::notice( 'mode_changed' );
} ) );
add_filter( 'tmdo_log_info', static fn() => true );
try {
$this->assertStringContainsString( '[WPDO][INFO][cache_flushed]', $this->logged( static fn() => WPDO_Logger::info( 'cache_flushed' ) ) );
} finally {
$GLOBALS['_wp_filter_callbacks']['tmdo_log_info'] = array();
}
}
public function test_warnings_are_always_logged(): void {
$this->assertStringContainsString( '[WPDO][WARN][odd_return]', $this->logged( static fn() => WPDO_Logger::warning( 'odd_return' ) ) );
}
}