Browse Source

Starting work to make Cake PSR-3 compliant and accept monolog as an
alternate logger without much pain

Jose Lorenzo Rodriguez 11 years ago
parent
commit
ff1161d8c8
5 changed files with 24 additions and 21 deletions
  1. 2 1
      composer.json
  2. 2 2
      src/Log/Engine/BaseLog.php
  3. 11 11
      src/Log/Log.php
  4. 4 4
      src/Log/LogEngineRegistry.php
  5. 5 3
      src/Log/LogInterface.php

+ 2 - 1
composer.json

@@ -24,7 +24,8 @@
 		"ext-mbstring": "*",
 		"ext-mbstring": "*",
 		"nesbot/Carbon": "1.8.*",
 		"nesbot/Carbon": "1.8.*",
 		"ircmaxell/password-compat": "1.0.*",
 		"ircmaxell/password-compat": "1.0.*",
-		"aura/intl": "1.1.*"
+		"aura/intl": "1.1.*",
+		"psr/log": "1.0"
 	},
 	},
 	"require-dev": {
 	"require-dev": {
 		"phpunit/phpunit": "*"
 		"phpunit/phpunit": "*"

+ 2 - 2
src/Log/Engine/BaseLog.php

@@ -15,13 +15,13 @@
 namespace Cake\Log\Engine;
 namespace Cake\Log\Engine;
 
 
 use Cake\Core\InstanceConfigTrait;
 use Cake\Core\InstanceConfigTrait;
-use Cake\Log\LogInterface;
+use Psr\Log\AbstractLogger;
 
 
 /**
 /**
  * Base log engine class.
  * Base log engine class.
  *
  *
  */
  */
-abstract class BaseLog implements LogInterface {
+abstract class BaseLog extends AbstractLogger {
 
 
 	use InstanceConfigTrait;
 	use InstanceConfigTrait;
 
 

+ 11 - 11
src/Log/Log.php

@@ -36,7 +36,7 @@ use InvalidArgumentException;
  * classname to use loggers in the `App\Log\Engine` & `Cake\Log\Engine` namespaces.
  * 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.
  * 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.
  * 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
  * Outside of the `className` key, all other configuration values will be passed to the
@@ -308,7 +308,7 @@ class Log {
  *
  *
  * If no configured logger can handle a log message (because of level or scope restrictions)
  * 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
  * 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.
  * @param int|string $level The severity level of the message being written.
  *    The value must be an integer or string matching a known level.
  *    The value must be an integer or string matching a known level.
@@ -329,26 +329,26 @@ class Log {
 		}
 		}
 
 
 		$logged = false;
 		$logged = false;
+		$scope = (array)$scope;
+
 		foreach (static::$_registry->loaded() as $streamName) {
 		foreach (static::$_registry->loaded() as $streamName) {
 			$logger = static::$_registry->{$streamName};
 			$logger = static::$_registry->{$streamName};
 			$levels = $scopes = null;
 			$levels = $scopes = null;
+
 			if ($logger instanceof BaseLog) {
 			if ($logger instanceof BaseLog) {
 				$levels = $logger->levels();
 				$levels = $logger->levels();
 				$scopes = $logger->scopes();
 				$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) {
 			if ($correctLevel && $inScope) {
-				$logger->write($level, $message, $scope);
+				$logger->log($level, $message, $scope);
 				$logged = true;
 				$logged = true;
 			}
 			}
 		}
 		}
+
 		return $logged;
 		return $logged;
 	}
 	}
 
 

+ 4 - 4
src/Log/LogEngineRegistry.php

@@ -16,8 +16,8 @@ namespace Cake\Log;
 
 
 use Cake\Core\App;
 use Cake\Core\App;
 use Cake\Core\ObjectRegistry;
 use Cake\Core\ObjectRegistry;
-use Cake\Log\LogInterface;
-use \RuntimeException;
+use Psr\Log\LoggerInterface;
+use RuntimeException;
 
 
 /**
 /**
  * Registry of loaded log engines
  * Registry of loaded log engines
@@ -74,12 +74,12 @@ class LogEngineRegistry extends ObjectRegistry {
 			$instance = new $class($settings);
 			$instance = new $class($settings);
 		}
 		}
 
 
-		if ($instance instanceof LogInterface) {
+		if ($instance instanceof LoggerInterface) {
 			return $instance;
 			return $instance;
 		}
 		}
 
 
 		throw new RuntimeException(
 		throw new RuntimeException(
-			'Loggers must implement Cake\Log\LogInterface.'
+			'Loggers must implement Psr\Log\LoggerInterface.'
 		);
 		);
 	}
 	}
 
 

+ 5 - 3
src/Log/LogInterface.php

@@ -16,11 +16,13 @@
  */
  */
 namespace Cake\Log;
 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.
  * 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
  * Write method to handle writes being made to the Logger
@@ -28,7 +30,7 @@ interface LogInterface {
  * @param string $level The severity level of the message being written.
  * @param string $level The severity level of the message being written.
  *    See Cake\Log\Log::$_levels for list of possible levels.
  *    See Cake\Log\Log::$_levels for list of possible levels.
  * @param string $message Message content to log
  * @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.
  *    See Cake\Log\Log::config() for more information on logging scopes.
  * @return void
  * @return void
  */
  */