Created
August 26, 2011 08:22
-
-
Save pepe84/1172973 to your computer and use it in GitHub Desktop.
PHP Zend: Multilog resource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'Zend/Application/Resource/ResourceAbstract.php'; | |
require_once 'Zend/Log.php'; | |
/** | |
* Multilog Resource | |
* | |
* @category Zend | |
* @package Zend_Application | |
* @subpackage Resource | |
* @author Rafael de Oleza Alomar <roa@tid.es> | |
* @author Jose Luis Jiménez <jljc.work@gmail.com> | |
*/ | |
class App_Zend_Application_Resource_Multilog | |
extends Zend_Application_Resource_ResourceAbstract | |
{ | |
/** | |
* Associative array containing all configured logs | |
* | |
* @var array | |
*/ | |
protected $_logs = null; | |
/** | |
* Initialize the Logs (instances of Zend_Log) | |
* | |
* @return App_Zend_Application_Resource_Multilog | |
*/ | |
public function init() | |
{ | |
$this->getLogs(); | |
return $this; | |
} | |
/** | |
* Fetch all available logs | |
* | |
* @return array | |
*/ | |
public function getLogs() | |
{ | |
if ($this->_logs === null) { | |
// Load logs | |
$options = $this->getOptions(); | |
$this->_logs = array(); | |
foreach ($options as $name => $params) { | |
/** | |
* @todo Talk with Rafeca about writerN issue | |
*/ | |
// Detect if we are only giving one writer | |
if (0 === strpos(key($params), 'writerN')) { | |
$params = array($name => $params); | |
} | |
$this->_logs[$name] = Zend_Log::factory($params); | |
} | |
} | |
return $this->_logs; | |
} | |
/** | |
* Has specific log? | |
* | |
* @param string $name | |
* @return boolean | |
*/ | |
public function hasLog($name) | |
{ | |
return isset($this->_logs[$name]); | |
} | |
/** | |
* Fetch log by name | |
* | |
* @param string $name | |
* @return Zend_Log | |
*/ | |
public function getLog($name) | |
{ | |
if (!$this->hasLog($name)) { | |
throw new Zend_Application_Resource_Exception( | |
"Log '$name' was not configured" | |
); | |
} | |
return $this->_logs[$name]; | |
} | |
/** | |
* | |
* @param string $name | |
* @return boolean | |
*/ | |
public function __isset($name) | |
{ | |
return $this->hasLog($name); | |
} | |
/** | |
* | |
* @param string $name | |
* @return Zend_Log | |
*/ | |
public function __get($name) | |
{ | |
return $this->getLog($name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment