* Log destinations may be one of the following:
* - false to completely remove from the output, including from $wgDebugLogFile.
* - string values specifying a filename or URI.
- * - associative array mapping 'destination' key to the desired filename or URI.
- * The associative array may also contain a 'sample' key with an integer value,
- * specifying a sampling factor.
+ * - associative array with keys:
+ * - 'destination' desired filename or URI.
+ * - 'sample' an integer value, specifying a sampling factor (optional)
+ * - 'level' A \Psr\Log\LogLevel constant, indicating the minimum level
+ * to log (optional, since 1.25)
*
* @par Example:
* @code
* $wgDebugLogGroups['memcached'] = array(
* 'destination' => '/var/log/mediawiki/memcached.log',
* 'sample' => 1000, // log 1 message out of every 1,000.
+ * 'level' => \Psr\Log\LogLevel::WARNING
* );
* @endcode
*/
* @author Bryan Davis <bd808@wikimedia.org>
* @copyright © 2014 Bryan Davis and Wikimedia Foundation.
*/
-class MWLoggerLegacyLogger extends \Psr\Log\AbstractLogger {
+use Psr\Log\AbstractLogger;
+use Psr\Log\LogLevel;
+
+class MWLoggerLegacyLogger extends AbstractLogger {
/**
* @var string $channel
*/
protected $channel;
+ /**
+ * Convert Psr\Log\LogLevel constants into int for sane comparisons
+ * These are the same values that Monlog uses
+ *
+ * @var array
+ */
+ protected static $levelMapping = array(
+ LogLevel::DEBUG => 100,
+ LogLevel::INFO => 200,
+ LogLevel::NOTICE => 250,
+ LogLevel::WARNING => 300,
+ LogLevel::ERROR => 400,
+ LogLevel::CRITICAL => 500,
+ LogLevel::ALERT => 550,
+ LogLevel::EMERGENCY => 600,
+ );
+
/**
* @param string $channel
* @param array $context
*/
public function log( $level, $message, array $context = array() ) {
- if ( self::shouldEmit( $this->channel, $message, $context ) ) {
+ if ( self::shouldEmit( $this->channel, $message, $level, $context ) ) {
$text = self::format( $this->channel, $message, $context );
$destination = self::destination( $this->channel, $message, $context );
self::emit( $text, $destination );
*
* @param string $channel
* @param string $message
+ * @param string|int $level Psr\Log\LogEvent constant or Monlog level int
* @param array $context
* @return bool True if message should be sent to disk/network, false
* otherwise
*/
- public static function shouldEmit( $channel, $message, $context ) {
+ public static function shouldEmit( $channel, $message, $level, $context ) {
global $wgDebugLogFile, $wgDBerrorLog, $wgDebugLogGroups;
if ( $channel === 'wfLogDBError' ) {
} elseif ( isset( $wgDebugLogGroups[$channel] ) ) {
$logConfig = $wgDebugLogGroups[$channel];
- if ( is_array( $logConfig ) && isset( $logConfig['sample'] ) ) {
- // Emit randomly with a 1 in 'sample' chance for each message.
- $shouldEmit = mt_rand( 1, $logConfig['sample'] ) === 1;
-
+ if ( is_array( $logConfig ) ) {
+ $shouldEmit = true;
+ if ( isset( $logConfig['sample'] ) ) {
+ // Emit randomly with a 1 in 'sample' chance for each message.
+ $shouldEmit = mt_rand( 1, $logConfig['sample'] ) === 1;
+ }
+
+ if ( isset( $logConfig['level'] ) ) {
+ if ( is_string( $level ) ) {
+ $level = self::$levelMapping[$level];
+ }
+ $shouldEmit = $level >= self::$levelMapping[$logConfig['level']];
+ }
} else {
// Emit unless the config value is explictly false.
$shouldEmit = $logConfig !== false;
*
* @file
*/
+use Psr\Log\LogLevel;
class MWLoggerLegacyLoggerTest extends MediaWikiTestCase {
);
}
+ /**
+ * @covers MWLoggerLegacyLogger::shouldEmit
+ * @dataProvider provideShouldEmit
+ */
+ public function testShouldEmit( $level, $config, $expected ) {
+ $this->setMwGlobals( 'wgDebugLogGroups', array( 'fakechannel' => $config ) );
+ $this->assertEquals(
+ $expected,
+ MWLoggerLegacyLogger::shouldEmit( 'fakechannel', 'some message', $level, array() )
+ );
+ }
+
+ public static function provideShouldEmit() {
+ $dest = array( 'destination' => 'foobar' );
+ $tests = array(
+ array(
+ LogLevel::DEBUG,
+ $dest,
+ true
+ ),
+ array(
+ LogLevel::WARNING,
+ $dest + array( 'level' => LogLevel::INFO ),
+ true,
+ ),
+ array(
+ LogLevel::INFO,
+ $dest + array( 'level' => LogLevel::CRITICAL ),
+ false,
+ ),
+ );
+
+ if ( class_exists( '\Monolog\Logger' ) ) {
+ $tests[] = array(
+ \Monolog\Logger::INFO,
+ $dest + array( 'level' => LogLevel::INFO ),
+ true,
+ );
+ $tests[] = array(
+ \Monolog\Logger::WARNING,
+ $dest + array( 'level' => LogLevel::EMERGENCY ),
+ false,
+ );
+ }
+
+ return $tests;
+ }
+
}