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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
/**
* 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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/**
* 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):
|
1 2 |
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

