From 74aa6b07db7ff346bab4d9ef1cb8871b6c8550cb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gerg=C5=91=20Tisza?= Date: Thu, 24 Nov 2016 05:19:05 +0000 Subject: [PATCH] Improve logging of exceptions which are not thrown but attached to context Bug: T151290 Change-Id: I9cc9f54c2987cf960343b5eca2a1c6d006b892bf --- .../logger/monolog/LogstashFormatter.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/includes/debug/logger/monolog/LogstashFormatter.php b/includes/debug/logger/monolog/LogstashFormatter.php index 553cbf61c4..09ed7555db 100644 --- a/includes/debug/logger/monolog/LogstashFormatter.php +++ b/includes/debug/logger/monolog/LogstashFormatter.php @@ -6,6 +6,7 @@ namespace MediaWiki\Logger\Monolog; * LogstashFormatter squashes the base message array and the context and extras subarrays into one. * This can result in unfortunately named context fields overwriting other data (T145133). * This class modifies the standard LogstashFormatter to rename such fields and flag the message. + * Also changes exception JSON-ification which is done poorly by the standard class. * * Compatible with Monolog 1.x only. * @@ -80,4 +81,31 @@ class LogstashFormatter extends \Monolog\Formatter\LogstashFormatter { } return $fields; } + + /** + * Use a more user-friendly trace format than NormalizerFormatter + * @param \Exception|\Throwable $e + * @return array + */ + protected function normalizeException( $e ) { + if ( !$e instanceof \Exception && !$e instanceof \Throwable ) { + throw new \InvalidArgumentException( 'Exception/Throwable expected, got ' + . gettype( $e ) . ' / ' . get_class( $e ) ); + } + + $data = [ + 'class' => get_class( $e ), + 'message' => $e->getMessage(), + 'code' => $e->getCode(), + 'file' => $e->getFile() . ':' . $e->getLine(), + 'trace' => \MWExceptionHandler::getRedactedTraceAsString( $e ), + ]; + + $previous = $e->getPrevious(); + if ( $previous ) { + $data['previous'] = $this->normalizeException( $previous ); + } + + return $data; + } } -- 2.20.1