ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
RotatingFileHandler.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jordi Boggiano <j.boggiano@seld.be>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Monolog\Handler;
13 
14 use Monolog\Logger;
15 
26 {
27  protected $filename;
28  protected $maxFiles;
29  protected $mustRotate;
30  protected $nextRotation;
31  protected $filenameFormat;
32  protected $dateFormat;
33 
42  public function __construct($filename, $maxFiles = 0, $level = Logger::DEBUG, $bubble = true, $filePermission = null, $useLocking = false)
43  {
44  $this->filename = $filename;
45  $this->maxFiles = (int) $maxFiles;
46  $this->nextRotation = new \DateTime('tomorrow');
47  $this->filenameFormat = '{filename}-{date}';
48  $this->dateFormat = 'Y-m-d';
49 
50  parent::__construct($this->getTimedFilename(), $level, $bubble, $filePermission, $useLocking);
51  }
52 
56  public function close()
57  {
58  parent::close();
59 
60  if (true === $this->mustRotate) {
61  $this->rotate();
62  }
63  }
64 
66  {
67  $this->filenameFormat = $filenameFormat;
68  $this->dateFormat = $dateFormat;
69  $this->url = $this->getTimedFilename();
70  $this->close();
71  }
72 
76  protected function write(array $record)
77  {
78  // on the first record written, if the log is new, we should rotate (once per day)
79  if (null === $this->mustRotate) {
80  $this->mustRotate = !file_exists($this->url);
81  }
82 
83  if ($this->nextRotation < $record['datetime']) {
84  $this->mustRotate = true;
85  $this->close();
86  }
87 
88  parent::write($record);
89  }
90 
94  protected function rotate()
95  {
96  // update filename
97  $this->url = $this->getTimedFilename();
98  $this->nextRotation = new \DateTime('tomorrow');
99 
100  // skip GC of old logs if files are unlimited
101  if (0 === $this->maxFiles) {
102  return;
103  }
104 
105  $logFiles = glob($this->getGlobPattern());
106  if ($this->maxFiles >= count($logFiles)) {
107  // no files to remove
108  return;
109  }
110 
111  // Sorting the files by name to remove the older ones
112  usort($logFiles, function ($a, $b) {
113  return strcmp($b, $a);
114  });
115 
116  foreach (array_slice($logFiles, $this->maxFiles) as $file) {
117  if (is_writable($file)) {
118  unlink($file);
119  }
120  }
121  }
122 
123  protected function getTimedFilename()
124  {
125  $fileInfo = pathinfo($this->filename);
126  $timedFilename = str_replace(
127  array('{filename}', '{date}'),
128  array($fileInfo['filename'], date($this->dateFormat)),
129  $fileInfo['dirname'] . '/' . $this->filenameFormat
130  );
131 
132  if (!empty($fileInfo['extension'])) {
133  $timedFilename .= '.'.$fileInfo['extension'];
134  }
135 
136  return $timedFilename;
137  }
138 
139  protected function getGlobPattern()
140  {
141  $fileInfo = pathinfo($this->filename);
142  $glob = str_replace(
143  array('{filename}', '{date}'),
144  array($fileInfo['filename'], '*'),
145  $fileInfo['dirname'] . '/' . $this->filenameFormat
146  );
147  if (!empty($fileInfo['extension'])) {
148  $glob .= '.'.$fileInfo['extension'];
149  }
150 
151  return $glob;
152  }
153 }
print $file
const DEBUG
Detailed debug information.
Definition: Logger.php:32
Stores logs to files that are rotated every day and a limited number of files are kept...
__construct($filename, $maxFiles=0, $level=Logger::DEBUG, $bubble=true, $filePermission=null, $useLocking=false)
Stores to any stream resource.
setFilenameFormat($filenameFormat, $dateFormat)