Skip to content

Commit

Permalink
Initial plugin commit v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Iwan Gurjanow committed Mar 8, 2023
1 parent 4fed9c8 commit 99e5f82
Show file tree
Hide file tree
Showing 18 changed files with 636 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
debug-log-monitoring.config.php
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/debug-log-monitoring.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions deactivation-hook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

function dlm_deactivate() {
dlm_log('Deactivating Debug Log Monitoring plugin');
$timestamp = wp_next_scheduled( 'dlm_cron_hook_process_log' );
wp_unschedule_event( $timestamp, 'dlm_cron_hook_process_log' );
wp_clear_scheduled_hook('dlm_cron_hook_process_log');
wp_unschedule_hook('dlm_cron_hook_process_log');
}
65 changes: 65 additions & 0 deletions debug-log-monitoring.config.example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

function dlm_getConfig(): array
{
return [
"appName" => "My awesome app",
"interval" => "24_hours",
"maxStoreAgeInDays" => 28,
"logsFolder" => "debug-logs",
"clearLog" => true,
"configVersion" => "1.0.0",
"notifications" => [
"enabled" => false,
"logFilters" => [
[
"filterKey" => "PHP Fatal error:",
"notificationString" => "%d PHP Fatal errors",
"emojis" => [
[
"threshold" => 10,
"slack" => ":surprised-pikachu:",
"email" => "😤"
],
[
"threshold" => 0,
"slack" => ":white_check_mark:",
"email" => "😎"
]
]
],
[
"filterKey" => "You have an error in your SQL syntax;",
"notificationString" => "%d SQL syntax errors",
"emojis" => [
[
"threshold" => 0,
"slack" => ":white_check_mark:",
"email" => "😎"
],
[
"threshold" => 10,
"slack" => ":pepesad:",
"email" => "😤"
]
]
]
],
"channels" => [
"slack" => [
"enabled" => false,
"webhookUrl" => ""
],
"email" => [
"enabled" => false,
"recipients" => [
"[email protected]",
],
"sender" => "Awesome App <[email protected]>",
"subject" => "My awesome App debug log report"
]
]
]
];
}

137 changes: 137 additions & 0 deletions debug-log-monitoring.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php
/**
Plugin Name: Debug Log Monitoring
Plugin URI: https://github.com/iwanmcm/debug-log-monitoring
Version: 1.0.0
Author: Dr. Iwan Gurjanow
Description: Splits the debug.log into daily files and clears the log file (stores 28 days by default). Optionally, a report can be send via Slack or E-Mail. Highly customizable.
Text Domain: dlm
*/

include_once('debug-log-monitoring.config.php');
include_once('utils/cron-schedules.php');
include_once('utils/log.php');
include_once('utils/message-builder.php');
include_once('deactivation-hook.php');

add_action( 'wp_mail_failed', 'onMailError', 10, 1 );
function onMailError( $wp_error ) {
dlm_log(print_r($wp_error, true), E_USER_ERROR);
}

add_action( 'dlm_cron_hook_process_log', 'dlm_cron_exec_process_log' );

function dlm_cron_exec_process_log() {
dlm_log('Start debug log monitoring procedure');
$logFile = WP_CONTENT_DIR . '/debug.log';
$config = dlm_getConfig();

if(!file_exists($logFile)) {
dlm_log('Stopping forcefully debug log monitoring procedure because no debug.log file available.');
return;
}

$debugLogsFolder = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . $config['logsFolder'];
if (!file_exists($debugLogsFolder)) {
mkdir($debugLogsFolder, 0700, true);
}

$logFileNew = $debugLogsFolder . '/' . date('Y-m-d_H:i') . '-debug.log';
// copy content of logfile to new file
copy($logFile, $logFileNew);
// delete content of logfile
if ($config['clearLog']) {
file_put_contents($logFile, '');
}

// remove log files older than x days
$files = glob($debugLogsFolder . '/*.log');
$now = time();
$maxAge = 60 * 60 * 24 * $config['maxStoreAgeInDays']; // 28 days
foreach ($files as $file) {
if (is_file($file)) {
if ($now - filemtime($file) >= $maxAge) {
unlink($file);
}
}
}

// Send slack message with summary
if ($config['notifications']['enabled']) {
dlm_doNotify($logFileNew);
}
else {
dlm_log('Skipping notification because notifications are disabled in config.');
}

dlm_log('End debug log procedure');
}

function dlm_doNotify(string $logFilePath): void {
$config = dlm_getConfig();
$channels = $config['notifications']['channels'];
$slackEnabled = $channels['slack']['enabled'];
$emailEnabled = $channels['email']['enabled'];
$isAnyChannelEnabled = $slackEnabled || $emailEnabled;
if (!$isAnyChannelEnabled) {
dlm_log('Skipping notification because no notification channel is enabled in config.');
return;
}

if ($slackEnabled) {
dlm_doNotifySlack($logFilePath);
}

if ($emailEnabled) {
dlm_doNotifyEmail($logFilePath);
}

dlm_log('Notification sent.');
}

function dlm_doNotifySlack(string $logFilePath): void {
dlm_log('Sending slack notification...');
$config = dlm_getConfig();
$slackChannel = $config['notifications']['channels']['slack'];
$message = dlm_buildMessage($logFilePath, 'slack');
$data = array(
'text' => $message,
);
$data = json_encode($data);
// json encode escapes automatically slashes, so we need to unescape them
$data = str_replace('\\\n', '\n', $data);
$curl = curl_init($slackChannel['webhookUrl']);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data)
));
$result = curl_exec($curl);
curl_close($curl);
dlm_log('End Slack Notification with log summary');
}

function dlm_doNotifyEmail(string $logFilePath): void {
dlm_log('Sending email notification...');
$config = dlm_getConfig();
$emailChannel = $config['notifications']['channels']['email'];
$message = dlm_buildMessage($logFilePath, 'email');
$sender = $emailChannel['sender'];
$headers = ["FROM: $sender", 'Content-Type: text/html; charset=UTF-8'];
$result = wp_mail($emailChannel['recipients'], $emailChannel['subject'], $message, $headers);
if (!$result) {
dlm_log('Error sending email notification.', E_USER_WARNING);
}
else {
dlm_log('End Email Notification with log summary');
}
}

if ( ! wp_next_scheduled( 'dlm_cron_hook_process_log' ) ) {
$config = dlm_getConfig();
wp_schedule_event( time(), $config["interval"], 'dlm_cron_hook_process_log' );
}

register_deactivation_hook( __FILE__, 'dlm_deactivate' );
Loading

0 comments on commit 99e5f82

Please sign in to comment.