Skip to content
Permalink
Browse files

Log::write() now accepts a scope key in last argument.

If a numerically indexed array is passed, it will understood as the
scope key.
  • Loading branch information...
lorenzo committed Sep 20, 2014
1 parent 21fea47 commit 499512b7bcd73fda3ae2ce6aee04915e2940c561
Showing with 16 additions and 9 deletions.
  1. +14 −7 src/Log/Log.php
  2. +2 −2 tests/TestCase/Log/LogTest.php
@@ -298,7 +298,7 @@ public static function engine($name) {
* When writing a log message you can define one or many scopes for the message.
* This allows you to handle messages differently based on application section/feature.
*
* `Log::write('warning', 'Payment failed', 'payment');`
* `Log::write('warning', 'Payment failed', ['scope' => 'payment']);`
*
* When configuring loggers you can configure the scopes a particular logger will handle.
* When using scopes, you must ensure that the level of the message, and the scope of the message
@@ -313,12 +313,15 @@ public static function engine($name) {
* @param int|string $level The severity level of the message being written.
* The value must be an integer or string matching a known level.
* @param mixed $message Message content to log
* @param string|array $scope The scope(s) a log message is being created in.
* See Cake\Log\Log::config() for more information on logging scopes.
* @param string|array $context Additioanl data to be used for logging the message.
* The special `scope` key can be passed to be used for further filtering of the
* log engines to be used. If a string or a numerically index array is passed, it
* will be treated as the `scope` key.
* See Cake\Log\Log::config() for more information on logging scopes.
* @return bool Success
* @throws \InvalidArgumentException If invalid level is passed.
*/
public static function write($level, $message, $scope = array()) {
public static function write($level, $message, $context = []) {
static::_init();
if (is_int($level) && isset(static::$_levels[$level])) {
$level = static::$_levels[$level];
@@ -329,7 +332,11 @@ public static function write($level, $message, $scope = array()) {
}
$logged = false;
$scope = (array)$scope;
$context = (array)$context;
if (isset($context[0])) {
$context = ['scope' => $context];
}
$context += ['scope' => []];
foreach (static::$_registry->loaded() as $streamName) {
$logger = static::$_registry->{$streamName};
@@ -341,10 +348,10 @@ public static function write($level, $message, $scope = array()) {
}
$correctLevel = empty($levels) || in_array($level, $levels);
$inScope = empty($scopes) || array_intersect($scope, $scopes);
$inScope = empty($scopes) || array_intersect($context['scope'], $scopes);
if ($correctLevel && $inScope) {
$logger->log($level, $message, $scope);
$logger->log($level, $message, $context);
$logged = true;
}
}
@@ -440,10 +440,10 @@ public function testPassingScopeToEngine() {
$this->assertNull($engine->passedScope);
Log::write('debug', 'test message', 'foo');
$this->assertEquals(['foo'], $engine->passedScope);
$this->assertEquals(['scope' => ['foo']], $engine->passedScope);
Log::write('debug', 'test message', ['foo', 'bar']);
$this->assertEquals(['foo', 'bar'], $engine->passedScope);
$this->assertEquals(['scope' => ['foo', 'bar']], $engine->passedScope);
$result = Log::write('debug', 'test message');
$this->assertFalse($result);

0 comments on commit 499512b

Please sign in to comment.
You can’t perform that action at this time.