Skip to content

Commit

Permalink
v 0.3.0
Browse files Browse the repository at this point in the history
- Read last 6 days of logs.
- Fix previous log check.
- Better display for long errors.
- Various fixes.
  • Loading branch information
Darklg committed Feb 25, 2024
1 parent c18f51b commit e60f0c0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 27 deletions.
16 changes: 12 additions & 4 deletions inc/WPUBaseToolbox/WPUBaseToolbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/*
Class Name: WPU Base Toolbox
Description: Cool helpers for WordPress Plugins
Version: 0.12.0
Version: 0.12.1
Class URI: https://github.com/WordPressUtilities/wpubaseplugin
Author: Darklg
Author URI: https://darklg.me/
Expand All @@ -15,7 +15,7 @@
defined('ABSPATH') || die;

class WPUBaseToolbox {
private $plugin_version = '0.12.0';
private $plugin_version = '0.12.1';
public function __construct() {
add_action('wp_enqueue_scripts', array(&$this,
'form_scripts'
Expand Down Expand Up @@ -405,6 +405,8 @@ function array_to_html_table($array, $args = array()) {
/* Fix args */
$default_args = array(
'table_classname' => 'widefat',
'htmlspecialchars_td' => true,
'htmlspecialchars_th' => true,
'colnames' => array()
);
if (!is_array($args)) {
Expand All @@ -421,7 +423,10 @@ function array_to_html_table($array, $args = array()) {
if (isset($args['colnames'][$key])) {
$label = $args['colnames'][$key];
}
$html .= '<th>' . htmlspecialchars($label) . '</th>';
if ($args['htmlspecialchars_th']) {
$label = htmlspecialchars($label);
}
$html .= '<th>' . $label . '</th>';
}
$html .= '</tr></thead>';

Expand All @@ -430,7 +435,10 @@ function array_to_html_table($array, $args = array()) {
foreach ($array as $line) {
$html .= '<tr>';
foreach ($line as $value) {
$html .= '<td>' . htmlspecialchars($value) . '</td>';
if ($args['htmlspecialchars_td']) {
$value = htmlspecialchars($value);
}
$html .= '<td>' . $value . '</td>';
}
$html .= '</tr>';
}
Expand Down
Binary file modified lang/wpuerrorlogs-fr_FR.mo
Binary file not shown.
20 changes: 12 additions & 8 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-02-20 23:00+0700\n"
"POT-Creation-Date: 2024-02-25 21:31+0100\n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Language-Team: \n"
Expand All @@ -28,30 +28,34 @@ msgstr "Réglages"
msgid "Import Settings"
msgstr "Paramètres d'importation"

#: wpuerrorlogs.php:144
#: wpuerrorlogs.php:150
msgid "Count"
msgstr "Nombre"

#: wpuerrorlogs.php:145
#: wpuerrorlogs.php:151
msgid "Date"
msgstr "Date"

#: wpuerrorlogs.php:146
#: wpuerrorlogs.php:152
msgid "Type"
msgstr "Type"

#: wpuerrorlogs.php:147
#: wpuerrorlogs.php:153
msgid "Text"
msgstr "Texte"

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

#: wpuerrorlogs.php:157 wpuerrorlogs.php:166
#: wpuerrorlogs.php:164 wpuerrorlogs.php:174
msgid "No errors at the moment."
msgstr "Pas d’erreurs pour le moment."

#: wpuerrorlogs.php:161
#: wpuerrorlogs.php:168
msgid "Latest errors"
msgstr "Dernières erreurs"

#: wpuerrorlogs.php:338
msgid "Full error"
msgstr "Erreur complète"
62 changes: 47 additions & 15 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.2.1
Version: 0.3.0
Author: Darklg
Author URI: https://github.com/Darklg
Text Domain: wpuerrorlogs
Expand All @@ -20,7 +20,7 @@
}

class WPUErrorLogs {
private $plugin_version = '0.2.1';
private $plugin_version = '0.3.0';
private $plugin_settings = array(
'id' => 'wpuerrorlogs',
'name' => 'WPU Error Logs'
Expand Down Expand Up @@ -139,6 +139,12 @@ public function page_content__main() {

$errors = $this->get_logs();

/* Prepare for display */
$errors = array_map(function ($item) {
$item['text'] = $this->display_content_with_toggle($item['text']);
return $item;
}, $errors);

/* Keep only first five and extract data */
$colnames = array(
'count' => __('Count', 'wpuerrorlogs'),
Expand All @@ -151,7 +157,8 @@ public function page_content__main() {
$top_errors = $this->sort_errors_by_top($errors, 10);
echo '<h2>' . __('Top errors', 'wpuerrorlogs') . '</h2>';
$html_errors = $this->basetoolbox->array_to_html_table($top_errors, array(
'table_classname' => 'widefat',
'table_classname' => 'widefat striped',
'htmlspecialchars_td' => false,
'colnames' => $colnames
));
echo $html_errors ? $html_errors : '<p>' . __('No errors at the moment.', 'wpuerrorlogs') . '</p>';
Expand All @@ -160,7 +167,8 @@ public function page_content__main() {
$latest_errors = $this->sort_errors_by_latest($errors, 10);
echo '<h2>' . __('Latest errors', 'wpuerrorlogs') . '</h2>';
$html_errors = $this->basetoolbox->array_to_html_table($latest_errors, array(
'table_classname' => 'widefat',
'table_classname' => 'widefat striped',
'htmlspecialchars_td' => false,
'colnames' => $colnames
));
echo $html_errors ? $html_errors : '<p>' . __('No errors at the moment.', 'wpuerrorlogs') . '</p>';
Expand Down Expand Up @@ -216,29 +224,34 @@ function get_logs() {
}

$errors = $this->get_logs_from_file($file);
$previous_file = $this->find_previous_log_file($file);
if ($previous_file) {
$previous_files = $this->find_previous_log_files($file, 5);
foreach ($previous_files as $previous_file) {
$errors_previous = $this->get_logs_from_file($previous_file);
$errors = $errors + $errors_previous;
foreach ($errors_previous as $error) {
$errors[] = $error;
}
}
return $errors;
}

function find_previous_log_file($file) {
function find_previous_log_files($file, $number_of_days = 5) {
$date_formats = array('Ymd', 'dmY');
$previous_files = array();
foreach ($date_formats as $date_format) {
$now_date = date($date_format);
if (strpos($file, $now_date) === false) {
continue;
}
$previous_date = date($date_format, time() - 86400);
$previous_file = str_replace($now_date, $previous_date, $file);
if (is_readable($previous_file)) {
return $previous_file;
for ($i = 1; $i <= $number_of_days; $i++) {
$previous_date = date($date_format, time() - 86400 * $i);
$previous_file = str_replace($now_date, $previous_date, $file);
if (is_readable($previous_file)) {
$previous_files[] = $previous_file;
}
}
}

return false;
return $previous_files;
}

function get_logs_from_file($file) {
Expand Down Expand Up @@ -269,15 +282,14 @@ function get_logs_from_file($file) {
$currentError['text'] = $this->minimize_error_text($currentError['text']);
$errors[] = $currentError;
}

return $errors;
}

function get_error_from_line($line) {
/* Extract values */
$date_parts = explode(']', $line);
$date = str_replace('[', '', $date_parts[0]);
$text = trim(substr($line, strlen('[' . $date . ']'), -1));
$text = trim(substr($line, strlen('[' . $date . ']')));

/* Extract type */
$type = 'none';
Expand Down Expand Up @@ -310,6 +322,26 @@ function get_error_from_line($line) {
Helpers
---------------------------------------------------------- */

/* Display content
-------------------------- */

function display_content_with_toggle($content) {
$content = strip_tags($content);
if (strpos($content, "\n") === false) {
return $content;
}
$content_parts = explode("\n", $content);
if (!isset($content_parts[1])) {
return $content;
}
$content = $content_parts[0];
$content .= '<details><summary>' . __('Full error', 'wpuerrorlogs') . '</summary><pre style="overflow:auto">' . implode("\n", $content_parts) . '</pre></details>';
return $content;
}

/* Minimize text
-------------------------- */

function minimize_get_correspondances() {
return array(
'abs' => ABSPATH,
Expand Down

0 comments on commit e60f0c0

Please sign in to comment.