PHP Classes

File: vendor/monolog/monolog/doc/

Recommend this page to a friend!
  Classes of Renato De Oliveira Lucena  >  PHP Pokemon Script  >  vendor/monolog/monolog/doc/  >  Download  
File: vendor/monolog/monolog/doc/
Role: Class source
Content type: text/markdown
Description: Class source
Class: PHP Pokemon Script
Provides an API to manage a database of Pokemons
Author: By
Last change:
Date: 2 years ago
Size: 2,159 bytes



Class file image Download

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.


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) {

            'channel' => $record['channel'],
            'level' => $record['level'],
            'message' => $record['formatted'],
            'time' => $record['datetime']->format('U'),

    private function initialize()
            '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:


$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']`).

&larr; Utility classes

For more information send a message to info at phpclasses dot org.