2
\$\begingroup\$

I am currently working in a Zend Framework 1 project. In the project we needed a sort of logging module, and Monolog was requested.

Now since I'm quite a beginner with PHP, I'd like to know if I'm doing this right. I feel like I'm missing the whole prospect of Monolog:

Custom Handler:

<?php

use Monolog\Logger;
use Monolog\Handler\AbstractProcessingHandler;

Class Default_Monolog_DBHandler extends AbstractProcessingHandler
{
    private $initialized = false;
    private $statement;

    public function __construct($level = Logger::DEBUG, $bubble = true)
    {
        parent::__construct($level, $bubble);
    }

    protected function write(array $record)
    {
        $log = new \Log();
        $log->add($record);
    }

    public function addInfo($type, $message, $order_id)
    {
        $identity = Zend_Auth::getInstance()->getIdentity();

        $record = array(
            'log'           => $message,
            'date_added'    => date('Y-m-d h:i:s'),
            'type'          => $type,
            'user_id'       => $identity->user_id,
            'order_id'      => $order_id
        );

        $this->write($record);
    }
}

Log.php

    public static function add(array $record)
    {
        $instance = self::getInstance();
        return $instance->insert($record);
    }

And in my controller I create a log like this:

$log = new Default_Monolog_DBHandler();
$log->addInfo('info', 'test', null);

Is this the right way to do?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.