From f1e898c61a0be812e9fa20ae2dae0860ff5c0120 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Thu, 10 Sep 2020 20:45:27 -0400 Subject: [PATCH 1/2] Improve log display and include info about the user --- inc/handler/class-post-handler.php | 14 ++++++++++++++ template-parts/log-display.php | 6 +++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/inc/handler/class-post-handler.php b/inc/handler/class-post-handler.php index 5033fb5f..cf44f33f 100644 --- a/inc/handler/class-post-handler.php +++ b/inc/handler/class-post-handler.php @@ -121,8 +121,22 @@ public function clear() { * @param array $record Log Record. */ protected function write( array $record ): void { + $user = wp_get_current_user(); + // Capture the stack trace. $record['extra']['backtrace'] = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace + $record['extra']['user'] = [ + 'ID' => $user->ID, + 'user_login' => $user->user_login, + 'user_email' => $user->user_email, + ]; + + /** + * Filter the log record. + * + * @param array $record Log record. + */ + $record = (array) apply_filters( 'ai_logger_log_record', $record ); $transient_key = 'ai_log_' . md5( $record['message'] . $record['channel'] ); diff --git a/template-parts/log-display.php b/template-parts/log-display.php index eee4a30c..c740cd7a 100644 --- a/template-parts/log-display.php +++ b/template-parts/log-display.php @@ -29,7 +29,7 @@ function ai_logger_render_table( array $data ) { %s @ %s (%s)', + '%s in %s at line %s', esc_html( $item['file'] ?? 'n/a' ), esc_html( $function ), esc_html( $item['line'] ?? '?' ) @@ -38,6 +38,10 @@ function ai_logger_render_table( array $data ) { + + + +
From 94729c187024d9c7b1387c5fab2b4640b7c4a24e Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Thu, 10 Sep 2020 20:46:55 -0400 Subject: [PATCH 2/2] Only include if the user is set --- inc/handler/class-post-handler.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/inc/handler/class-post-handler.php b/inc/handler/class-post-handler.php index cf44f33f..74f8507a 100644 --- a/inc/handler/class-post-handler.php +++ b/inc/handler/class-post-handler.php @@ -125,11 +125,14 @@ protected function write( array $record ): void { // Capture the stack trace. $record['extra']['backtrace'] = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace - $record['extra']['user'] = [ - 'ID' => $user->ID, - 'user_login' => $user->user_login, - 'user_email' => $user->user_email, - ]; + + if ( $user ) { + $record['extra']['user'] = [ + 'ID' => $user->ID, + 'user_login' => $user->user_login, + 'user_email' => $user->user_email, + ]; + } /** * Filter the log record.