From f210ce84cd9604af38048265a22a4200cc4db251 Mon Sep 17 00:00:00 2001 From: Bryan Davis Date: Thu, 20 Nov 2014 23:23:49 -0700 Subject: [PATCH] Allow limiting Monolog output using legacy settings Add $useLegacyFilter option to MWLoggerMonologHandler constructor that will use MWLoggerLegacyLogger::shouldEmit to decide if a given log message should be emitted. Bug: T845 Change-Id: If311308faad35348fdc7e85155a1bc16bbf75c85 --- includes/debug/logger/legacy/Logger.php | 10 +++++----- includes/debug/logger/monolog/Handler.php | 22 +++++++++++++++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/includes/debug/logger/legacy/Logger.php b/includes/debug/logger/legacy/Logger.php index 8c0495aa14..c67bd7b949 100644 --- a/includes/debug/logger/legacy/Logger.php +++ b/includes/debug/logger/legacy/Logger.php @@ -77,7 +77,7 @@ class MWLoggerLegacyLogger extends \Psr\Log\AbstractLogger { * @return bool True if message should be sent to disk/network, false * otherwise */ - protected static function shouldEmit( $channel, $message, $context ) { + public static function shouldEmit( $channel, $message, $context ) { global $wgDebugLogFile, $wgDBerrorLog, $wgDebugLogGroups; if ( $channel === 'wfLogDBError' ) { @@ -102,10 +102,10 @@ class MWLoggerLegacyLogger extends \Psr\Log\AbstractLogger { } } elseif ( isset( $context['private'] ) && $context['private'] ) { - // Don't emit if the message didn't match previous checks based on the - // channel and the event is marked as private. This check discards - // messages sent via wfDebugLog() with dest == 'private' and no explicit - // wgDebugLogGroups configuration. + // Don't emit if the message didn't match previous checks based on + // the channel and the event is marked as private. This check + // discards messages sent via wfDebugLog() with dest == 'private' + // and no explicit wgDebugLogGroups configuration. $shouldEmit = false; } else { // Default return value is the the same as the historic wfDebug diff --git a/includes/debug/logger/monolog/Handler.php b/includes/debug/logger/monolog/Handler.php index 02ab309891..b2e3012013 100644 --- a/includes/debug/logger/monolog/Handler.php +++ b/includes/debug/logger/monolog/Handler.php @@ -48,6 +48,12 @@ class MWLoggerMonologHandler extends \Monolog\Handler\AbstractProcessingHandler */ protected $uri; + /** + * Filter log events using legacy rules + * @var bool $useLegacyFilter + */ + protected $useLegacyFilter; + /** * Log sink * @var resource $sink @@ -77,16 +83,30 @@ class MWLoggerMonologHandler extends \Monolog\Handler\AbstractProcessingHandler /** * @param string $stream Stream URI + * @param bool $useLegacyFilter Filter log events using legacy rules * @param int $level Minimum logging level that will trigger handler * @param bool $bubble Can handled meesages bubble up the handler stack? */ public function __construct( - $stream, $level = \Monolog\Logger::DEBUG, $bubble = true + $stream, + $useLegacyFilter = false, + $level = \Monolog\Logger::DEBUG, + $bubble = true ) { parent::__construct( $level, $bubble ); $this->uri = $stream; + $this->useLegacyFilter = $useLegacyFilter; } + public function isHandling( array $record ) { + $levelOk = parent::isHandling( $record ); + if ( $levelOk && $this->useLegacyFilter ) { + return MWLoggerLegacyLogger::shouldEmit( + $record['channel'], $record['message'], $record + ); + } + return $levelOk; + } /** * Open the log sink described by our stream URI. -- 2.20.1