Skip to content

Commit

Permalink
Update config and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
dani committed Jun 20, 2024
1 parent 1bed4bf commit a8b753e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/LogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function containsSpecificString($message)
// If the specific string is found, strpos() returns the position of the first occurrence
// of the specific string within the message. If the specific string is not found,
// strpos() returns false.
return strpos($message, $specificString) !== false;
return empty($specificString) ? true : strpos($message, $specificString) !== false;
}

/**
Expand Down Expand Up @@ -91,6 +91,9 @@ protected function logError(MessageLogged $event)
// Get the time in minutes to consider an error log as recent
$log_time_frame = config('log-alarm.log_time_frame');

// Get specified number of error logs in time frame
$log_per_time_frame = config('log-alarm.log_per_time_frame');

// Filter out logs that are older than the specified time frame
$errorLogs = array_filter($errorLogs, function ($timestamp) use ($log_time_frame) {
return $timestamp >= Carbon::now()->subMinutes($log_time_frame);
Expand All @@ -100,7 +103,7 @@ protected function logError(MessageLogged $event)
Cache::put($log_alarm_cache_key_enc, $errorLogs, Carbon::now()->addMinutes($log_time_frame));

// Check if there have been a specified number of error logs in time frame (in the last minute for example)
if (count($errorLogs) >= config('log-alarm.log_per_time_frame')) {
if (count($errorLogs) >= $log_per_time_frame) {

// Retrieve the time of the last notification from the cache or initialize null if no notification time exists
$last_notification_time = Cache::get($this->notification_cache_key.'_'.$log_alarm_cache_key_enc);
Expand All @@ -115,6 +118,8 @@ protected function logError(MessageLogged $event)
// Get the message to be sent in the notification from the config file
$message = empty(config('log-alarm.notification_message')) ? $log_message : config('log-alarm.notification_message');

$message = "The Error was occurred {$log_per_time_frame} times in the last {$log_time_frame} minutes: \r\n\r\n{$message}";

// Send the notification
NotificationService::send($message);

Expand Down
4 changes: 2 additions & 2 deletions src/config/log-alarm.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
'specific_string' => env('LA_SPECIFIC_STRING', ''), // also possible: 'table lock' or 'foo' or 'bar' or leave empty '' to enable any word

// notification message for log alarm
'notification_message' => env('LA_NOTIFICATION_MESSAGE', 'Log Alarm got triggered!'),
'notification_message' => env('LA_NOTIFICATION_MESSAGE', ''), // Leave empty '' to enable error log triggered alarm

// Slack webhook url for log alarm
'slack_webhook_url' => env('LA_SLACK_WEBHOOK_URL', ''),

// notification email address for log alarm
'notification_email' => env('LA_NOTIFICATION_EMAIL', '[email protected],[email protected]'),
'notification_email' => env('LA_NOTIFICATION_EMAIL', '[email protected]'), // also possible: '[email protected],[email protected]'

// notification email subject for log alarm
'notification_email_subject' => env('LA_NOTIFICATION_EMAIL_SUBJECT', 'Log Alarm Notification'),
Expand Down

0 comments on commit a8b753e

Please sign in to comment.