| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| add_action( 'admin_menu', function () {
|
| add_menu_page(
|
| 'Debug Log',
|
| 'Debug Log',
|
| 'manage_options',
|
| 'debug-log-viewer',
|
| 'debug_log_viewer_page',
|
| 'dashicons-code-standards',
|
| 80
|
| );
|
| });
|
|
|
|
|
| function debug_log_viewer_page() {
|
| if ( ! current_user_can( 'manage_options' ) ) {
|
| wp_die( 'Insufficient permissions.' );
|
| }
|
|
|
| $log_path = defined( 'WP_DEBUG_LOG' ) && is_string( WP_DEBUG_LOG )
|
| ? WP_DEBUG_LOG
|
| : WP_CONTENT_DIR . '/debug.log';
|
|
|
| $search = isset( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : '';
|
| $level = isset( $_GET['level'] ) ? sanitize_text_field( $_GET['level'] ) : '';
|
|
|
| $entries = [];
|
| if ( file_exists( $log_path ) ) {
|
| $lines = file( $log_path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
|
| foreach ( $lines as $line ) {
|
| if ( $search && stripos( $line, $search ) === false ) continue;
|
| if ( $level && stripos( $line, $level ) === false ) continue;
|
| $entries[] = $line;
|
| }
|
| }
|
|
|
|
|
| $entries = array_slice( array_reverse( $entries ), 0, 500 );
|
| ?>
|
| <style>
|
| .dlv-wrap { font-family: monospace; font-size: 13px; }
|
| .dlv-controls { margin-bottom: 1em; display: flex; gap: 8px; flex-wrap: wrap; }
|
| .dlv-controls input[type="search"] { width: 320px; }
|
| .dlv-log {
|
| background:
|
| padding: 16px; border-radius: 6px;
|
| overflow-x: auto; white-space: pre-wrap;
|
| word-break: break-all;
|
| }
|
| .dlv-line { display: flex; gap: 12px; }
|
| .dlv-lineno { color:
|
| .dlv-error { color:
|
| .dlv-warning { color:
|
| .dlv-notice { color:
|
| .dlv-fatal { color:
|
| .dlv-empty { text-align: center; color:
|
| </style>
|
|
|
| <div class="wrap dlv-wrap">
|
| <h1>Debug Log Viewer</h1>
|
| <p>
|
| <strong>File:</strong> <?php echo esc_html( $log_path ); ?><br>
|
| <strong>Size:</strong> <?php echo file_exists( $log_path ) ? size_format( filesize( $log_path ) ) : 'N/A'; ?>
|
| </p>
|
|
|
| <form method="get" class="dlv-controls">
|
| <input type="hidden" name="page" value="debug-log-viewer">
|
| <input type="search" name="s" value="<?php echo esc_attr( $search ); ?>" placeholder="Search…">
|
| <select name="level">
|
| <option value="">All levels</option>
|
| <option value="PHP Fatal error" <?php selected( $level, 'PHP Fatal error' ); ?>>Fatal</option>
|
| <option value="PHP Warning" <?php selected( $level, 'PHP Warning' ); ?>>Warning</option>
|
| <option value="PHP Notice" <?php selected( $level, 'PHP Notice' ); ?>>Notice</option>
|
| <option value="Deprecated" <?php selected( $level, 'Deprecated' ); ?>>Deprecated</option>
|
| </select>
|
| <button type="submit" class="button">Filter</button>
|
| <a href="<?php echo esc_url( admin_url( 'admin.php?page=debug-log-viewer' ) ); ?>" class="button button-secondary">Reset</a>
|
| </form>
|
|
|
| <?php if ( empty( $entries ) ) : ?>
|
| <div class="dlv-empty">No log entries found. Ensure <code>WP_DEBUG</code> and <code>WP_DEBUG_LOG</code> are set to <code>true</code>.</div>
|
| <?php else : ?>
|
| <div class="dlv-log">
|
| <?php foreach ( $entries as $i => $line ) :
|
| $cls = '';
|
| if ( stripos( $line, 'fatal' ) !== false ) $cls = 'dlv-fatal';
|
| elseif ( stripos( $line, 'warning' ) !== false ) $cls = 'dlv-warning';
|
| elseif ( stripos( $line, 'notice' ) !== false ) $cls = 'dlv-notice';
|
| elseif ( stripos( $line, 'deprecated' ) !== false ) $cls = 'dlv-notice';
|
| else $cls = 'dlv-error';
|
| ?>
|
| <div class="dlv-line">
|
| <span class="dlv-lineno"><?php echo count( $entries ) - $i; ?></span>
|
| <span class="<?php echo esc_attr( $cls ); ?>"><?php echo esc_html( $line ); ?></span>
|
| </div>
|
| <?php endforeach; ?>
|
| </div>
|
| <?php endif; ?>
|
| </div>
|
| <?php
|
| }
|
| |
| |
Comments