Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 38 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
# Bea logger
Basic and simple logger
# Bea Logger

# Usage
Basic and simple logger for WordPress.

## Usage

<?php $logger = new Bea_Log( WP_CONTENT_DIR . '/my-logger' );
$logger->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`).
127 changes: 88 additions & 39 deletions bea-logger.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
<?php
/*
Plugin Name: BEA lOGGER
Version: 0.3
Plugin URI: https://github.com/beapi/bea-logger
Description: Allow to log basic data on a log file
Author: BeAPI
Author URI: http://www.beapi.fr

----

Copyright 2015 Beapi Technical team (technique@beapi.fr)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Plugin Name: BEA lOGGER
Version: 0.4
Plugin URI: https://github.com/beapi/bea-logger
Description: Allow to log basic data on a log file
Author: BeAPI
Author URI: http://www.beapi.fr

----

Copyright 2015 Beapi Technical team (technique@beapi.fr)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

/**
* Check if class already exists
* @since 0.3
*
* @since 0.4
*/
if ( class_exists( 'Bea_Log' ) ) {
return;
}
}

class Bea_Log {

Expand All @@ -46,9 +47,10 @@ class Bea_Log {
/**
* The log file extension
* .log by default
*
* @var string
*/
private $file_extension ='.log' ;
private $file_extension = '.log';

/**
* The log file max size
Expand Down Expand Up @@ -80,11 +82,12 @@ class Bea_Log {
/**
* Construct the logged file
*
* @param $file_path
* @param string $file_extension
* @param string $retention_size
* @param string $file_path Base path without extension.
* @param string $file_extension File extension including dot.
* @param string $retention_size Max file size in bytes before rotation.
* @param bool $write_enabled When false, log_this is a no-op (no disk I/O).
*/
function __construct( $file_path, $file_extension = '.log', $retention_size = '' ) {
function __construct( $file_path, $file_extension = '.log', $retention_size = '', $write_enabled = true ) {
if ( ! isset( $file_path ) || empty( $file_path ) ) {
return false;
}
Expand All @@ -102,15 +105,14 @@ function __construct( $file_path, $file_extension = '.log', $retention_size = ''
$this->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
*/
Expand All @@ -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;
}
Expand Down Expand Up @@ -163,8 +165,55 @@ 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 );
Comment thread
pngouahbeapi marked this conversation as resolved.
}
}

/**
* 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;
}

$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 ) ) {
unlink( $zip_path );
}
return;
}

if ( is_file( $zip_path ) && filesize( $zip_path ) > 0 ) {
unlink( $rotated_path );
Comment thread
cursor[bot] marked this conversation as resolved.
}
}

/**
Expand Down