Linux unitednationsplay.com 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64
nginx/1.20.1
Server IP : 188.130.139.92 & Your IP : 3.12.164.78
Domains :
Cant Read [ /etc/named.conf ]
User : web
Terminal
Auto Root
Create File
Create Folder
Localroot Suggester
Backdoor Destroyer
Readme
/
home /
www /
3 /
monolog /
monolog /
doc /
Delete
Unzip
Name
Size
Permission
Date
Action
01-usage.md
8.28
KB
-rw-r--r--
2018-11-05 09:00
02-handlers-formatters-processors.md
9.82
KB
-rw-r--r--
2018-11-05 09:00
03-utilities.md
928
B
-rw-r--r--
2018-11-05 09:00
04-extending.md
2.11
KB
-rw-r--r--
2018-11-05 09:00
sockets.md
1023
B
-rw-r--r--
2018-11-05 09:00
Save
Rename
# Extending Monolog Monolog is fully extensible, allowing you to adapt your logger to your needs. ## Writing your own handler Monolog provides many built-in handlers. But if the one you need does not exist, you can write it and use it in your logger. The only requirement is to implement `Monolog\Handler\HandlerInterface`. Let's write a PDOHandler to log records to a database. We will extend the abstract class provided by Monolog to keep things DRY. ```php <?php use Monolog\Logger; use Monolog\Handler\AbstractProcessingHandler; class PDOHandler extends AbstractProcessingHandler { private $initialized = false; private $pdo; private $statement; public function __construct(PDO $pdo, $level = Logger::DEBUG, $bubble = true) { $this->pdo = $pdo; parent::__construct($level, $bubble); } protected function write(array $record) { if (!$this->initialized) { $this->initialize(); } $this->statement->execute(array( 'channel' => $record['channel'], 'level' => $record['level'], 'message' => $record['formatted'], 'time' => $record['datetime']->format('U'), )); } private function initialize() { $this->pdo->exec( 'CREATE TABLE IF NOT EXISTS monolog ' .'(channel VARCHAR(255), level INTEGER, message LONGTEXT, time INTEGER UNSIGNED)' ); $this->statement = $this->pdo->prepare( 'INSERT INTO monolog (channel, level, message, time) VALUES (:channel, :level, :message, :time)' ); $this->initialized = true; } } ``` You can now use this handler in your logger: ```php <?php $logger->pushHandler(new PDOHandler(new PDO('sqlite:logs.sqlite'))); // You can now use your logger $logger->addInfo('My logger is now ready'); ``` The `Monolog\Handler\AbstractProcessingHandler` class provides most of the logic needed for the handler, including the use of processors and the formatting of the record (which is why we use ``$record['formatted']`` instead of ``$record['message']``). ← [Utility classes](03-utilities.md)