FRED™  3.0
FRED™: Framework for Rapid and Easy Development
File.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred\Log\Handler;
4 
5 class File extends \Rsi\Fred\Log\Handler{
6 
7  public $format = null;
8  public $dateTimeFormat = 'Y-m-d H:i:s';
9  public $separator = "\n*****\n\n";
10  public $maxSize = 102400;
11 
12  protected $_filename = null;
13  protected $_lastInsertTime = null;
14 
15  protected function format($prio,$context){
16  $result = $this->separator;
17  foreach($context as $key => $value) if(strlen($result .= $key . ': ' . print_r($value,true) . "\n") > $this->maxSize){
18  $result .= "...\n";
19  break;
20  }
21  return $result;
22  }
23 
24  public function add($prio,$message,$context){
25  if($this->filename) try{
26  $context = [
27  '*date/time' => $this->_lastInsertTime = date($this->dateTimeFormat),
28  '*message' => $message,
29  '*prio' => $prio
30  ] + $context;
31  if(!\Rsi::commandLine()){
32  if($user_agent = $this->_log->component('client')->userAgent) $context = ['*user agent' => $user_agent] + $context;
33  $context = [
34  '*request' => $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . ' ' . $_SERVER['SERVER_PROTOCOL'],
35  '*remote addr' => $_SERVER['REMOTE_ADDR'],
36  '*started' => date($this->dateTimeFormat,$this->_log->fred->startTime)
37  ] + $context;
38  }
39  \Rsi\File::mkdir(\Rsi\File::dirname($this->filename));
40  \Rsi\File::write($this->filename,$this->format($prio,$context),0666,true);
41  }
42  catch(\Exception $e){
43  if($this->_log->fred->debug) throw $e;
44  }
45  }
46  /**
47  * Delete the file for the next period (when rotating logs).
48  */
49  public function deleteNext(){
50  if($this->format){
51  $time = time();
52  foreach([60,3600,86400] as $delta) if(($next = \Rsi\Str::replaceDate($this->format,$time + $delta)) != $this->filename){
53  \Rsi\File::unlink($next);
54  break;
55  }
56  }
57  }
58 
59  protected function getFilename(){
60  if($this->_filename === null){
61  $this->_filename = $this->_config['filename'] ?? null;
62  if(!$this->_filename) $this->_filename = \Rsi\Str::replaceDate($this->format);
63  }
64  return $this->_filename;
65  }
66 
67 }
deleteNext()
Delete the file for the next period (when rotating logs).
Definition: File.php:49
add($prio, $message, $context)
Definition: File.php:24
format($prio, $context)
Definition: File.php:15