From 635db99939a6f2f45fe43408a20a5dbe35ec40c0 Mon Sep 17 00:00:00 2001 From: Paul NGOUAH MAVIANE Date: Wed, 13 May 2026 10:16:49 +0200 Subject: [PATCH 1/2] 889116 - Make log optional + zip log file when size exceed retention_size --- README.md | 54 +++++++++++++++------- bea-logger.php | 119 +++++++++++++++++++++++++++++++++---------------- 2 files changed, 118 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index de8dc3d..a35ed4c 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,46 @@ -# Bea logger -Basic and simple logger +# Bea Logger -# Usage +Basic and simple logger for WordPress. +## Usage - log_this( 'Log this message', Bea_Log::gravity_0 ); +```php +$logger = new Bea_Log( WP_CONTENT_DIR . '/my-logger' ); +$logger->log_this( 'Log this message', Bea_Log::gravity_0 ); +``` -Will log something like this this : +Produces a line like: +``` [d-m-Y H:i:s][Emerg] Log this message +``` +## Constructor - const gravity_0 = 'Emerg'; - const gravity_1 = 'Alert'; - const gravity_2 = 'Crit'; - const gravity_3 = 'Err'; - const gravity_4 = 'Warning'; - const gravity_5 = 'Notice'; - const gravity_6 = 'Info'; - const gravity_7 = 'Debug'; - -By default the level will be gravity_7 +```php +new Bea_Log( $file_path, $file_extension = '.log', $retention_size = '', $write_enabled = true ); +``` + +- `$file_path`: base path without extension. +- `$file_extension`: file extension including the dot (default `.log`). +- `$retention_size`: max file size in bytes before rotation (default `419430400`, ~400 MB). +- `$write_enabled`: when `false`, `log_this()` is a no-op (no disk I/O). + +## Rotation & compression + +When the log file exceeds `$retention_size`, it is renamed to `{file_path}-{Y-m-d-H-i-s}{ext}` and, when `ZipArchive` is available, compressed into a sibling `.zip` (the plain rotated file is then removed). + +## Gravity levels + +```php +const gravity_0 = 'Emerg'; +const gravity_1 = 'Alert'; +const gravity_2 = 'Crit'; +const gravity_3 = 'Err'; +const gravity_4 = 'Warning'; +const gravity_5 = 'Notice'; +const gravity_6 = 'Info'; +const gravity_7 = 'Debug'; +``` + +Default level is `gravity_7` (`Debug`). diff --git a/bea-logger.php b/bea-logger.php index 835e5a6..343e5bf 100644 --- a/bea-logger.php +++ b/bea-logger.php @@ -1,38 +1,39 @@ retention_size = $retention_size; } - $this->is_configured = true; + $this->is_configured = (bool) $write_enabled; } /** * Log data in multiple files when full * - * @param $message - * @param string $type - *extension + * @param mixed $message Log payload. + * @param string $type Gravity label. * @return bool * @author Nicolas Juen */ @@ -120,13 +122,13 @@ public function log_this( $message, $type = self::gravity_7 ) { } // Make the file path - $file_path = $this->file_path.$this->file_extension; + $file_path = $this->file_path . $this->file_extension; // Maybe move the file $this->maybe_move_file( $file_path ); // Log the error - error_log( sprintf( '[%s][%s] %s', date( 'd-m-Y H:i:s' ), $type, self::convert_message( $message ) )."\n", 3, $file_path ); + error_log( sprintf( '[%s][%s] %s', date( 'd-m-Y H:i:s' ), $type, self::convert_message( $message ) ) . "\n", 3, $file_path ); return true; } @@ -163,8 +165,47 @@ private function maybe_move_file( $file_path ) { return; } - // Rename the file - rename( $file_path, sprintf( '%s-%s%s', $this->file_path, date( 'Y-m-d-H-i-s' ), $this->file_extension ) ); + $rotated_path = sprintf( '%s-%s%s', $this->file_path, date( 'Y-m-d-H-i-s' ), $this->file_extension ); + + // Rename the file, then compress the rotated log when possible. + if ( rename( $file_path, $rotated_path ) ) { + $this->maybe_zip_rotated_file( $rotated_path ); + } + } + + /** + * Compress a rotated log file into a sibling .zip and remove the plain file on success. + * + * @param string $rotated_path Absolute path to the rotated .log file. + */ + private function maybe_zip_rotated_file( $rotated_path ) { + if ( ! class_exists( 'ZipArchive' ) ) { + return; + } + + if ( ! is_string( $rotated_path ) || '' === $rotated_path || ! is_file( $rotated_path ) ) { + return; + } + + $zip_path = $rotated_path . '.zip'; + $zip = new ZipArchive(); + + if ( true !== $zip->open( $zip_path, ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) { + return; + } + + $zip->addFile( $rotated_path, basename( $rotated_path ) ); + + if ( ! $zip->close() ) { + if ( is_file( $zip_path ) ) { + unlink( $zip_path ); + } + return; + } + + if ( is_file( $zip_path ) && filesize( $zip_path ) > 0 ) { + unlink( $rotated_path ); + } } /** From 90a941ddc6c5e7cd57fe8f9beb491f1e9e2fd44d Mon Sep 17 00:00:00 2001 From: Paul NGOUAH MAVIANE Date: Wed, 13 May 2026 12:05:46 +0200 Subject: [PATCH 2/2] Add control on zip file addition --- bea-logger.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/bea-logger.php b/bea-logger.php index 343e5bf..fb0f6db 100644 --- a/bea-logger.php +++ b/bea-logger.php @@ -194,7 +194,15 @@ private function maybe_zip_rotated_file( $rotated_path ) { return; } - $zip->addFile( $rotated_path, basename( $rotated_path ) ); + $entry_name = basename( $rotated_path ); + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- ZipArchive API property. + if ( ! $zip->addFile( $rotated_path, $entry_name ) || $zip->numFiles < 1 ) { + $zip->close(); + if ( is_file( $zip_path ) ) { + unlink( $zip_path ); + } + return; + } if ( ! $zip->close() ) { if ( is_file( $zip_path ) ) {