From 1bdd51d9a3a4dfbbacbe949429c66b016f07f37f Mon Sep 17 00:00:00 2001 From: wpdev Date: Sun, 27 Sep 2026 16:35:13 +0800 Subject: [PATCH] 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 --- includes/class-tmdo-logger.php | 20 ++++++++++++++++++-- tests/unit/LoggerTest.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/includes/class-tmdo-logger.php b/includes/class-tmdo-logger.php index feac105..1ceff62 100644 --- a/includes/class-tmdo-logger.php +++ b/includes/class-tmdo-logger.php @@ -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 $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 ); } } diff --git a/tests/unit/LoggerTest.php b/tests/unit/LoggerTest.php index 9b3cffa..b4f9578 100644 --- a/tests/unit/LoggerTest.php +++ b/tests/unit/LoggerTest.php @@ -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' ) ) ); + } }