From: Timo Tijhof Date: Wed, 30 Aug 2017 21:38:57 +0000 (+0100) Subject: exception: Support message normalisation for structured logging X-Git-Tag: 1.31.0-rc.0~2188^2 X-Git-Url: http://git.cyclocoop.org/ecrire?a=commitdiff_plain;h=c2b3638a975c6b159a78c5e1f00481b01014fd66;p=lhc%2Fweb%2Fwiklou.git exception: Support message normalisation for structured logging Let Monolog insert exception_id and exception_url so that consumers of logging data (such as Logstash) may provide a normalised message that does not contain these variants, for ease of aggregation and message trend counting. Bug: T45086 Change-Id: I7cc8f8c9e68031ad6771593d390079c0a3a535b9 --- diff --git a/includes/exception/MWExceptionHandler.php b/includes/exception/MWExceptionHandler.php index bf232e9799..a2ec391dc1 100644 --- a/includes/exception/MWExceptionHandler.php +++ b/includes/exception/MWExceptionHandler.php @@ -441,6 +441,24 @@ TXT; return "[$id] $url $type from line $line of $file: $message"; } + /** + * Get a normalised message for formatting with PSR-3 log event context. + * + * Must be used together with `getLogContext()` to be useful. + * + * @since 1.30 + * @param Exception|Throwable $e + * @return string + */ + public static function getLogNormalMessage( $e ) { + $type = get_class( $e ); + $file = $e->getFile(); + $line = $e->getLine(); + $message = $e->getMessage(); + + return "[{exception_id}] {exception_url} $type from line $line of $file: $message"; + } + /** * @param Exception|Throwable $e * @return string @@ -468,6 +486,7 @@ TXT; return [ 'exception' => $e, 'exception_id' => WebRequest::getRequestId(), + 'exception_url' => self::getURL() ?: '[no req]', 'caught_by' => $catcher ]; } @@ -595,7 +614,7 @@ TXT; if ( !( $e instanceof MWException ) || $e->isLoggable() ) { $logger = LoggerFactory::getInstance( 'exception' ); $logger->error( - self::getLogMessage( $e ), + self::getLogNormalMessage( $e ), self::getLogContext( $e, $catcher ) ); @@ -629,7 +648,7 @@ TXT; $logger = LoggerFactory::getInstance( $channel ); $logger->log( $level, - self::getLogMessage( $e ), + self::getLogNormalMessage( $e ), self::getLogContext( $e, $catcher ) ); }