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