FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Stats.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred\Log\Handler;
4 
5 class Stats extends \Rsi\Fred\Log\Handler{
6 
7  public $filename = null; //!< File to store messages.
8  public $ttl = 3600; //!< Time to keep messages in memory.
9  public $charThreshold = 100; //!< Minimum number of charactes to match, or:
10  public $percThreshold = 75; //!< Minimum percentage to match.
11  public $threshold = 10; //!< Send mail after this number of messages within the TTL.
12  public $interval = 60; //!< Minimal mail interval (minutes).
13  public $from = null; //!< Sender (defaults to default From address).
14  public $to = null; //!< Send mail to this address (or array for multiple; defaults to default From address).
15  public $subject = 'Log stats *'; //!< Subject for the mail (an asterisk is replaced with the host name).
16  public $message = "Message '[message]' ([filename]@[lineNo]) occurred [threshold] times in the last [ttl] seconds"; //!< Mail
17  // message (tags [filename], [lineNo], [message], [threshold] and [ttl] are replaced with their values).
18 
19  protected function same($new_message,$old_message){
20  return (similar_text($new_message,$old_message,$perc) >= $this->charThreshold) || ($perc >= $this->percThreshold);
21  }
22 
23  protected function send($filename,$line_no,$message){
24  return $this->_log->component('mail')->send(
25  $this->from,
26  $this->to ?: ini_get('sendmail_from'),
27  str_replace('*',\Rsi\Http::host(),$this->subject),
28  strtr($this->message,[
29  '[filename]' => $filename ?: 'unknown',
30  '[lineNo]' => $line_no ?: 0,
31  '[message]' => $message,
32  '[threshold]' => $this->threshold,
33  '[ttl]' => $this->ttl
34  ]),
35  true
36  );
37  }
38 
39  public function add($prio,$message,$context){
40  if($this->filename) try{
41  $records = \Rsi\File::unserialize($this->filename,[]);
42  $limit = time() - $this->ttl;
43  foreach($records as $index => &$record){
44  $times = [];
45  foreach($record['times'] as $time) if($time > $limit) $times[] = $time;
46  if($times) $record['times'] = $times;
47  elseif(!$record['lastSend'] || ($record['lastSend'] < time() - $this->interval * 60)) unset($records[$index]);
48  }
49  unset($record);
50  $filename = $context['filename'] ?? null;
51  $line_no = $context['lineNo'] ?? null;
52  foreach($records as $index => &$record) if(
53  ($record['filename'] == $filename) &&
54  ($record['lineNo'] == $line_no) &&
55  $this->same($message,$record['message'])
56  ){
57  if(
58  (!$record['lastSend'] || ($record['lastSend'] < time() - $this->interval * 60)) &&
59  (count($record['times']) >= $this->threshold) &&
60  $this->send($filename,$line_no,$message)
61  ) $record = ['lastSend' => time(),'times' => []] + $record;
62  $record['times'][] = time();
63  $message = false;
64  break;
65  }
66  unset($record);
67  if($message) $records[] = ['filename' => $filename,'lineNo' => $line_no,'message' => $message,'lastSend' => null,'times' => [time()]];
68  \Rsi\File::serialize($temp = $this->filename . \Rsi\Str::random(),$records);
69  rename($temp,$this->filename);
70  }
71  catch(\Exception $e){
72  if($this->_log->fred->debug) throw $e;
73  }
74  }
75 
76 }
Rsi\Fred\Log\Handler\Stats\$charThreshold
$charThreshold
Minimum number of charactes to match, or:
Definition: Stats.php:9
Rsi\Fred\Log\Handler\Stats
Definition: Stats.php:5
Rsi\Fred\Log\Handler\Stats\$message
$message
Mail.
Definition: Stats.php:16
Rsi
Rsi\Fred\Log\Handler\Stats\send
send($filename, $line_no, $message)
Definition: Stats.php:23
Rsi\Fred\Log\Handler\Stats\$percThreshold
$percThreshold
Minimum percentage to match.
Definition: Stats.php:10
Rsi\Fred\Log\Handler\Stats\same
same($new_message, $old_message)
Definition: Stats.php:19
Rsi\Fred\Log\Handler\Stats\$ttl
$ttl
Time to keep messages in memory.
Definition: Stats.php:8
Rsi\Fred\Log\Handler\Stats\$threshold
$threshold
Send mail after this number of messages within the TTL.
Definition: Stats.php:11
Rsi\Fred\Log\Handler
Definition: Console.php:3
Rsi\Fred\Log\Handler\Stats\$to
$to
Send mail to this address (or array for multiple; defaults to default From address).
Definition: Stats.php:14
Rsi\Fred\Log\Handler\Stats\$interval
$interval
Minimal mail interval (minutes).
Definition: Stats.php:12
Rsi\Fred\Log\Handler\Stats\$subject
$subject
Subject for the mail (an asterisk is replaced with the host name).
Definition: Stats.php:15
Rsi\Fred\Log\Handler\Stats\$filename
$filename
File to store messages.
Definition: Stats.php:7
Rsi\Fred\Log\Handler\Stats\$from
$from
Sender (defaults to default From address).
Definition: Stats.php:13
Rsi\Fred\Log\Handler\Stats\add
add($prio, $message, $context)
Definition: Stats.php:39
Rsi\Fred\Exception
Definition: Exception.php:5