Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions dist-persist/wbstack/src/Logging/CustomLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) ) {
Expand All @@ -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 );
}

}
27 changes: 9 additions & 18 deletions dist-persist/wbstack/src/Logging/CustomSpi.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,25 @@

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.
*
* @param string $channel Logging channel
* @return \Psr\Log\AbstractLogger Logger instance
*/
public function getLogger( $channel ) {
return new CustomLogger( $channel, $this->config);
return new CustomLogger( $channel, $this->ignoreChannels, $this->ignoreLevels );
}
}
42 changes: 11 additions & 31 deletions dist-persist/wbstack/src/Settings/LocalSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,44 +52,24 @@
$wgMWLoggerDefaultSpi = [
'class' => \WBStack\Logging\CustomSpi::class,
'args' => [[
'ignoreLevels' => [
'debug',
'info',
],
'ignoreAllInGroup' => [
'DBPerformance',
'objectcache',// ideally want to show objectcache errors, but not warnings
],
'logAllInGroup' => [
'WBSTACK',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe previously all debug and info level things in this group were logged where as now they will not be

'HttpError',
'SpamBlacklistHit',
'security',
'exception-json',
//'error',
'fatal',
'badpass',
'badpass-priv',
'api-warning',
],
'logAllInGroupExceptDebug' => [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this may now be ignored when before it wasn't. This being info level log lines explaining that a user was on a IP block list (which we want to log so we can tell users what to do when they complain)

//'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'
]
]
]],
];
}

// 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
];
}

Expand Down
31 changes: 10 additions & 21 deletions dist/wbstack/src/Logging/CustomLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) ) {
Expand All @@ -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 );
}

}
27 changes: 9 additions & 18 deletions dist/wbstack/src/Logging/CustomSpi.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,25 @@

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.
*
* @param string $channel Logging channel
* @return \Psr\Log\AbstractLogger Logger instance
*/
public function getLogger( $channel ) {
return new CustomLogger( $channel, $this->config);
return new CustomLogger( $channel, $this->ignoreChannels, $this->ignoreLevels );
}
}
42 changes: 11 additions & 31 deletions dist/wbstack/src/Settings/LocalSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,44 +52,24 @@
$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'
]
]
]],
];
}

// 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
];
}

Expand Down