From 2129829e7d7081440360dabfc1f49d1a4de761ea Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Fri, 4 Oct 2024 10:09:10 +0200 Subject: [PATCH] Avoid OOM errors because of large log files --- .../Tools/class-health-check-debug-log-viewer.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/HealthCheck/Tools/class-health-check-debug-log-viewer.php b/HealthCheck/Tools/class-health-check-debug-log-viewer.php index c3bc67f..7f9e4f0 100644 --- a/HealthCheck/Tools/class-health-check-debug-log-viewer.php +++ b/HealthCheck/Tools/class-health-check-debug-log-viewer.php @@ -28,7 +28,16 @@ private function read_debug_log() { return ''; } - $debug_log = @file_get_contents( $logfile ); + if ( ! is_readable( $logfile ) ) { + return sprintf( + // translators: %s: The path to the debug log file. + __( 'The debug log file found at `%s`, could not be read.', 'health-check' ), + $logfile + ); + } + + // Only read the last 200k of the log file to avoid Oout of memory errors. + $debug_log = file_get_contents( $logfile, null, null, max( 0, filesize( $logfile ) - 200000 ) ); if ( false === $debug_log ) { return sprintf(