Skip to content

Commit

Permalink
v 0.8.0
Browse files Browse the repository at this point in the history
Filter results
  • Loading branch information
Darklg committed Apr 23, 2024
1 parent ddd4da8 commit c840d2a
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 29 deletions.
Binary file modified lang/wpuerrorlogs-fr_FR.mo
Binary file not shown.
38 changes: 25 additions & 13 deletions lang/wpuerrorlogs-fr_FR.po
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
msgid ""
msgstr ""
"Project-Id-Version: WPU Error Logs\n"
"POT-Creation-Date: 2024-04-16 22:55+0200\n"
"POT-Creation-Date: 2024-04-23 17:14+0200\n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Language-Team: \n"
Expand All @@ -24,52 +24,64 @@ msgstr "Donnez un sens à vos fichiers logs"
msgid "Settings"
msgstr "Réglages"

#: wpuerrorlogs.php:102
#: wpuerrorlogs.php:113
msgid "Count"
msgstr "Nombre"

#: wpuerrorlogs.php:103
#: wpuerrorlogs.php:114
msgid "Date"
msgstr "Date"

#: wpuerrorlogs.php:104
#: wpuerrorlogs.php:115
msgid "Type"
msgstr "Type"

#: wpuerrorlogs.php:105
#: wpuerrorlogs.php:116
msgid "Text"
msgstr "Texte"

#: wpuerrorlogs.php:109
#: wpuerrorlogs.php:120 wpuerrorlogs.php:138
msgid "Filter results"
msgstr "Filtrer les résultats"

#: wpuerrorlogs.php:124
msgid "Check the last :"
msgstr "Vérifiez les derniers :"

#: wpuerrorlogs.php:112
#: wpuerrorlogs.php:127
msgid "1 day"
msgstr "1 jour"

#: wpuerrorlogs.php:112
#: wpuerrorlogs.php:127
#, php-format
msgid "%s days"
msgstr "%s jours"

#: wpuerrorlogs.php:118
#: wpuerrorlogs.php:134
msgid "Search in errors"
msgstr "Rechercher dans les erreurs"

#: wpuerrorlogs.php:135
msgid "Search"
msgstr "Rechercher"

#: wpuerrorlogs.php:144
msgid "Top errors"
msgstr "Principales erreurs"

#: wpuerrorlogs.php:124 wpuerrorlogs.php:134
#: wpuerrorlogs.php:150 wpuerrorlogs.php:160
msgid "No errors at the moment."
msgstr "Pas d’erreurs pour le moment."

#: wpuerrorlogs.php:128
#: wpuerrorlogs.php:154
msgid "Latest errors"
msgstr "Dernières erreurs"

#: wpuerrorlogs.php:147
#: wpuerrorlogs.php:173
msgid "Latest fatal errors"
msgstr "Dernières erreurs fatales"

#: wpuerrorlogs.php:342
#: wpuerrorlogs.php:464
msgid "Full error"
msgstr "Erreur complète"

Expand Down
136 changes: 120 additions & 16 deletions wpuerrorlogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Plugin URI: https://github.com/WordPressUtilities/wpuerrorlogs
Update URI: https://github.com/WordPressUtilities/wpuerrorlogs
Description: Make sense of your log files
Version: 0.7.0
Version: 0.8.0
Author: Darklg
Author URI: https://github.com/Darklg
Text Domain: wpuerrorlogs
Expand All @@ -23,7 +23,7 @@
class WPUErrorLogs {
public $settings_update;
private $number_of_days = 10;
private $plugin_version = '0.7.0';
private $plugin_version = '0.8.0';
private $plugin_settings = array(
'id' => 'wpuerrorlogs',
'name' => 'WPU Error Logs'
Expand Down Expand Up @@ -64,6 +64,9 @@ public function plugins_loaded() {
'settings_name' => __('Settings', 'wpuerrorlogs'),
'function_content' => array(&$this,
'page_content__main'
),
'function_action' => array(&$this,
'page_action__main'
)
)
);
Expand Down Expand Up @@ -95,7 +98,15 @@ public function page_content__main() {
$number_of_days = intval($_GET['number_of_days']);
}

$errors = $this->get_logs($number_of_days);
$search = '';
if (isset($_GET['s'])) {
$search = stripslashes(stripslashes($_GET['s']));
}

$errors = $this->get_logs(array(
'number_of_days' => $number_of_days,
'search_string' => $search
));

/* Keep only first five and extract data */
$colnames = array(
Expand All @@ -105,13 +116,28 @@ public function page_content__main() {
'text' => __('Text', 'wpuerrorlogs')
);

echo '<details>';
echo '<summary>' . __('Filter results', 'wpuerrorlogs') . '</summary>';

/* Select number of days */
echo '<label for="wpuerrorlogs_switch_day">' . __('Check the last :', 'wpuerrorlogs') . '</label> ';
echo '<select id="wpuerrorlogs_switch_day" onchange="document.location.href=\'' . $this->adminpages->get_page_url('main') . '&number_of_days=\' + this.value;">';
echo '<p>';
echo '<label for="wpuerrorlogs_switch_day">' . __('Check the last :', 'wpuerrorlogs') . '</label><br />';
echo '<select name="number_of_days" id="wpuerrorlogs_switch_day" onchange="document.location.href=\'' . $this->adminpages->get_page_url('main') . '&number_of_days=\' + this.value;">';
for ($i = $this->number_of_days; $i > 0; $i--) {
echo '<option value="' . $i . '"' . ($number_of_days == $i ? ' selected' : '') . '>' . ($i < 2 ? __('1 day', 'wpuerrorlogs') : sprintf(__('%s days', 'wpuerrorlogs'), $i)) . '</option>';
}
echo '</select>';
echo '</p>';

/* Search bar */
echo '<p>';
echo '<label>' . __('Search in errors', 'wpuerrorlogs') . '</label><br />';
echo '<input name="wpuerrorlogs_search" type="search" placeholder="' . __('Search', 'wpuerrorlogs') . '" value="' . htmlentities($search) . '" id="wpuerrorlogs_search" />';
echo '</p>';

submit_button(__('Filter results', 'wpuerrorlogs'));

echo '</details>';

/* Top errors */
$top_errors = $this->sort_errors_by_top($errors, 10);
Expand Down Expand Up @@ -150,6 +176,19 @@ public function page_content__main() {

}

function page_action__main() {
$new_url = $this->adminpages->get_page_url('main');
if (isset($_POST['number_of_days'])) {
$new_url = add_query_arg('number_of_days', urlencode($_POST['number_of_days']), $new_url);
}
if (isset($_POST['wpuerrorlogs_search'])) {
$new_url = add_query_arg('s', urlencode($_POST['wpuerrorlogs_search']), $new_url);
}

wp_redirect($new_url);
die;
}

/* ----------------------------------------------------------
Sort errors
---------------------------------------------------------- */
Expand Down Expand Up @@ -201,7 +240,30 @@ function prepare_errors_for_display($errors) {
Extract logs from file
---------------------------------------------------------- */

function get_logs($number_of_days) {
function get_logs($args = array()) {
$args = wp_parse_args($args, array(
'number_of_days' => 5,
'search_string' => ''
));

$excluded_strings = array();
$included_strings = array();

$args['search_string'] = str_replace('-"', '"-', $args['search_string']);
$search_parts = explode('"', $args['search_string']);
$search_parts = array_map(function ($a) {
return str_replace('"', '', $a);
}, $search_parts);
if (!empty($search_parts)) {
$search_parts = array_filter($search_parts);
foreach ($search_parts as $search_part) {
if (substr($search_part, 0, 1) == '-') {
$excluded_strings[] = trim(substr($search_part, 1));
} else {
$included_strings[] = trim($search_part);
}
}
}

$previous_files = array();

Expand All @@ -217,24 +279,32 @@ function get_logs($number_of_days) {
arsort($previous_files);
if (isset($previous_files[0])) {
$file = array_shift($previous_files);
$previous_files = array_slice($previous_files, 0, $number_of_days);
$previous_files = array_slice($previous_files, 0, $args['number_of_days']);
}
}
}
if (!is_readable($file)) {
return array();
}

$max_date = time() - 86400 * $number_of_days;
$max_date = time() - 86400 * $args['number_of_days'];

/* Parse errors in files */
$errors = $this->get_logs_from_file($file, $max_date);
$errors = $this->get_logs_from_file($file, array(
'max_date' => $max_date,
'included_strings' => $included_strings,
'excluded_strings' => $excluded_strings
));
if (empty($previous_files)) {
$previous_files = $this->find_previous_log_files($file, $number_of_days);
$previous_files = $this->find_previous_log_files($file, $args['number_of_days']);
}

foreach ($previous_files as $previous_file) {
$errors_previous = $this->get_logs_from_file($previous_file, $max_date);
$errors_previous = $this->get_logs_from_file($previous_file, array(
'max_date' => $max_date,
'included_strings' => $included_strings,
'excluded_strings' => $excluded_strings
));
foreach ($errors_previous as $error) {
$errors[] = $error;
}
Expand Down Expand Up @@ -262,7 +332,13 @@ function find_previous_log_files($file, $number_of_days = 5) {
return $previous_files;
}

function get_logs_from_file($file, $max_date) {
function get_logs_from_file($file, $args = array()) {

$args = wp_parse_args($args, array(
'max_date' => time(),
'included_strings' => array(),
'excluded_strings' => array()
));

$lines = array_reverse(file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
$errors = [];
Expand All @@ -285,21 +361,27 @@ function get_logs_from_file($file, $max_date) {
/* If date is not ok */
if (isset($matches[0])) {
$time = strtotime(str_replace(array('[', ']'), '', $matches[0]));
if ($time < $max_date) {
if ($time < $args['max_date']) {
break;
}
}

if ($currentError['date'] == 'none' && !empty($currentError['text'])) {
$line_error['text'] = array_merge($currentError['text'], $line_error['text']);
$line_error['text'] = $this->minimize_error_text(implode("\n", array_reverse($line_error['text'])));
$errors[] = $line_error;
$line_error = $this->filter_visible_error($line_error, $args);
if ($line_error) {
$errors[] = $line_error;
}
$currentError = $default_error;
continue;
}

$line_error['text'] = implode('', $line_error['text']);
$errors[] = $line_error;
$line_error = $this->filter_visible_error($line_error, $args);
if ($line_error) {
$errors[] = $line_error;
}

} else {
$currentError['text'][] = $line;
Expand All @@ -308,12 +390,34 @@ function get_logs_from_file($file, $max_date) {

if (!empty($currentError) && is_array($currentError['text']) && !empty($currentError['text'])) {
$currentError['text'] = $this->minimize_error_text(implode("\n", array_reverse($currentError['text'])));
$errors[] = $currentError;
$currentError = $this->filter_visible_error($currentError, $args);
if ($currentError) {
$errors[] = $currentError;
}
}

return $errors;
}

function filter_visible_error($error, $args) {
if (!empty($args['included_strings'])) {
foreach ($args['included_strings'] as $included_string) {
if (strpos($error['text'], $included_string) === false) {
return false;
}
}
}
if (!empty($args['excluded_strings'])) {
foreach ($args['excluded_strings'] as $excluded_string) {
if (strpos($error['text'], $excluded_string) !== false) {
return false;
}
}
}

return $error;
}

function get_error_from_line($line) {
/* Extract values */
$date_parts = explode(']', $line);
Expand Down

0 comments on commit c840d2a

Please sign in to comment.