Skip to content

Commit

Permalink
Merge pull request #29 from alleyinteractive/improve-log-display
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher authored Oct 1, 2020
2 parents c83db05 + 94729c1 commit 0304047
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
17 changes: 17 additions & 0 deletions inc/handler/class-post-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,26 @@ 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

if ( $user ) {
$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'] );

$this->queue[ $transient_key ] = $record;
Expand Down
6 changes: 5 additions & 1 deletion template-parts/log-display.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function ai_logger_render_table( array $data ) {
<?php
$function = ! empty( $item['class'] ) ? $item['class'] . '::' . $item['function'] : $item['function'];
printf(
'<code>%s</code> @ <code>%s</code> (%s)',
'<code>%s</code> in <strong>%s</strong> at line <strong>%s</strong>',
esc_html( $item['file'] ?? 'n/a' ),
esc_html( $function ),
esc_html( $item['line'] ?? '?' )
Expand All @@ -38,6 +38,10 @@ function ai_logger_render_table( array $data ) {
</li>
<?php endforeach; ?>
</ul>
<?php elseif ( 'user' === $key ) : ?>
<table>
<?php ai_logger_render_table( (array) $value ); ?>
</table>
<?php elseif ( is_scalar( $value ) ) : ?>
<code><?php echo esc_html( $value ); ?></code>
<?php elseif ( null === $value ) : ?>
Expand Down

0 comments on commit 0304047

Please sign in to comment.