Skip to content

Instantly share code, notes, and snippets.

@rafeca
Forked from pepe84/Multilog.php
Created August 28, 2011 08:32
Show Gist options
  • Save rafeca/1176423 to your computer and use it in GitHub Desktop.
Save rafeca/1176423 to your computer and use it in GitHub Desktop.
PHP Zend: Multilog resource
<?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)
{
$logs = $this->getLogs();
return isset($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