You are not logged in.
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
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 levelor, 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
Hey Stephen, this looks good... but I wonder if you can use it without relying on Zend_Controller
Offline
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
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: textMaybe someone has some use for it ![]()
Ivo
Last edited by Ivo Trompert (2010-06-20 06:40:13)
Offline
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