Zendcasts Forum

A community of developers who work with the Zend Framework and other enterprise PHP technologies

You are not logged in.

#1 2010-06-15 16:06:52

Jon Lebensold
Administrator
Registered: 2009-06-27
Posts: 279

ZC55 – Transparent Logging with Zend_Log

I was working on a project for a client the other day and noticed a couple of lines in the ErrorController for automatically logging errors with Zend_Log. In 10 minutes, you can have a fully integrated logging framework. I also implement a singleton pattern for reusing your Zend_Log configuration (defined in the application.ini) anywhere else in your application.

Offline

#2 2010-06-15 16:48:01

stephenminded
New member
Registered: 2010-06-15
Posts: 2

Re: ZC55 – Transparent Logging with Zend_Log

Good screencast! I've actually solved the same problem in a slightly different way. I setup a controller helper as a proxy to the log object setup in the log resource. This way, the registry isn't needed and I don't have to use a singleton. My log calls look like the following:

$this->_helper->log('hello again'); // defaults to debug level

or, for a specific level:

$this->_helper->log->info('an info message');

The code for the helper is here: http://gist.github.com/406559

Keep up the good work on the screencasts!
-steve

Offline

#3 2010-06-15 16:55:08

Jon Lebensold
Administrator
Registered: 2009-06-27
Posts: 279

Re: ZC55 – Transparent Logging with Zend_Log

Hey Stephen, this looks good... but I wonder if you can use it without relying on Zend_Controller

Offline

#4 2010-06-15 16:57:47

stephenminded
New member
Registered: 2010-06-15
Posts: 2

Re: ZC55 – Transparent Logging with Zend_Log

You're right, it's definitely dependent on Zend_Controller and thus can really only be used within a controller. If you wanted to log in a service layer or elsewhere, the singleton solution works better, I think.

-steve

Offline

#5 2010-06-20 06:39:10

Ivo Trompert
New member
From: Amersfoort, Netherlands
Registered: 2009-08-06
Posts: 1

Re: ZC55 – Transparent Logging with Zend_Log

I have written a error log writer for Doctrine:

The class:

<?php

/**
 * A log writer for Doctrine
 *
 * @author ivotrompert
 */
class Ivo_Log_Writer_Doctrine extends Zend_Log_Writer_Abstract {

    /**
     * The Doctrine class name for the tabel
     * 
     * @var String
     */
    private $_model;

    /**
     * Relates database columns names to log data field keys.
     *
     * @var null|array
     */
    private $_columnMap;

    /**
     * @param string $model
     * @param array $columnMap
     */

    public function  __construct($model,$columnMap) {

        $this->_model = $model;
        $this->_columnMap = $columnMap;
    }

    static public function  factory($config) {
        $config = self::_parseConfig($config);
        $config = array_merge(array(
            'model'     => null,
            'columnMap' => null,
        ), $config);

        if (isset($config['columnmap'])) {
            $config['columnMap'] = $config['columnmap'];
        }

        return new self(
            $config['model'],
            $config['columnMap']
        );
    }

    /**
     * Formatting is not possible on this writer
     */
    public function setFormatter($formatter)
    {
        require_once 'Zend/Log/Exception.php';
        throw new Zend_Log_Exception(get_class() . ' does not support formatting');
    }

    /**
     * Write a message to the log.
     *
     * @param  array  $event  event data
     * @return void
     */
    protected function _write($event)
    {
        if ($this->_model === null) {
            require_once 'Zend/Log/Exception.php';
            throw new Zend_Log_Exception('Doctrine modelname is null');
        }

        if ($this->_columnMap === null) {
            require_once 'Zend/Log/Exception.php';
            throw new Zend_Log_Exception('Colum mapping is null');
        } else {
            $dataToInsert = array();
            foreach ($this->_columnMap as $columnName => $fieldKey) {
                $dataToInsert[$columnName] = $event[$fieldKey];
            }
        }

        $model = new $this->_model;
        
        $model->fromArray($dataToInsert);
        $model->save();
    }
    
}
?>

The setup in the ini file:

resources.log.database.writerNamespace = "Ivo_Log_Writer"
resources.log.database.writerName = "Doctrine"
resources.log.database.writerParams.model = "Model_Log"
resources.log.database.writerParams.columnMap.title = "message"
resources.log.database.writerParams.columnMap.priority ="priority"
resources.log.database.writerParams.columnMap.priorityName = "priorityName"
resources.log.database.writerParams.columnMap.message = "info"

The YML file:

options:
  type: INNODB
  collate: utf8_general_ci
  charset: utf8
  
Log:
  columns:
    id:
      type: integer
      primary: true
      autoincrement: true
    priority: tinyint
    priorityName: string(10)
    title: string(250)
    message: text

Maybe someone has some use for it smile

Ivo

Last edited by Ivo Trompert (2010-06-20 06:40:13)

Offline

#6 2010-06-20 11:16:00

Jon Lebensold
Administrator
Registered: 2009-06-27
Posts: 279

Re: ZC55 – Transparent Logging with Zend_Log

Hi Ivo,

this is really neat. I wonder if you could add a postSave hook to your other models that would automatically log changes..

Offline

Board footer

Powered by FluxBB