From 057b870b47a56fa8e7e056df02c2334d2273caea Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gerg=C5=91=20Tisza?= Date: Tue, 4 Oct 2016 00:24:58 +0000 Subject: [PATCH] Warn when a log context accidentally overrides a log processor Bug: T145133 Change-Id: I6677dbf608aac1d7fd6036f5e51977d56ce2fc57 --- .../debug/logger/monolog/WikiProcessor.php | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/includes/debug/logger/monolog/WikiProcessor.php b/includes/debug/logger/monolog/WikiProcessor.php index ad939a0932..81e1e14763 100644 --- a/includes/debug/logger/monolog/WikiProcessor.php +++ b/includes/debug/logger/monolog/WikiProcessor.php @@ -29,6 +29,17 @@ namespace MediaWiki\Logger\Monolog; * @copyright © 2013 Bryan Davis and Wikimedia Foundation. */ class WikiProcessor { + /** @var array Keys which should not be used in log context */ + protected $reservedKeys = [ + // from monolog:src/Monolog/Formatter/LogstashFormatter.php#L71-L88 + 'message', 'channel', 'level', 'type', + // from WebProcessor + 'url', 'ip', 'http_method', 'server', 'referrer', + // from WikiProcessor + 'host', 'wiki', 'reqId', 'mwversion', + // from config magic + 'normalized_message', + ]; /** * @param array $record @@ -36,6 +47,15 @@ class WikiProcessor { */ public function __invoke( array $record ) { global $wgVersion; + + // some log aggregators such as Logstash will merge the log context into the main + // metadata and end up overwriting the data coming from processors + foreach ( $this->reservedKeys as $key ) { + if ( isset( $record['context'][$key] ) ) { + wfLogWarning( __METHOD__ . ": '$key' key overwritten in log context." ); + } + } + $record['extra'] = array_merge( $record['extra'], [ @@ -47,5 +67,4 @@ class WikiProcessor { ); return $record; } - } -- 2.20.1