Skip to content

Commit

Permalink
use Reflection to log Exception properties
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Oct 20, 2022
1 parent 4f1ad95 commit b3df450
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ parameters:
- '#LoggerInterface\|null#'
- '#Function apply_filters invoked with \d+ parameters, \d+ required.#'
- '#Parameter \#2 \$callback of static method WP_Mock::expect.*Added\(\) expects callable\(\): mixed, array.*given\.#'
- '#Variable \$log_data in empty\(\) always exists and is not falsy#'
16 changes: 13 additions & 3 deletions src/API/class-bh-wp-psr-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public function get_logger(): LoggerInterface {
* When an error is being logged, record the time of the last error, so later, an admin notice can be displayed,
* to inform them of the new problem.
*
* TODO: This always displays the admin notice even when the log itself is filtered. i.e. this function runs before
* the filter, so the code needs to be moved.
* TODO: Allow configuring which log levels result in the admin notice.
*
* TODO: include a link to the log url so the last file with an error will be linked, rather than the most recent log file.
*
* @param string $message The message to be logged.
Expand Down Expand Up @@ -86,7 +90,7 @@ public function log( $level, $message, $context = array() ) {
$context['filters'] = $wp_current_filter;

} elseif ( LogLevel::WARNING === $level || LogLevel::DEBUG === $settings_log_level ) {
$debug_backtrace = $this->get_backtrace( 2 );
$debug_backtrace = $this->get_backtrace( 3 );
$context['debug_backtrace'] = $debug_backtrace;

global $wp_current_filter;
Expand All @@ -99,7 +103,13 @@ public function log( $level, $message, $context = array() ) {
$exception_details['class'] = get_class( $exception );
$exception_details['message'] = $exception->getMessage();

// TODO: Use reflection to log properties of Exception subclasses.
$reflect = new \ReflectionClass( get_class( $exception ) );
$props = array();
foreach ( $reflect->getProperties() as $property ) {
$property->setAccessible( true );
$props[ $property->getName() ] = $property->getValue( $exception );
}
$exception_details['properties'] = $props;

$context['exception'] = $exception_details;
}
Expand All @@ -123,7 +133,7 @@ public function log( $level, $message, $context = array() ) {
* Filter to modify the log data.
* Return null to cancel logging this message.
*
* @pararm array{level:string,message:string,context:array} $log_data
* @param array{level:string,message:string,context:array} $log_data
* @param Logger_Settings_Interface $settings
* @param BH_WP_PSR_Logger $bh_wp_psr_logger
*/
Expand Down
12 changes: 8 additions & 4 deletions tests/unit/API/class-bh-wp-psr-logger-unit-Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ protected function tearDown(): void {
}

/**
* When an exception is passed in the context, it just gets logged as `{}`, so let's instead log the
* exception type and any strings it contains.
* When an exception is passed in the context, it normally just gets logged as `{}`, so let's instead log the
* exception type and message, and use reflection to get its properties.
*
* @covers ::log
*/
Expand All @@ -32,7 +32,7 @@ public function test_exception(): void {

$sut = new BH_WP_PSR_Logger( $settings, $logger );

$exception = new \Exception( 'Exception message' );
$exception = new \Exception( 'Exception message', 123 );

\WP_Mock::userFunction(
'update_option'
Expand All @@ -48,8 +48,12 @@ public function test_exception(): void {
$logged_exception = $logger->recordsByLevel['error'][0]['context']['exception'];

$this->assertArrayHasKey( 'class', $logged_exception );
$this->assertArrayHasKey( 'message', $logged_exception );

$this->assertArrayHasKey( 'message', $logged_exception );
$this->assertEquals( 'Exception message', $logged_exception['message'] );

$this->assertArrayHasKey( 'properties', $logged_exception );
$this->assertEquals( 123, $logged_exception['properties']['code'] );

}
}

0 comments on commit b3df450

Please sign in to comment.