-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-log-viewer.php
More file actions
186 lines (147 loc) · 6.85 KB
/
error-log-viewer.php
File metadata and controls
186 lines (147 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* Plugin Name: Error Log Viewer
* Description: Muestra, descarga y limpia el archivo debug.log desde el panel de administración, con filtro y resaltado.
* Version: 1.3.0
* Author: Yogui Dev
* Author URI: https://github.com/yogui-dev
*/
if (!defined('ABSPATH')) exit;
define('YD_VE_ERROR_LOG_PATH', ABSPATH . 'wp-content/debug.log');
add_action('admin_menu', 'yd_ve_error_log_viewer_menu');
function yd_ve_error_log_viewer_menu()
{
add_menu_page(
'Error Log Viewer',
'Error Log Viewer',
'manage_options',
'yd-ve-error-log-viewer',
'yd_ve_error_log_viewer_page',
'dashicons-warning',
80
);
}
function yd_ve_error_log_viewer_page()
{
if (!current_user_can('manage_options')) wp_die('No tienes permisos para ver esto.');
// Descargar el archivo
if (isset($_GET['download']) && file_exists(YD_VE_ERROR_LOG_PATH)) {
header('Content-Description: File Transfer');
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename=debug.log');
header('Content-Length: ' . filesize(YD_VE_ERROR_LOG_PATH));
readfile(YD_VE_ERROR_LOG_PATH);
exit;
}
// Limpiar el archivo (renombrar y crear nuevo vacío)
if (isset($_POST['clear_log']) && check_admin_referer('yd_ve_clear_log')) {
$timestamp = date('Y-m-d_H-i-s');
$old_log_path = dirname(YD_VE_ERROR_LOG_PATH) . "/debug.old.$timestamp.log";
if (file_exists(YD_VE_ERROR_LOG_PATH)) {
// Renombrar el log
rename(YD_VE_ERROR_LOG_PATH, $old_log_path);
// Crear archivo nuevo vacío
file_put_contents(YD_VE_ERROR_LOG_PATH, '');
echo '<div class="notice notice-success"><p>Log archivado como <code>' . esc_html(basename($old_log_path)) . '</code> y nuevo archivo creado.</p></div>';
} else {
echo '<div class="notice notice-warning"><p>No se encontró el archivo debug.log para archivar.</p></div>';
}
}
echo '<div class="wrap"><h1>Error Log Viewer (debug.log)</h1>';
if (!file_exists(YD_VE_ERROR_LOG_PATH)) {
echo '<p><strong>No se encontró el archivo debug.log.</strong></p></div>';
return;
}
$selected_log = isset($_GET['logfile']) ? sanitize_file_name($_GET['logfile']) : '';
$log_path = $selected_log ? dirname(YD_VE_ERROR_LOG_PATH) . '/' . $selected_log : YD_VE_ERROR_LOG_PATH;
if (!file_exists($log_path)) {
echo '<p><strong>No se encontró el archivo seleccionado.</strong></p></div>';
return;
}
$log_content = file_get_contents($log_path);
$search_term = isset($_GET['s']) ? sanitize_text_field($_GET['s']) : '';
echo '<form method="get" style="margin-bottom:10px;">';
echo '<input type="hidden" name="page" value="yd-ve-error-log-viewer" />';
echo '<input type="text" name="s" value="' . esc_attr($search_term) . '" placeholder="Buscar en el log..." />';
echo '<button class="button">Buscar</button>';
echo '</form>';
echo '<p><a class="button button-primary" href="' . admin_url('admin.php?page=yd-ve-error-log-viewer&download=1') . '">📥 Descargar log</a></p>';
echo '<form method="post" style="margin-top:10px;">';
wp_nonce_field('yd_ve_clear_log');
echo '<button type="submit" name="clear_log" class="button">🧹 Limpiar log</button>';
echo '</form>';
echo '<div style="background:#fff; border:1px solid #ccc; padding:10px; max-height:600px; overflow:auto; margin-top:20px; font-family:monospace;">';
$lines = explode(PHP_EOL, $log_content);
$lines = array_reverse($lines); // Muestra últimos errores arriba
foreach ($lines as $line) {
if ($search_term && stripos($line, $search_term) === false) continue;
$style = '';
if (stripos($line, 'Fatal error') !== false) $style = 'color:red;';
elseif (stripos($line, 'Warning') !== false) $style = 'color:orange;';
elseif (stripos($line, 'Notice') !== false) $style = 'color:blue;';
echo '<div style="' . esc_attr($style) . '">' . esc_html($line) . '</div>';
}
// Mostrar lista de logs antiguos
yd_ve_list_old_logs($selected_log);
echo '</div></div>';
}
add_action('admin_notices', 'yd_ve_check_debug_settings');
function yd_ve_check_debug_settings()
{
if (!current_user_can('manage_options')) return;
$debug_enabled = defined('WP_DEBUG') && WP_DEBUG;
$debug_log_enabled = defined('WP_DEBUG_LOG') && WP_DEBUG_LOG;
if (!$debug_enabled || !$debug_log_enabled) {
echo '<div class="notice notice-error">';
echo '<p><strong>Atención:</strong> El log de errores de WordPress no está activo.</p>';
echo '<p>Asegúrate de tener estas líneas en tu <code>wp-config.php</code>:</p>';
echo '<pre style="background:#f1f1f1; padding:10px; border-radius:5px;">define(\'WP_DEBUG\', true);
define(\'WP_DEBUG_LOG\', true);</pre>';
echo '</div>';
}
}
// Hook para mostrar aviso en el admin si hay nuevos errores
add_action('admin_notices', 'yd_ve_show_new_errors_notice');
function yd_ve_show_new_errors_notice()
{
if (!current_user_can('manage_options')) return;
$log_path = YD_VE_ERROR_LOG_PATH;
if (!file_exists($log_path)) return;
$current_size = filesize($log_path);
$stored_size = get_option('yd_ve_error_log_last_size', 0);
// Si hay nuevo contenido
if ($current_size > $stored_size) {
$size_diff = $current_size - $stored_size;
echo '<div class="notice notice-warning is-dismissible">';
echo '<p><strong>Nuevo error detectado:</strong> El archivo <code>debug.log</code> ha aumentado en <code>' . size_format($size_diff) . '</code>. ';
echo '<a href="' . admin_url('admin.php?page=yd-ve-error-log-viewer') . '">Revisar log</a>.</p>';
echo '</div>';
}
}
// Al visitar el visor, actualizar tamaño del log
add_action('admin_init', function () {
if (!current_user_can('manage_options')) return;
if (isset($_GET['page']) && $_GET['page'] === 'yd-ve-error-log-viewer') {
if (file_exists(YD_VE_ERROR_LOG_PATH)) {
update_option('yd_ve_error_log_last_size', filesize(YD_VE_ERROR_LOG_PATH));
}
}
});
function yd_ve_list_old_logs($selected_file = '')
{
$dir = dirname(YD_VE_ERROR_LOG_PATH);
$files = glob($dir . '/debug.old.*.log');
if (!$files) {
echo '<p><em>No hay logs antiguos.</em></p>';
return;
}
echo '<h2 style="margin-top:40px;">Logs Archivados</h2>';
echo '<ul style="list-style-type:disc; padding-left:20px;">';
foreach ($files as $file) {
$basename = basename($file);
$link = admin_url('admin.php?page=yd-ve-error-log-viewer&logfile=' . urlencode($basename));
$active = ($basename === $selected_file) ? 'font-weight:bold;' : '';
echo '<li><a href="' . esc_url($link) . '" style="' . $active . '">' . esc_html($basename) . '</a></li>';
}
echo '</ul>';
}