Allow limiting Monolog output using legacy settings
authorBryan Davis <bd808@wikimedia.org>
Fri, 21 Nov 2014 06:23:49 +0000 (23:23 -0700)
committerBryan Davis <bd808@wikimedia.org>
Fri, 21 Nov 2014 06:30:42 +0000 (23:30 -0700)
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
includes/debug/logger/monolog/Handler.php

index 8c0495a..c67bd7b 100644 (file)
@@ -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
index 02ab309..b2e3012 100644 (file)
@@ -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.