Browse Source

Fixes #14273: yii\log\Target::$enabled now supports callable value (#14539)

* Fixes #14273: yii\log\Target:: now supports callable value

* added empty line in changelog [skip ci]

* getter and setter

* Update Target.php
tags/2.0.13
Dmitry Dorogin 7 years ago committed by Carsten Brandt
parent
commit
9b01ca275f
  1. 1
      framework/CHANGELOG.md
  2. 31
      framework/log/Target.php
  3. 17
      tests/framework/log/TargetTest.php

1
framework/CHANGELOG.md

@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.13 under development
------------------------
- Enh #14273: `yii\log\Target::$enabled` now supports callable value (dmirogin)
- Bug #14723: Fixed serialization of `yii\db\Connection` instance closes database connection (klimov-paul)
- Bug #14697: Fixed `console\widgets\Table` rendering when there's no data supplied (bscheshirwork)
- Enh #13853: Added `yii\db\Migration::$compact` as well as `yii\console\controllers\BaseMigrateController::$compact` to allow making the migration console output more compact (francislavoie)

31
framework/log/Target.php

@ -28,6 +28,7 @@ use yii\web\Request;
* @property int $levels The message levels that this target is interested in. This is a bitmap of level
* values. Defaults to 0, meaning all available levels. Note that the type of this property differs in getter
* and setter. See [[getLevels()]] and [[setLevels()]] for details.
* @property bool $enabled Whether to enable this log target. Defaults to true.
*
* For more details and usage information on Target, see the [guide article on logging & targets](guide:runtime-logging).
*
@ -37,10 +38,6 @@ use yii\web\Request;
abstract class Target extends Component
{
/**
* @var bool whether to enable this log target. Defaults to true.
*/
public $enabled = true;
/**
* @var array list of message categories that this target is interested in. Defaults to empty, meaning all categories.
* You can use an asterisk at the end of a category so that the category may be used to
* match those categories sharing the same common prefix. For example, 'yii\db\*' will match
@ -94,7 +91,7 @@ abstract class Target extends Component
public $messages = [];
private $_levels = 0;
private $_enabled = true;
/**
* Exports log [[messages]] to a specific destination.
@ -311,4 +308,28 @@ abstract class Target extends Component
return "[$ip][$userID][$sessionID]";
}
/**
* Sets a value indicating whether this log target is enabled.
* @param bool|callable $value a boolean value or a callable to obtain the value from.
* The callable value is available since version 2.0.13.
*/
public function setEnabled($value)
{
$this->_enabled = $value;
}
/**
* Check whether the log target is enabled.
* @property Indicates whether this log target is enabled. Defaults to true.
* @return bool A value indicating whether this log target is enabled.
*/
public function getEnabled()
{
if (is_callable($this->_enabled)) {
return call_user_func($this->_enabled, $this);
}
return $this->_enabled;
}
}

17
tests/framework/log/TargetTest.php

@ -165,6 +165,23 @@ class TargetTest extends TestCase
$this->expectExceptionMessage('Incorrect 128 value');
$target->setLevels(128);
}
public function testGetEnabled()
{
/** @var Target $target */
$target = $this->getMockForAbstractClass('yii\\log\\Target');
$target->enabled = true;
$this->assertTrue($target->enabled);
$target->enabled = false;
$this->assertFalse($target->enabled);
$target->enabled = function ($target) {
return empty($target->messages);
};
$this->assertTrue($target->enabled);
}
}
class TestTarget extends Target

Loading…
Cancel
Save