Skip to content

Commit 635db99

Browse files
committed
889116 - Make log optional + zip log file when size exceed retention_size
1 parent 654a8a2 commit 635db99

2 files changed

Lines changed: 118 additions & 55 deletions

File tree

README.md

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,46 @@
1-
# Bea logger
2-
Basic and simple logger
1+
# Bea Logger
32

4-
# Usage
3+
Basic and simple logger for WordPress.
54

5+
## Usage
66

7-
<?php $logger = new Bea_Log( WP_CONTENT_DIR . '/my-logger' );
8-
$logger->log_this( 'Log this message', Bea_Log::gravity_0 );
7+
```php
8+
$logger = new Bea_Log( WP_CONTENT_DIR . '/my-logger' );
9+
$logger->log_this( 'Log this message', Bea_Log::gravity_0 );
10+
```
911

10-
Will log something like this this :
12+
Produces a line like:
1113

14+
```
1215
[d-m-Y H:i:s][Emerg] Log this message
16+
```
1317

18+
## Constructor
1419

15-
const gravity_0 = 'Emerg';
16-
const gravity_1 = 'Alert';
17-
const gravity_2 = 'Crit';
18-
const gravity_3 = 'Err';
19-
const gravity_4 = 'Warning';
20-
const gravity_5 = 'Notice';
21-
const gravity_6 = 'Info';
22-
const gravity_7 = 'Debug';
23-
24-
By default the level will be gravity_7
20+
```php
21+
new Bea_Log( $file_path, $file_extension = '.log', $retention_size = '', $write_enabled = true );
22+
```
23+
24+
- `$file_path`: base path without extension.
25+
- `$file_extension`: file extension including the dot (default `.log`).
26+
- `$retention_size`: max file size in bytes before rotation (default `419430400`, ~400 MB).
27+
- `$write_enabled`: when `false`, `log_this()` is a no-op (no disk I/O).
28+
29+
## Rotation & compression
30+
31+
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).
32+
33+
## Gravity levels
34+
35+
```php
36+
const gravity_0 = 'Emerg';
37+
const gravity_1 = 'Alert';
38+
const gravity_2 = 'Crit';
39+
const gravity_3 = 'Err';
40+
const gravity_4 = 'Warning';
41+
const gravity_5 = 'Notice';
42+
const gravity_6 = 'Info';
43+
const gravity_7 = 'Debug';
44+
```
45+
46+
Default level is `gravity_7` (`Debug`).

bea-logger.php

Lines changed: 80 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
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
*/
3334
if ( class_exists( 'Bea_Log' ) ) {
3435
return;
35-
}
36+
}
3637

3738
class 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

Comments
 (0)