| 1 |
<?php |
| 2 |
/** |
| 3 |
* WordPress handler for Monolog |
| 4 |
* |
| 5 |
* Handles all features of WordPress handler for Monolog. |
| 6 |
* |
| 7 |
* @package Handlers |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Decalog\Handler; |
| 13 |
|
| 14 |
use DLMonolog\Logger; |
| 15 |
use DLMonolog\Handler\AbstractProcessingHandler; |
| 16 |
use DLMonolog\Formatter\FormatterInterface; |
| 17 |
use Decalog\Formatter\WordpressFormatter; |
| 18 |
use Decalog\Storage\DBStorage; |
| 19 |
use Decalog\Storage\APCuStorage; |
| 20 |
|
| 21 |
/** |
| 22 |
* Define the Monolog WordPress handler. |
| 23 |
* |
| 24 |
* Handles all features of WordPress handler for Monolog. |
| 25 |
* |
| 26 |
* @package Handlers |
| 27 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
class WordpressHandler extends AbstractProcessingHandler { |
| 31 |
|
| 32 |
/** |
| 33 |
* The table name. |
| 34 |
* |
| 35 |
* @since 1.0.0 |
| 36 |
* @var string $table The table name. |
| 37 |
*/ |
| 38 |
private $table = ''; |
| 39 |
|
| 40 |
/** |
| 41 |
* The storage engine. |
| 42 |
* |
| 43 |
* @since 3.0.0 |
| 44 |
* @var \Decalog\Storage\AbstractStorage $storage The storage engine. |
| 45 |
*/ |
| 46 |
private $storage = null; |
| 47 |
|
| 48 |
/** |
| 49 |
* Initialize the class and set its properties. |
| 50 |
* |
| 51 |
* @param string $table The table name. |
| 52 |
* @param string $storage The storage type. |
| 53 |
* @param integer $level Optional. The min level to log. |
| 54 |
* @param boolean $bubble Optional. Has the record to bubble?. |
| 55 |
* @since 1.0.0 |
| 56 |
*/ |
| 57 |
public function __construct( string $table, string $storage, $level = Logger::DEBUG, bool $bubble = true ) { |
| 58 |
parent::__construct( $level, $bubble ); |
| 59 |
$this->table = $table; |
| 60 |
switch ( $storage ) { |
| 61 |
case 'apcu': |
| 62 |
$this->storage = new APCuStorage( $this->table ); |
| 63 |
break; |
| 64 |
default: |
| 65 |
$this->storage = new DBStorage( $this->table ); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* {@inheritDoc} |
| 71 |
*/ |
| 72 |
protected function getDefaultFormatter(): FormatterInterface { |
| 73 |
return new WordpressFormatter(); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Write the record in the table. |
| 78 |
* |
| 79 |
* @param array $record The record to write. |
| 80 |
* @since 1.0.0 |
| 81 |
*/ |
| 82 |
protected function write( array $record ): void { |
| 83 |
// phpcs:ignore |
| 84 |
$messages = unserialize( $record['formatted'] ); |
| 85 |
if ( is_array( $messages ) ) { |
| 86 |
foreach ( $messages as $message ) { |
| 87 |
if ( is_array( $message ) ) { |
| 88 |
$this->storage->insert_value( $message ); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|