From 78f850664ac3cb8c3bbd5ecd2076ff04498ffa68 Mon Sep 17 00:00:00 2001 From: Andrew Kostka Date: Fri, 2 Feb 2024 10:24:39 +0000 Subject: [PATCH] Cleanup custom logging classes --- .../wbstack/src/Logging/CustomLogger.php | 31 +++++--------- .../wbstack/src/Logging/CustomSpi.php | 27 ++++-------- .../wbstack/src/Settings/LocalSettings.php | 42 +++++-------------- dist/wbstack/src/Logging/CustomLogger.php | 31 +++++--------- dist/wbstack/src/Logging/CustomSpi.php | 27 ++++-------- dist/wbstack/src/Settings/LocalSettings.php | 42 +++++-------------- 6 files changed, 60 insertions(+), 140 deletions(-) diff --git a/dist-persist/wbstack/src/Logging/CustomLogger.php b/dist-persist/wbstack/src/Logging/CustomLogger.php index 502e37a37..47e3e73c7 100644 --- a/dist-persist/wbstack/src/Logging/CustomLogger.php +++ b/dist-persist/wbstack/src/Logging/CustomLogger.php @@ -8,34 +8,25 @@ class CustomLogger extends AbstractLogger { private $channel; - private $config; + private $ignoreChannels; + private $ignoreLevels; - public function __construct($channel, $config ) { + public function __construct( $channel, array $ignoreChannels, array $ignoreLevels ) { $this->channel = $channel; - $this->config = $config; + $this->ignoreChannels = $ignoreChannels; + $this->ignoreLevels = $ignoreLevels; } public function log( $level, $message, array $context = [] ) { - if(in_array($this->channel, $this->config['logAllInGroup'])) { - $this->doLog( $level, $message, $context ); - return; - } - if(in_array($this->channel, $this->config['logAllInGroupExceptDebug']) && $level !== 'debug') { - $this->doLog( $level, $message, $context ); - return; - } + $ignore = in_array( $this->channel, $this->ignoreChannels ) + || in_array( $level, $this->ignoreLevels ); - if(in_array($this->channel, $this->config['ignoreAllInGroup'])) { - return; - } - if(in_array($level, $this->config['ignoreLevels'])) { - return; + if ( !$ignore ) { + $this->doLog( $level, $message, $context ); } - - $this->doLog($level, $message, $context); } - private function doLog( $level, $message, $context ) { + private function doLog( $level, $message, array $context = [] ) { $payload = false; if ( str_contains( $this->channel, 'json' ) ) { @@ -62,8 +53,6 @@ private function doLog( $level, $message, $context ) { if ( json_last_error() !== JSON_ERROR_NONE ) { $output = "[$level] " . LegacyLogger::format( $this->channel, $message, $context ); } - fwrite( STDERR, $output . PHP_EOL ); } - } diff --git a/dist-persist/wbstack/src/Logging/CustomSpi.php b/dist-persist/wbstack/src/Logging/CustomSpi.php index e01b0b463..3ace7b664 100644 --- a/dist-persist/wbstack/src/Logging/CustomSpi.php +++ b/dist-persist/wbstack/src/Logging/CustomSpi.php @@ -6,27 +6,18 @@ class CustomSpi implements Spi { - // Array of channels to log - protected $config; + private $ignoreChannels = []; + private $ignoreLevels = []; - public function __construct( array $config ) { -// if(!array_key_exists('channels', $config)) { -// throw new \InvalidArgumentException('channels arg needed'); -// } - if(!array_key_exists('ignoreLevels', $config)) { - throw new \InvalidArgumentException('ignoreLevels arg needed'); + public function __construct( array $config = [] ) { + if ( isset( $config[ 'ignore' ][ 'channels' ] ) ) { + $this->ignoreChannels = $config[ 'ignore' ][ 'channels' ]; } - if(!array_key_exists('ignoreAllInGroup', $config)) { - throw new \InvalidArgumentException('ignoreAllInGroup arg needed'); + if ( isset( $config[ 'ignore' ][ 'levels' ] ) ) { + $this->ignoreLevels = $config[ 'ignore' ][ 'levels' ]; } - if(!array_key_exists('logAllInGroup', $config)) { - throw new \InvalidArgumentException('logAllInGroup arg needed'); - } - if(!array_key_exists('logAllInGroupExceptDebug', $config)) { - throw new \InvalidArgumentException('logAllInGroupExceptDebug arg needed'); - } - $this->config = $config; } + /** * Get a logger instance. * @@ -34,6 +25,6 @@ public function __construct( array $config ) { * @return \Psr\Log\AbstractLogger Logger instance */ public function getLogger( $channel ) { - return new CustomLogger( $channel, $this->config); + return new CustomLogger( $channel, $this->ignoreChannels, $this->ignoreLevels ); } } diff --git a/dist-persist/wbstack/src/Settings/LocalSettings.php b/dist-persist/wbstack/src/Settings/LocalSettings.php index 844173117..32d5c653c 100644 --- a/dist-persist/wbstack/src/Settings/LocalSettings.php +++ b/dist-persist/wbstack/src/Settings/LocalSettings.php @@ -52,30 +52,16 @@ $wgMWLoggerDefaultSpi = [ 'class' => \WBStack\Logging\CustomSpi::class, 'args' => [[ - 'ignoreLevels' => [ - 'debug', - 'info', - ], - 'ignoreAllInGroup' => [ - 'DBPerformance', - 'objectcache',// ideally want to show objectcache errors, but not warnings - ], - 'logAllInGroup' => [ - 'WBSTACK', - 'HttpError', - 'SpamBlacklistHit', - 'security', - 'exception-json', - //'error', - 'fatal', - 'badpass', - 'badpass-priv', - 'api-warning', - ], - 'logAllInGroupExceptDebug' => [ - //'Wikibase', - 'BlockManager',// we want info https://gerrit.wikimedia.org/g/mediawiki/core/+/916c0307a06e64b49d4b7f0340808a38b6d5b9a4/includes/block/BlockManager.php#426 - ], + 'ignore' => [ + 'levels' => [ + 'debug', + 'info' + ], + 'channels' => [ + 'DBPerformance', + 'objectcache' + ] + ] ]], ]; } @@ -83,13 +69,7 @@ // Disable logging for local dev setup so it get's redirected to stderr and therefore can be viewed in the Kubernetes dashboard if ( getenv('MW_LOG_TO_STDERR') === 'yes' ) { $wgMWLoggerDefaultSpi = [ - 'class' => \WBStack\Logging\CustomSpi::class, - 'args' => [[ - 'ignoreLevels' => [], - 'ignoreAllInGroup' => [], - 'logAllInGroup' => [], - 'logAllInGroupExceptDebug' => [], - ]], + 'class' => \WBStack\Logging\CustomSpi::class ]; } diff --git a/dist/wbstack/src/Logging/CustomLogger.php b/dist/wbstack/src/Logging/CustomLogger.php index 502e37a37..47e3e73c7 100644 --- a/dist/wbstack/src/Logging/CustomLogger.php +++ b/dist/wbstack/src/Logging/CustomLogger.php @@ -8,34 +8,25 @@ class CustomLogger extends AbstractLogger { private $channel; - private $config; + private $ignoreChannels; + private $ignoreLevels; - public function __construct($channel, $config ) { + public function __construct( $channel, array $ignoreChannels, array $ignoreLevels ) { $this->channel = $channel; - $this->config = $config; + $this->ignoreChannels = $ignoreChannels; + $this->ignoreLevels = $ignoreLevels; } public function log( $level, $message, array $context = [] ) { - if(in_array($this->channel, $this->config['logAllInGroup'])) { - $this->doLog( $level, $message, $context ); - return; - } - if(in_array($this->channel, $this->config['logAllInGroupExceptDebug']) && $level !== 'debug') { - $this->doLog( $level, $message, $context ); - return; - } + $ignore = in_array( $this->channel, $this->ignoreChannels ) + || in_array( $level, $this->ignoreLevels ); - if(in_array($this->channel, $this->config['ignoreAllInGroup'])) { - return; - } - if(in_array($level, $this->config['ignoreLevels'])) { - return; + if ( !$ignore ) { + $this->doLog( $level, $message, $context ); } - - $this->doLog($level, $message, $context); } - private function doLog( $level, $message, $context ) { + private function doLog( $level, $message, array $context = [] ) { $payload = false; if ( str_contains( $this->channel, 'json' ) ) { @@ -62,8 +53,6 @@ private function doLog( $level, $message, $context ) { if ( json_last_error() !== JSON_ERROR_NONE ) { $output = "[$level] " . LegacyLogger::format( $this->channel, $message, $context ); } - fwrite( STDERR, $output . PHP_EOL ); } - } diff --git a/dist/wbstack/src/Logging/CustomSpi.php b/dist/wbstack/src/Logging/CustomSpi.php index e01b0b463..3ace7b664 100644 --- a/dist/wbstack/src/Logging/CustomSpi.php +++ b/dist/wbstack/src/Logging/CustomSpi.php @@ -6,27 +6,18 @@ class CustomSpi implements Spi { - // Array of channels to log - protected $config; + private $ignoreChannels = []; + private $ignoreLevels = []; - public function __construct( array $config ) { -// if(!array_key_exists('channels', $config)) { -// throw new \InvalidArgumentException('channels arg needed'); -// } - if(!array_key_exists('ignoreLevels', $config)) { - throw new \InvalidArgumentException('ignoreLevels arg needed'); + public function __construct( array $config = [] ) { + if ( isset( $config[ 'ignore' ][ 'channels' ] ) ) { + $this->ignoreChannels = $config[ 'ignore' ][ 'channels' ]; } - if(!array_key_exists('ignoreAllInGroup', $config)) { - throw new \InvalidArgumentException('ignoreAllInGroup arg needed'); + if ( isset( $config[ 'ignore' ][ 'levels' ] ) ) { + $this->ignoreLevels = $config[ 'ignore' ][ 'levels' ]; } - if(!array_key_exists('logAllInGroup', $config)) { - throw new \InvalidArgumentException('logAllInGroup arg needed'); - } - if(!array_key_exists('logAllInGroupExceptDebug', $config)) { - throw new \InvalidArgumentException('logAllInGroupExceptDebug arg needed'); - } - $this->config = $config; } + /** * Get a logger instance. * @@ -34,6 +25,6 @@ public function __construct( array $config ) { * @return \Psr\Log\AbstractLogger Logger instance */ public function getLogger( $channel ) { - return new CustomLogger( $channel, $this->config); + return new CustomLogger( $channel, $this->ignoreChannels, $this->ignoreLevels ); } } diff --git a/dist/wbstack/src/Settings/LocalSettings.php b/dist/wbstack/src/Settings/LocalSettings.php index 844173117..32d5c653c 100644 --- a/dist/wbstack/src/Settings/LocalSettings.php +++ b/dist/wbstack/src/Settings/LocalSettings.php @@ -52,30 +52,16 @@ $wgMWLoggerDefaultSpi = [ 'class' => \WBStack\Logging\CustomSpi::class, 'args' => [[ - 'ignoreLevels' => [ - 'debug', - 'info', - ], - 'ignoreAllInGroup' => [ - 'DBPerformance', - 'objectcache',// ideally want to show objectcache errors, but not warnings - ], - 'logAllInGroup' => [ - 'WBSTACK', - 'HttpError', - 'SpamBlacklistHit', - 'security', - 'exception-json', - //'error', - 'fatal', - 'badpass', - 'badpass-priv', - 'api-warning', - ], - 'logAllInGroupExceptDebug' => [ - //'Wikibase', - 'BlockManager',// we want info https://gerrit.wikimedia.org/g/mediawiki/core/+/916c0307a06e64b49d4b7f0340808a38b6d5b9a4/includes/block/BlockManager.php#426 - ], + 'ignore' => [ + 'levels' => [ + 'debug', + 'info' + ], + 'channels' => [ + 'DBPerformance', + 'objectcache' + ] + ] ]], ]; } @@ -83,13 +69,7 @@ // Disable logging for local dev setup so it get's redirected to stderr and therefore can be viewed in the Kubernetes dashboard if ( getenv('MW_LOG_TO_STDERR') === 'yes' ) { $wgMWLoggerDefaultSpi = [ - 'class' => \WBStack\Logging\CustomSpi::class, - 'args' => [[ - 'ignoreLevels' => [], - 'ignoreAllInGroup' => [], - 'logAllInGroup' => [], - 'logAllInGroupExceptDebug' => [], - ]], + 'class' => \WBStack\Logging\CustomSpi::class ]; }