[MediaWiki-commits] [Gerrit] Convert MWExceptionHandler to use structured logging - change (mediawiki/core)

2015-07-15 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged.

Change subject: Convert MWExceptionHandler to use structured logging
..


Convert MWExceptionHandler to use structured logging

Replace wfDebugLog() calls in MWExceptionHandler with direct use of
LoggerFactory and LoggerInterface. Logged exceptions are added to the
log message context.

LegacyLogger is also updated to append stack traces to any log event
when $wgLogExceptionBacktrace is true and the PSR-3 recommendation of
passing the exception as an 'exception' context item.

Handling of context data in LegacyLogger is expanded to support arrays,
exceptions and common object types.

Bug: T88649
Change-Id: I71499d895582bdea033a2516c902e23e38084080
---
M includes/debug/logger/LegacyLogger.php
M includes/exception/MWExceptionHandler.php
M tests/phpunit/includes/debug/logger/LegacyLoggerTest.php
3 files changed, 215 insertions(+), 48 deletions(-)

Approvals:
  Gergő Tisza: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/debug/logger/LegacyLogger.php 
b/includes/debug/logger/LegacyLogger.php
index 6bd6411..8010790 100644
--- a/includes/debug/logger/LegacyLogger.php
+++ b/includes/debug/logger/LegacyLogger.php
@@ -22,6 +22,7 @@
 
 use DateTimeZone;
 use MWDebug;
+use MWExceptionHandler;
 use Psr\Log\AbstractLogger;
 use Psr\Log\LogLevel;
 use UDPTransport;
@@ -167,7 +168,7 @@
 * @return string
 */
public static function format( $channel, $message, $context ) {
-   global $wgDebugLogGroups;
+   global $wgDebugLogGroups, $wgLogExceptionBacktrace;
 
if ( $channel === 'wfDebug' ) {
$text = self::formatAsWfDebug( $channel, $message, 
$context );
@@ -213,6 +214,16 @@
} else {
// Default formatting is wfDebugLog's historic style
$text = self::formatAsWfDebugLog( $channel, $message, 
$context );
+   }
+
+   // Append stacktrace of exception if available
+   if ( $wgLogExceptionBacktrace 
+   isset( $context['exception'] ) 
+   $context['exception'] instanceof Exception
+   ) {
+   $text .= MWExceptionHandler::getRedactedTraceAsString(
+   $context['exception']-getTraceAsString()
+   ) . \n;
}
 
return self::interpolate( $text, $context );
@@ -301,7 +312,7 @@
if ( strpos( $message, '{' ) !== false ) {
$replace = array();
foreach ( $context as $key = $val ) {
-   $replace['{' . $key . '}'] = $val;
+   $replace['{' . $key . '}'] = self::flatten( 
$val );
}
$message = strtr( $message, $replace );
}
@@ -310,6 +321,66 @@
 
 
/**
+* Convert a logging context element to a string suitable for
+* interpolation.
+*
+* @param mixed $item
+* @return string
+*/
+   protected static function flatten( $item ) {
+   if ( null === $item ) {
+   return '[Null]';
+   }
+
+   if ( is_bool( $item ) ) {
+   return $item ? 'true' : 'false';
+   }
+
+   if ( is_float( $item ) ) {
+   if ( is_infinite( $item ) ) {
+   return ( $item  0 ? '' : '-' ) . 'INF';
+   }
+   if ( is_nan( $item ) ) {
+   return 'NaN';
+   }
+   return $data;
+   }
+
+   if ( is_scalar( $item ) ) {
+   return (string) $item;
+   }
+
+   if ( is_array( $item ) ) {
+   return '[Array(' . count( $item ) . ')]';
+   }
+
+   if ( $item instanceof \DateTime ) {
+   return $item-format( 'c' );
+   }
+
+   if ( $item instanceof \Exception ) {
+   return '[Exception ' . get_class( $item ) . '( ' .
+   $item-getFile() . ':' . $item-getLine() . ') 
' .
+   $item-getMessage() . ']';
+   }
+
+   if ( is_object( $item ) ) {
+   if ( method_exists( $item, '__toString' ) ) {
+   return (string) $item;
+   }
+
+   return '[Object ' . get_class( $item ) . ']';
+   }
+
+   if ( is_resource( $item ) ) {
+   return '[Resource ' . get_resource_type( $item ) . ']';
+   }
+
+   return '[Unknown ' . gettype( $item ) 

[MediaWiki-commits] [Gerrit] Convert MWExceptionHandler to use structured logging - change (mediawiki/core)

2015-05-24 Thread BryanDavis (Code Review)
BryanDavis has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/213348

Change subject: Convert MWExceptionHandler to use structured logging
..

Convert MWExceptionHandler to use structured logging

Replace wfDebugLog() calls in MWExceptionHandler with direct use of
LoggerFactory and LoggerInterface. Logged exceptions are added to the
log message context as structured data similar to the existing
MWExceptionHandler::jsonSerializeException() formatted data.

LegacyLogger is also updated to append stack traces to any log event
when $wgLogExceptionBacktrace is true and the PSR-3 recommendation of
passing the exception as an 'exception' context item.

Bug: T88649
Change-Id: I71499d895582bdea033a2516c902e23e38084080
---
M includes/debug/logger/LegacyLogger.php
M includes/exception/MWExceptionHandler.php
2 files changed, 113 insertions(+), 47 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/48/213348/1

diff --git a/includes/debug/logger/LegacyLogger.php 
b/includes/debug/logger/LegacyLogger.php
index 6027375..8d5a5bf 100644
--- a/includes/debug/logger/LegacyLogger.php
+++ b/includes/debug/logger/LegacyLogger.php
@@ -22,6 +22,7 @@
 
 use DateTimeZone;
 use MWDebug;
+use MWExceptionHandler;
 use Psr\Log\AbstractLogger;
 use Psr\Log\LogLevel;
 use UDPTransport;
@@ -167,7 +168,7 @@
 * @return string
 */
public static function format( $channel, $message, $context ) {
-   global $wgDebugLogGroups;
+   global $wgDebugLogGroups, $wgLogExceptionBacktrace;
 
if ( $channel === 'wfDebug' ) {
$text = self::formatAsWfDebug( $channel, $message, 
$context );
@@ -213,6 +214,20 @@
} else {
// Default formatting is wfDebugLog's historic style
$text = self::formatAsWfDebugLog( $channel, $message, 
$context );
+   }
+
+   if ( $wgLogExceptionBacktrace  isset( $context['exception'] ) 
) {
+   // Append stacktrace of exception if available
+   $e = $context['exception'];
+   if ( is_object( $e )  $e instanceof Exception ) {
+   $text .= 
MWExceptionHandler::getRedactedTraceAsString(
+   $e-getTraceAsString()
+   ) . \n;
+   } elseif ( is_array( $e )  isset( $e['backtrace'] ) ) 
{
+   $text .= 
MWExceptionHandler::prettyPrintRedactedTrace(
+   $e['backtrace']
+   ) . \n;
+   }
}
 
return self::interpolate( $text, $context );
@@ -301,7 +316,9 @@
if ( strpos( $message, '{' ) !== false ) {
$replace = array();
foreach ( $context as $key = $val ) {
-   $replace['{' . $key . '}'] = $val;
+   if ( is_string( $val ) ) {
+   $replace['{' . $key . '}'] = $val;
+   }
}
$message = strtr( $message, $replace );
}
diff --git a/includes/exception/MWExceptionHandler.php 
b/includes/exception/MWExceptionHandler.php
index c50b6c8..9c09052 100644
--- a/includes/exception/MWExceptionHandler.php
+++ b/includes/exception/MWExceptionHandler.php
@@ -18,6 +18,8 @@
  * @file
  */
 
+use MediaWiki\Logger\LoggerFactory;
+
 /**
  * Handler class for MWExceptions
  * @ingroup Exception
@@ -127,10 +129,11 @@
public static function rollbackMasterChangesAndLog( Exception $e ) {
$factory = wfGetLBFactory();
if ( $factory-hasMasterChanges() ) {
-   wfDebugLog( 'Bug56269',
+   $logger = LoggerFactory::getInstance( 'Bug56269' );
+   $logger-warning(
'Exception thrown with an uncommited database 
transaction: ' .
-   MWExceptionHandler::getLogMessage( $e ) 
. \n .
-   $e-getTraceAsString()
+   MWExceptionHandler::getLogMessage( $e ),
+   self::getLogContext( $e )
);
$factory-rollbackMasterChanges();
}
@@ -276,9 +279,23 @@
 * @return string
 */
public static function getRedactedTraceAsString( Exception $e ) {
+   return self::prettyPrintRedactedTrace(
+   self::getRedactedTrace( $e )
+   );
+   }
+
+   /**
+* Generate a string representation of a structured stack trace 
generated
+* by getRedactedTrace().
+*