Skip to content

Commit 9778b62

Browse files
committed
Convert Log to use new Registry object.
Update the registry and Log to use the new Registry base class.
1 parent edce895 commit 9778b62

File tree

2 files changed

+76
-47
lines changed

2 files changed

+76
-47
lines changed

lib/Cake/Log/Log.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,11 @@ protected static function _init() {
174174
*/
175175
protected static function _loadConfig() {
176176
$loggers = Configure::read('Log');
177-
foreach ((array)$loggers as $key => $config) {
178-
static::$_registry->load($key, $config);
177+
foreach ((array)$loggers as $name => $properties) {
178+
if (isset($properties['engine'])) {
179+
$properties['className'] = $properties['engine'];
180+
}
181+
static::$_registry->load($name, $properties);
179182
}
180183
}
181184

@@ -285,7 +288,7 @@ public static function disable($streamName) {
285288
public static function engine($name, $engine = null) {
286289
static::_init();
287290
if ($engine) {
288-
static::$_registry->load($name, $engine);
291+
static::$_registry->add($name, $engine);
289292
return;
290293
}
291294
if (static::$_registry->{$name}) {
@@ -346,7 +349,7 @@ public static function write($level, $message, $scope = array()) {
346349
$level = static::$_levels[$level];
347350
}
348351
$logged = false;
349-
foreach (static::$_registry->enabled() as $streamName) {
352+
foreach (static::$_registry->loaded() as $streamName) {
350353
$logger = static::$_registry->{$streamName};
351354
$levels = $scopes = null;
352355
if ($logger instanceof BaseLog) {

lib/Cake/Log/LogEngineRegistry.php

Lines changed: 69 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,64 +17,90 @@
1717
use Cake\Core\App;
1818
use Cake\Error;
1919
use Cake\Log\LogInterface;
20-
use Cake\Utility\ObjectCollection;
20+
use Cake\Utility\ObjectRegistry;
2121

2222
/**
2323
* Registry of loaded log engines
24-
*
25-
* @package Cake.Log
2624
*/
27-
class LogEngineRegistry extends ObjectCollection {
25+
class LogEngineRegistry extends ObjectRegistry {
26+
2827

2928
/**
30-
* Loads/constructs a Log engine.
29+
* Resolve a logger classname.
3130
*
32-
* @param string $name instance identifier
33-
* @param LogInterface|array $options Setting for the Log Engine, or the log engine
34-
* If a log engine is used, the adapter will be enabled.
35-
* @return BaseLog BaseLog engine instance
36-
* @throws Cake\Error\Exception when logger class does not implement a write method
31+
* Part of the template method for Cake\Utility\ObjectRegistry::load()
32+
*
33+
* @param string $class Partial classname to resolve.
34+
* @return string|false Either the correct classname or false.
3735
*/
38-
public function load($name, $options = array()) {
39-
$logger = false;
40-
if ($options instanceof LogInterface) {
41-
$enable = true;
42-
$logger = $options;
43-
$options = null;
44-
}
45-
if (is_array($options)) {
46-
$enable = isset($options['enabled']) ? $options['enabled'] : true;
47-
$logger = $this->_getLogger($options);
48-
}
49-
if (!$logger instanceof LogInterface) {
50-
throw new Error\Exception(sprintf(
51-
__d('cake_dev', 'logger class %s is not an instance of Cake\Log\LogInterface.'), $name
52-
));
53-
}
54-
$this->_loaded[$name] = $logger;
55-
if ($enable) {
56-
$this->enable($name);
57-
}
58-
return $logger;
36+
protected function _resolveClassName($class) {
37+
return App::classname($class, 'Log/Engine', 'Log');
5938
}
6039

6140
/**
62-
* Attempts to import a logger class from the various paths it could be on.
63-
* Checks that the logger class implements a write method as well.
41+
* Throws an exception when a logger is missing.
6442
*
65-
* @param array $options The configuration options to load a logger with.
66-
* @return false|LogInterface boolean false on any failures, log adapter interface on success
43+
* Part of the template method for Cake\Utility\ObjectRegistry::load()
44+
*
45+
* @param string $class The classname that is missing.
46+
* @param string $plugin The plugin the logger is missing in.
6747
* @throws Cake\Error\Exception
6848
*/
69-
protected static function _getLogger($options) {
70-
$name = isset($options['engine']) ? $options['engine'] : null;
71-
unset($options['engine']);
72-
$class = App::classname($name, 'Log/Engine', 'Log');
73-
if (!$class) {
74-
throw new Error\Exception(__d('cake_dev', 'Could not load class %s', $name));
49+
protected function _throwMissingClassError($class, $plugin) {
50+
throw new Error\Exception(__d('cake_dev', 'Could not load class %s', $class));
51+
}
52+
53+
/**
54+
* Create the logger instance.
55+
*
56+
* Part of the template method for Cake\Utility\ObjectRegistry::load()
57+
* @param string $class The classname that is missing.
58+
* @param array $settings An array of settings to use for the logger.
59+
* @return LogEngine The constructed logger class.
60+
*/
61+
protected function _create($class, $settings) {
62+
$instance = new $class($settings);
63+
$this->_checkInstance($instance);
64+
return $instance;
65+
}
66+
67+
/**
68+
* Check an instance to see if it implements the LogInterface.
69+
*
70+
* @param mixed $instance The instance to check.
71+
* @return void
72+
* @throws Cake\Error\Exception when an object doesn't implement
73+
* the correct interface.
74+
*/
75+
protected function _checkInstance($instance) {
76+
if ($instance instanceof LogInterface) {
77+
return;
7578
}
76-
$logger = new $class($options);
77-
return $logger;
79+
throw new Error\Exception(__d(
80+
'cake_dev',
81+
'Loggers must implement Cake\Log\LogInterface.'
82+
));
83+
}
84+
85+
/**
86+
* Add a logger into a given name.
87+
*
88+
* @param string $name The name of the logger.
89+
* @param Cake\Log\LogInterface $instance A logger implementing LogInterface.
90+
*/
91+
public function add($name, $instance) {
92+
$this->_checkInstance($instance);
93+
$this->_loaded[$name] = $instance;
94+
}
95+
96+
/**
97+
* Remove a single logger from the registry.
98+
*
99+
* @param string $name The logger name.
100+
* @return void
101+
*/
102+
public function unload($name) {
103+
unset($this->_loaded[$name]);
78104
}
79105

80106
}

0 commit comments

Comments
 (0)