Zend_Log wrapper for Propel
Although Doctrine has its advantages, I still like Propel. It does its job and it does it fairly well.
If you integrate Propel with Zend Framework, you basically only need a Zend_Log wrapper that implements the BasicLogger interface Propel requires. This lets you use Zend_Log as the only log class in your application instead of creating one especially for Propel.
/**
* Wrapper for Zend_Log for use as Propel log
* @author Sam Hauglustaine
*/
class strictlyPHP_Propel_Log extends Zend_Log implements BasicLogger
{
/**
* Log
* @var Zend_Log
*/
private $_log;
/**
* Constructor
* @param Zend_Log $log
*/
public function __construct(Zend_Log $log) {
$this->_log = $log;
}
/**
* Log message of specified severity
* @param string $message
* @param int $severity
*/
public function log($message, $severity = null) {
$this->_log->log($message, $severity);
}
public function emergency($message) {
$this->log($message, Zend_Log::EMERG);
}
public function alert($message) {
$this->log($message, Zend_Log::ALERT);
}
public function crit($message) {
$this->log($message, Zend_Log::CRIT);
}
public function err($message) {
$this->log($message, Zend_Log::ERR);
}
public function warning($message) {
$this->log($message, Zend_Log::WARN);
}
public function notice($message) {
$this->log($message, Zend_Log::NOTICE);
}
public function info($message) {
$this->log($message, Zend_Log::INFO);
}
public function debug($message) {
$this->log($message, Zend_Log::DEBUG);
}
}
(the Zend_Log priority constants nicely match their Propel equivalents.)
You then need to extend the main Propel class to do the wrapping:
/**
* Wrapper for Propel
* @author Sam Hauglustaine
*/
class strictlyPHP_Propel extends Propel
{
/**
* Set Propel log (required)
* @param mixed $log
* @return void
*/
public static function setLog($log)
{
if($log instanceof Zend_Log)
$log = new strictlyPHP_Propel_Log($log);
parent::setLogger($log);
}
}
That’s all. You can then call your Propel wrapper’s static methods in your bootstrap or a front controller plugin (I implemented that last option for a brand new project, a directory for stallion owners):
strictlyPHP_Propel::setLog($myLog); strictlyPHP_Propel::init($myConfig);
Of course the bigger part is using the Propel class generator itself but that’s something for another post.
Tags: propel, zend framework
