11<?php
22/*
3- Plugin Name: BEA lOGGER
4- Version: 0.3
5- Plugin URI: https://github.com/beapi/bea-logger
6- Description: Allow to log basic data on a log file
7- Author: BeAPI
8- Author URI: http://www.beapi.fr
9-
10- ----
11-
12- Copyright 2015 Beapi Technical team (technique@beapi.fr)
13-
14- This program is free software; you can redistribute it and/or modify
15- it under the terms of the GNU General Public License as published by
16- the Free Software Foundation; either version 2 of the License, or
17- (at your option) any later version.
18-
19- This program is distributed in the hope that it will be useful,
20- but WITHOUT ANY WARRANTY; without even the implied warranty of
21- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22- GNU General Public License for more details.
23-
24- You should have received a copy of the GNU General Public License
25- along with this program; if not, write to the Free Software
26- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3+ Plugin Name: BEA lOGGER
4+ Version: 0.4
5+ Plugin URI: https://github.com/beapi/bea-logger
6+ Description: Allow to log basic data on a log file
7+ Author: BeAPI
8+ Author URI: http://www.beapi.fr
9+
10+ ----
11+
12+ Copyright 2015 Beapi Technical team (technique@beapi.fr)
13+
14+ This program is free software; you can redistribute it and/or modify
15+ it under the terms of the GNU General Public License as published by
16+ the Free Software Foundation; either version 2 of the License, or
17+ (at your option) any later version.
18+
19+ This program is distributed in the hope that it will be useful,
20+ but WITHOUT ANY WARRANTY; without even the implied warranty of
21+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+ GNU General Public License for more details.
23+
24+ You should have received a copy of the GNU General Public License
25+ along with this program; if not, write to the Free Software
26+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2727 */
2828
2929/**
3030 * Check if class already exists
31- * @since 0.3
31+ *
32+ * @since 0.4
3233 */
3334if ( class_exists ( 'Bea_Log ' ) ) {
3435 return ;
35- }
36+ }
3637
3738class Bea_Log {
3839
@@ -46,9 +47,10 @@ class Bea_Log {
4647 /**
4748 * The log file extension
4849 * .log by default
50+ *
4951 * @var string
5052 */
51- private $ file_extension ='.log ' ;
53+ private $ file_extension = '.log ' ;
5254
5355 /**
5456 * The log file max size
@@ -80,11 +82,12 @@ class Bea_Log {
8082 /**
8183 * Construct the logged file
8284 *
83- * @param $file_path
84- * @param string $file_extension
85- * @param string $retention_size
85+ * @param string $file_path Base path without extension.
86+ * @param string $file_extension File extension including dot.
87+ * @param string $retention_size Max file size in bytes before rotation.
88+ * @param bool $write_enabled When false, log_this is a no-op (no disk I/O).
8689 */
87- function __construct ( $ file_path , $ file_extension = '.log ' , $ retention_size = '' ) {
90+ function __construct ( $ file_path , $ file_extension = '.log ' , $ retention_size = '' , $ write_enabled = true ) {
8891 if ( ! isset ( $ file_path ) || empty ( $ file_path ) ) {
8992 return false ;
9093 }
@@ -102,15 +105,14 @@ function __construct( $file_path, $file_extension = '.log', $retention_size = ''
102105 $ this ->retention_size = $ retention_size ;
103106 }
104107
105- $ this ->is_configured = true ;
108+ $ this ->is_configured = ( bool ) $ write_enabled ;
106109 }
107110
108111 /**
109112 * Log data in multiple files when full
110113 *
111- * @param $message
112- * @param string $type
113- *extension
114+ * @param mixed $message Log payload.
115+ * @param string $type Gravity label.
114116 * @return bool
115117 * @author Nicolas Juen
116118 */
@@ -120,13 +122,13 @@ public function log_this( $message, $type = self::gravity_7 ) {
120122 }
121123
122124 // Make the file path
123- $ file_path = $ this ->file_path . $ this ->file_extension ;
125+ $ file_path = $ this ->file_path . $ this ->file_extension ;
124126
125127 // Maybe move the file
126128 $ this ->maybe_move_file ( $ file_path );
127129
128130 // Log the error
129- error_log ( sprintf ( '[%s][%s] %s ' , date ( 'd-m-Y H:i:s ' ), $ type , self ::convert_message ( $ message ) ). "\n" , 3 , $ file_path );
131+ error_log ( sprintf ( '[%s][%s] %s ' , date ( 'd-m-Y H:i:s ' ), $ type , self ::convert_message ( $ message ) ) . "\n" , 3 , $ file_path );
130132
131133 return true ;
132134 }
@@ -163,8 +165,47 @@ private function maybe_move_file( $file_path ) {
163165 return ;
164166 }
165167
166- // Rename the file
167- rename ( $ file_path , sprintf ( '%s-%s%s ' , $ this ->file_path , date ( 'Y-m-d-H-i-s ' ), $ this ->file_extension ) );
168+ $ rotated_path = sprintf ( '%s-%s%s ' , $ this ->file_path , date ( 'Y-m-d-H-i-s ' ), $ this ->file_extension );
169+
170+ // Rename the file, then compress the rotated log when possible.
171+ if ( rename ( $ file_path , $ rotated_path ) ) {
172+ $ this ->maybe_zip_rotated_file ( $ rotated_path );
173+ }
174+ }
175+
176+ /**
177+ * Compress a rotated log file into a sibling .zip and remove the plain file on success.
178+ *
179+ * @param string $rotated_path Absolute path to the rotated .log file.
180+ */
181+ private function maybe_zip_rotated_file ( $ rotated_path ) {
182+ if ( ! class_exists ( 'ZipArchive ' ) ) {
183+ return ;
184+ }
185+
186+ if ( ! is_string ( $ rotated_path ) || '' === $ rotated_path || ! is_file ( $ rotated_path ) ) {
187+ return ;
188+ }
189+
190+ $ zip_path = $ rotated_path . '.zip ' ;
191+ $ zip = new ZipArchive ();
192+
193+ if ( true !== $ zip ->open ( $ zip_path , ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) {
194+ return ;
195+ }
196+
197+ $ zip ->addFile ( $ rotated_path , basename ( $ rotated_path ) );
198+
199+ if ( ! $ zip ->close () ) {
200+ if ( is_file ( $ zip_path ) ) {
201+ unlink ( $ zip_path );
202+ }
203+ return ;
204+ }
205+
206+ if ( is_file ( $ zip_path ) && filesize ( $ zip_path ) > 0 ) {
207+ unlink ( $ rotated_path );
208+ }
168209 }
169210
170211 /**
0 commit comments