Skip to content

Commit

Permalink
v 0.1.1
Browse files Browse the repository at this point in the history
- Cleaner code.
- Add Github Workflows.
  • Loading branch information
Darklg committed Feb 19, 2024
1 parent 05ee0c5 commit b1fa077
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 30 deletions.
1 change: 1 addition & 0 deletions .github/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deny from all
19 changes: 19 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: PHP Lint

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
phplint:

runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: PHP Lint
uses: michaelw90/[email protected]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# WPU Error Logs

[![PHP workflow](https://github.com/WordPressUtilities/wpuerrorlogs/actions/workflows/php.yml/badge.svg 'PHP workflow')](https://github.com/WordPressUtilities/wpuerrorlogs/actions)

Make sense of your log files


Expand Down
1 change: 1 addition & 0 deletions uninstall.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
defined('ABSPATH') || die;
if (!defined('WP_UNINSTALL_PLUGIN')) {
die;
}
Expand Down
78 changes: 48 additions & 30 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.1.0
Version: 0.1.1
Author: Darklg
Author URI: https://github.com/Darklg
Text Domain: wpuerrorlogs
Expand All @@ -20,7 +20,7 @@
}

class WPUErrorLogs {
private $plugin_version = '0.1.0';
private $plugin_version = '0.1.1';
private $plugin_settings = array(
'id' => 'wpuerrorlogs',
'name' => 'WPU Error Logs'
Expand Down Expand Up @@ -132,52 +132,29 @@ public function wpuerrorlogs__cron_hook() {
public function page_content__main() {

/* Find debug file */
$logfile = ABSPATH . '/wp-content/debug.log';
if (!WP_DEBUG_LOG) {
echo 'Debug logs are not enabled';
return;
}
if (is_readable(WP_DEBUG_LOG)) {
$logfile = WP_DEBUG_LOG;
}

$errors = $this->extract_logs_from_file($logfile);

$textCounts = [];

foreach ($errors as $error) {
if (!isset($textCounts[$error['text']])) {
$textCounts[$error['text']] = 0;
}
$textCounts[$error['text']]++;
}
arsort($textCounts);
$errors = $this->extract_logs_from_file();

/* Keep only first five and extract data */
$textCounts = array_slice($textCounts, 0, 5, true);
$display_values = array();
foreach ($textCounts as $text => $count) {
$display_values[] = array(
'count' => $count,
'text' => $this->expand_error_text($text)
);
}

$colnames = array(
'count' => __('Count', 'wpuerrorlogs'),
'date' => __('Date', 'wpuerrorlogs'),
'type' => __('Type', 'wpuerrorlogs'),
'text' => __('Text', 'wpuerrorlogs')
);

$top_errors = $this->sort_errors_by_top($errors, 10);
echo '<h2>' . __('Top errors', 'wpuerrorlogs') . '</h2>';
echo $this->array_to_html_table($display_values, array(
echo $this->array_to_html_table($top_errors, array(
'table_classname' => 'widefat',
'colnames' => $colnames
));

$latest_errors = array_reverse($errors);
$latest_errors = array_slice($latest_errors, 0, 5, true);
$latest_errors = $this->sort_errors_by_latest($errors, 10);
echo '<h2>' . __('Latest errors', 'wpuerrorlogs') . '</h2>';
echo $this->array_to_html_table($latest_errors, array(
'table_classname' => 'widefat',
Expand All @@ -186,11 +163,52 @@ public function page_content__main() {

}

/* ----------------------------------------------------------
Sort errors
---------------------------------------------------------- */

function sort_errors_by_top($errors, $max_number = 5) {
$top_errors_raw = [];
foreach ($errors as $error) {
if (!isset($top_errors_raw[$error['text']])) {
$top_errors_raw[$error['text']] = 0;
}
$top_errors_raw[$error['text']]++;
}
arsort($top_errors_raw);

$top_errors_raw = array_slice($top_errors_raw, 0, $max_number, true);

$top_errors = array();
foreach ($top_errors_raw as $text => $count) {
$top_errors[] = array(
'count' => $count,
'text' => $this->expand_error_text($text)
);
}
return $top_errors;
}

function sort_errors_by_latest($errors, $max_number = 5) {
$latest_errors = array_reverse($errors);
$latest_errors = array_slice($latest_errors, 0, $max_number, true);
foreach ($latest_errors as $i => $error) {
$latest_errors[$i]['text'] = $this->expand_error_text($error['text']);
}
return $latest_errors;
}

/* ----------------------------------------------------------
Extract logs from file
---------------------------------------------------------- */

function extract_logs_from_file($file) {
function extract_logs_from_file() {

$file = ABSPATH . '/wp-content/debug.log';
if (is_readable(WP_DEBUG_LOG)) {
$file = WP_DEBUG_LOG;
}

$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$errors = [];
$currentError = array();
Expand Down

0 comments on commit b1fa077

Please sign in to comment.