Skip to content

Commit

Permalink
Convert Log to use new Registry object.
Browse files Browse the repository at this point in the history
Update the registry and Log to use the new Registry base class.
  • Loading branch information
markstory committed Aug 13, 2013
1 parent edce895 commit 9778b62
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 47 deletions.
11 changes: 7 additions & 4 deletions lib/Cake/Log/Log.php
Expand Up @@ -174,8 +174,11 @@ protected static function _init() {
*/
protected static function _loadConfig() {
$loggers = Configure::read('Log');
foreach ((array)$loggers as $key => $config) {
static::$_registry->load($key, $config);
foreach ((array)$loggers as $name => $properties) {
if (isset($properties['engine'])) {
$properties['className'] = $properties['engine'];
}
static::$_registry->load($name, $properties);
}
}

Expand Down Expand Up @@ -285,7 +288,7 @@ public static function disable($streamName) {
public static function engine($name, $engine = null) {
static::_init();
if ($engine) {
static::$_registry->load($name, $engine);
static::$_registry->add($name, $engine);
return;
}
if (static::$_registry->{$name}) {
Expand Down Expand Up @@ -346,7 +349,7 @@ public static function write($level, $message, $scope = array()) {
$level = static::$_levels[$level];
}
$logged = false;
foreach (static::$_registry->enabled() as $streamName) {
foreach (static::$_registry->loaded() as $streamName) {
$logger = static::$_registry->{$streamName};
$levels = $scopes = null;
if ($logger instanceof BaseLog) {
Expand Down
112 changes: 69 additions & 43 deletions lib/Cake/Log/LogEngineRegistry.php
Expand Up @@ -17,64 +17,90 @@
use Cake\Core\App;
use Cake\Error;
use Cake\Log\LogInterface;
use Cake\Utility\ObjectCollection;
use Cake\Utility\ObjectRegistry;

/**
* Registry of loaded log engines
*
* @package Cake.Log
*/
class LogEngineRegistry extends ObjectCollection {
class LogEngineRegistry extends ObjectRegistry {


/**
* Loads/constructs a Log engine.
* Resolve a logger classname.
*
* @param string $name instance identifier
* @param LogInterface|array $options Setting for the Log Engine, or the log engine
* If a log engine is used, the adapter will be enabled.
* @return BaseLog BaseLog engine instance
* @throws Cake\Error\Exception when logger class does not implement a write method
* Part of the template method for Cake\Utility\ObjectRegistry::load()
*
* @param string $class Partial classname to resolve.
* @return string|false Either the correct classname or false.
*/
public function load($name, $options = array()) {
$logger = false;
if ($options instanceof LogInterface) {
$enable = true;
$logger = $options;
$options = null;
}
if (is_array($options)) {
$enable = isset($options['enabled']) ? $options['enabled'] : true;
$logger = $this->_getLogger($options);
}
if (!$logger instanceof LogInterface) {
throw new Error\Exception(sprintf(
__d('cake_dev', 'logger class %s is not an instance of Cake\Log\LogInterface.'), $name
));
}
$this->_loaded[$name] = $logger;
if ($enable) {
$this->enable($name);
}
return $logger;
protected function _resolveClassName($class) {
return App::classname($class, 'Log/Engine', 'Log');
}

/**
* Attempts to import a logger class from the various paths it could be on.
* Checks that the logger class implements a write method as well.
* Throws an exception when a logger is missing.
*
* @param array $options The configuration options to load a logger with.
* @return false|LogInterface boolean false on any failures, log adapter interface on success
* Part of the template method for Cake\Utility\ObjectRegistry::load()
*
* @param string $class The classname that is missing.
* @param string $plugin The plugin the logger is missing in.
* @throws Cake\Error\Exception
*/
protected static function _getLogger($options) {
$name = isset($options['engine']) ? $options['engine'] : null;
unset($options['engine']);
$class = App::classname($name, 'Log/Engine', 'Log');
if (!$class) {
throw new Error\Exception(__d('cake_dev', 'Could not load class %s', $name));
protected function _throwMissingClassError($class, $plugin) {
throw new Error\Exception(__d('cake_dev', 'Could not load class %s', $class));
}

/**
* Create the logger instance.
*
* Part of the template method for Cake\Utility\ObjectRegistry::load()
* @param string $class The classname that is missing.
* @param array $settings An array of settings to use for the logger.
* @return LogEngine The constructed logger class.
*/
protected function _create($class, $settings) {
$instance = new $class($settings);
$this->_checkInstance($instance);
return $instance;
}

/**
* Check an instance to see if it implements the LogInterface.
*
* @param mixed $instance The instance to check.
* @return void
* @throws Cake\Error\Exception when an object doesn't implement
* the correct interface.
*/
protected function _checkInstance($instance) {
if ($instance instanceof LogInterface) {
return;
}
$logger = new $class($options);
return $logger;
throw new Error\Exception(__d(
'cake_dev',
'Loggers must implement Cake\Log\LogInterface.'
));
}

/**
* Add a logger into a given name.
*
* @param string $name The name of the logger.
* @param Cake\Log\LogInterface $instance A logger implementing LogInterface.
*/
public function add($name, $instance) {
$this->_checkInstance($instance);
$this->_loaded[$name] = $instance;
}

/**
* Remove a single logger from the registry.
*
* @param string $name The logger name.
* @return void
*/
public function unload($name) {
unset($this->_loaded[$name]);
}

}

0 comments on commit 9778b62

Please sign in to comment.