From 1476f1a061690d92e7d3b0a2caa8a789833164da Mon Sep 17 00:00:00 2001 From: Andrew Kostka Date: Fri, 2 Feb 2024 13:00:06 +0000 Subject: [PATCH] Add documentation to custom logging classes --- .../wbstack/src/Logging/CustomLogger.php | 28 ++++++++++++------- .../wbstack/src/Logging/CustomSpi.php | 25 +++++++++-------- dist/wbstack/src/Logging/CustomLogger.php | 28 ++++++++++++------- dist/wbstack/src/Logging/CustomSpi.php | 25 +++++++++-------- 4 files changed, 62 insertions(+), 44 deletions(-) diff --git a/dist-persist/wbstack/src/Logging/CustomLogger.php b/dist-persist/wbstack/src/Logging/CustomLogger.php index 502e37a37..e6b26e522 100644 --- a/dist-persist/wbstack/src/Logging/CustomLogger.php +++ b/dist-persist/wbstack/src/Logging/CustomLogger.php @@ -10,29 +10,39 @@ class CustomLogger extends AbstractLogger { private $channel; private $config; - public function __construct($channel, $config ) { + /** + * @param string $channel Logging channel + * @param array $config = [ + * 'ignoreLevels' => (string[]) Ignore messages with these levels + * 'ignoreAllInGroup' => (string[]) Ignore messages in these channels + * 'logAllInGroup' => (string[]) Log all messages in these channels regardless + * of what is set in either 'ignoreLevels' or 'ignoreAllInGroup' + * 'logAllInGroupExceptDebug' => (string[]) Log all non-debug level messages + * in these channels regardless of what is set in either 'ignoreLevels' + * or 'ignoreAllInGroup' + * ] + */ + public function __construct( $channel, $config ) { $this->channel = $channel; $this->config = $config; } public function log( $level, $message, array $context = [] ) { - if(in_array($this->channel, $this->config['logAllInGroup'])) { + if( in_array( $this->channel, $this->config[ 'logAllInGroup'] ) ) { $this->doLog( $level, $message, $context ); return; } - if(in_array($this->channel, $this->config['logAllInGroupExceptDebug']) && $level !== 'debug') { + if( in_array( $this->channel, $this->config[ 'logAllInGroupExceptDebug' ] ) && $level !== 'debug' ) { $this->doLog( $level, $message, $context ); return; } - - if(in_array($this->channel, $this->config['ignoreAllInGroup'])) { + if( in_array( $this->channel, $this->config[ 'ignoreAllInGroup' ] ) ) { return; } - if(in_array($level, $this->config['ignoreLevels'])) { + if( in_array( $level, $this->config[ 'ignoreLevels' ] ) ) { return; } - - $this->doLog($level, $message, $context); + $this->doLog( $level, $message, $context ); } private function doLog( $level, $message, $context ) { @@ -62,8 +72,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..ecd9f851d 100644 --- a/dist-persist/wbstack/src/Logging/CustomSpi.php +++ b/dist-persist/wbstack/src/Logging/CustomSpi.php @@ -6,27 +6,28 @@ class CustomSpi implements Spi { - // Array of channels to log protected $config; + /** + * @param array $config Logger configuration + * @see CustomLogger::__construct() + */ public function __construct( array $config ) { -// if(!array_key_exists('channels', $config)) { -// throw new \InvalidArgumentException('channels arg needed'); -// } - if(!array_key_exists('ignoreLevels', $config)) { + if ( !array_key_exists( 'ignoreLevels', $config ) ) { throw new \InvalidArgumentException('ignoreLevels arg needed'); } - if(!array_key_exists('ignoreAllInGroup', $config)) { - throw new \InvalidArgumentException('ignoreAllInGroup arg needed'); + if ( !array_key_exists( 'ignoreAllInGroup', $config ) ) { + throw new \InvalidArgumentException( 'ignoreAllInGroup arg needed' ); } - if(!array_key_exists('logAllInGroup', $config)) { - throw new \InvalidArgumentException('logAllInGroup arg needed'); + if ( !array_key_exists( 'logAllInGroup', $config ) ) { + throw new \InvalidArgumentException( 'logAllInGroup arg needed' ); } - if(!array_key_exists('logAllInGroupExceptDebug', $config)) { - throw new \InvalidArgumentException('logAllInGroupExceptDebug arg needed'); + if ( !array_key_exists( 'logAllInGroupExceptDebug', $config ) ) { + throw new \InvalidArgumentException( 'logAllInGroupExceptDebug arg needed' ); } $this->config = $config; } + /** * Get a logger instance. * @@ -34,6 +35,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->config ); } } diff --git a/dist/wbstack/src/Logging/CustomLogger.php b/dist/wbstack/src/Logging/CustomLogger.php index 502e37a37..e6b26e522 100644 --- a/dist/wbstack/src/Logging/CustomLogger.php +++ b/dist/wbstack/src/Logging/CustomLogger.php @@ -10,29 +10,39 @@ class CustomLogger extends AbstractLogger { private $channel; private $config; - public function __construct($channel, $config ) { + /** + * @param string $channel Logging channel + * @param array $config = [ + * 'ignoreLevels' => (string[]) Ignore messages with these levels + * 'ignoreAllInGroup' => (string[]) Ignore messages in these channels + * 'logAllInGroup' => (string[]) Log all messages in these channels regardless + * of what is set in either 'ignoreLevels' or 'ignoreAllInGroup' + * 'logAllInGroupExceptDebug' => (string[]) Log all non-debug level messages + * in these channels regardless of what is set in either 'ignoreLevels' + * or 'ignoreAllInGroup' + * ] + */ + public function __construct( $channel, $config ) { $this->channel = $channel; $this->config = $config; } public function log( $level, $message, array $context = [] ) { - if(in_array($this->channel, $this->config['logAllInGroup'])) { + if( in_array( $this->channel, $this->config[ 'logAllInGroup'] ) ) { $this->doLog( $level, $message, $context ); return; } - if(in_array($this->channel, $this->config['logAllInGroupExceptDebug']) && $level !== 'debug') { + if( in_array( $this->channel, $this->config[ 'logAllInGroupExceptDebug' ] ) && $level !== 'debug' ) { $this->doLog( $level, $message, $context ); return; } - - if(in_array($this->channel, $this->config['ignoreAllInGroup'])) { + if( in_array( $this->channel, $this->config[ 'ignoreAllInGroup' ] ) ) { return; } - if(in_array($level, $this->config['ignoreLevels'])) { + if( in_array( $level, $this->config[ 'ignoreLevels' ] ) ) { return; } - - $this->doLog($level, $message, $context); + $this->doLog( $level, $message, $context ); } private function doLog( $level, $message, $context ) { @@ -62,8 +72,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..ecd9f851d 100644 --- a/dist/wbstack/src/Logging/CustomSpi.php +++ b/dist/wbstack/src/Logging/CustomSpi.php @@ -6,27 +6,28 @@ class CustomSpi implements Spi { - // Array of channels to log protected $config; + /** + * @param array $config Logger configuration + * @see CustomLogger::__construct() + */ public function __construct( array $config ) { -// if(!array_key_exists('channels', $config)) { -// throw new \InvalidArgumentException('channels arg needed'); -// } - if(!array_key_exists('ignoreLevels', $config)) { + if ( !array_key_exists( 'ignoreLevels', $config ) ) { throw new \InvalidArgumentException('ignoreLevels arg needed'); } - if(!array_key_exists('ignoreAllInGroup', $config)) { - throw new \InvalidArgumentException('ignoreAllInGroup arg needed'); + if ( !array_key_exists( 'ignoreAllInGroup', $config ) ) { + throw new \InvalidArgumentException( 'ignoreAllInGroup arg needed' ); } - if(!array_key_exists('logAllInGroup', $config)) { - throw new \InvalidArgumentException('logAllInGroup arg needed'); + if ( !array_key_exists( 'logAllInGroup', $config ) ) { + throw new \InvalidArgumentException( 'logAllInGroup arg needed' ); } - if(!array_key_exists('logAllInGroupExceptDebug', $config)) { - throw new \InvalidArgumentException('logAllInGroupExceptDebug arg needed'); + if ( !array_key_exists( 'logAllInGroupExceptDebug', $config ) ) { + throw new \InvalidArgumentException( 'logAllInGroupExceptDebug arg needed' ); } $this->config = $config; } + /** * Get a logger instance. * @@ -34,6 +35,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->config ); } }