Skip to content

Commit

Permalink
Starting work to make Cake PSR-3 compliant and accept monolog as an
Browse files Browse the repository at this point in the history
alternate logger without much pain
  • Loading branch information
lorenzo committed Sep 19, 2014
1 parent a183391 commit ff1161d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -24,7 +24,8 @@
"ext-mbstring": "*",
"nesbot/Carbon": "1.8.*",
"ircmaxell/password-compat": "1.0.*",
"aura/intl": "1.1.*"
"aura/intl": "1.1.*",
"psr/log": "1.0"
},
"require-dev": {
"phpunit/phpunit": "*"
Expand Down
4 changes: 2 additions & 2 deletions src/Log/Engine/BaseLog.php
Expand Up @@ -15,13 +15,13 @@
namespace Cake\Log\Engine;

use Cake\Core\InstanceConfigTrait;
use Cake\Log\LogInterface;
use Psr\Log\AbstractLogger;

/**
* Base log engine class.
*
*/
abstract class BaseLog implements LogInterface {
abstract class BaseLog extends AbstractLogger {

use InstanceConfigTrait;

Expand Down
22 changes: 11 additions & 11 deletions src/Log/Log.php
Expand Up @@ -36,7 +36,7 @@
* classname to use loggers in the `App\Log\Engine` & `Cake\Log\Engine` namespaces.
* You can also use plugin short hand to use logging classes provided by plugins.
*
* Log adapters are required to implement `Cake\Log\LogInterface`, and there is a
* Log adapters are required to implement `Psr\Log\LoggerInterface`, and there is a
* built-in base class (`Cake\Log\Engine\BaseLog`) that can be used for custom loggers.
*
* Outside of the `className` key, all other configuration values will be passed to the
Expand Down Expand Up @@ -308,7 +308,7 @@ public static function engine($name) {
*
* If no configured logger can handle a log message (because of level or scope restrictions)
* then the logged message will be ignored and silently dropped. You can check if this has happened
* by inspecting the return of write(). If false the message was not handled.
* by inspecting the return of write(). If false the message was not handled.
*
* @param int|string $level The severity level of the message being written.
* The value must be an integer or string matching a known level.
Expand All @@ -329,26 +329,26 @@ public static function write($level, $message, $scope = array()) {
}

$logged = false;
$scope = (array)$scope;

foreach (static::$_registry->loaded() as $streamName) {
$logger = static::$_registry->{$streamName};
$levels = $scopes = null;

if ($logger instanceof BaseLog) {
$levels = $logger->levels();
$scopes = $logger->scopes();
}
$correctLevel = (
empty($levels) ||
in_array($level, $levels)
);
$inScope = (
empty($scopes) ||
count(array_intersect((array)$scope, $scopes)) > 0
);

$correctLevel = empty($levels) || in_array($level, $levels);
$inScope = empty($scopes) || array_intersect($scope, $scopes);

if ($correctLevel && $inScope) {
$logger->write($level, $message, $scope);
$logger->log($level, $message, $scope);
$logged = true;
}
}

return $logged;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Log/LogEngineRegistry.php
Expand Up @@ -16,8 +16,8 @@

use Cake\Core\App;
use Cake\Core\ObjectRegistry;
use Cake\Log\LogInterface;
use \RuntimeException;
use Psr\Log\LoggerInterface;
use RuntimeException;

/**
* Registry of loaded log engines
Expand Down Expand Up @@ -74,12 +74,12 @@ protected function _create($class, $alias, $settings) {
$instance = new $class($settings);
}

if ($instance instanceof LogInterface) {
if ($instance instanceof LoggerInterface) {
return $instance;
}

throw new RuntimeException(
'Loggers must implement Cake\Log\LogInterface.'
'Loggers must implement Psr\Log\LoggerInterface.'
);
}

Expand Down
8 changes: 5 additions & 3 deletions src/Log/LogInterface.php
Expand Up @@ -16,19 +16,21 @@
*/
namespace Cake\Log;

use Psr\Log\LoggerInterface;

/**
* LogStreamInterface is the interface that should be implemented
* LogInterface is the interface that should be implemented
* by all classes that are going to be used as Log streams.
*/
interface LogInterface {
interface LogInterface extends LoggerInterface {

/**
* Write method to handle writes being made to the Logger
*
* @param string $level The severity level of the message being written.
* See Cake\Log\Log::$_levels for list of possible levels.
* @param string $message Message content to log
* @param string|array $scope The scope(s) a log message is being created in.
* @param array $scope The scope(s) a log message is being created in.
* See Cake\Log\Log::config() for more information on logging scopes.
* @return void
*/
Expand Down

0 comments on commit ff1161d

Please sign in to comment.