ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  const FILE_PER_DAY = 'Y-m-d';
28  const FILE_PER_MONTH = 'Y-m';
29  const FILE_PER_YEAR = 'Y';
30 
31  protected $filename;
32  protected $maxFiles;
33  protected $mustRotate;
34  protected $nextRotation;
35  protected $filenameFormat;
36  protected $dateFormat;
37 
46  public function __construct($filename, $maxFiles = 0, $level = Logger::DEBUG, $bubble = true, $filePermission = null, $useLocking = false)
47  {
48  $this->filename = $filename;
49  $this->maxFiles = (int) $maxFiles;
50  $this->nextRotation = new \DateTime('tomorrow');
51  $this->filenameFormat = '{filename}-{date}';
52  $this->dateFormat = 'Y-m-d';
53 
54  parent::__construct($this->getTimedFilename(), $level, $bubble, $filePermission, $useLocking);
55  }
56 
60  public function close()
61  {
62  parent::close();
63 
64  if (true === $this->mustRotate) {
65  $this->rotate();
66  }
67  }
68 
70  {
71  if (!preg_match('{^Y(([/_.-]?m)([/_.-]?d)?)?$}', $dateFormat)) {
72  trigger_error(
73  'Invalid date format - format must be one of '.
74  'RotatingFileHandler::FILE_PER_DAY ("Y-m-d"), RotatingFileHandler::FILE_PER_MONTH ("Y-m") '.
75  'or RotatingFileHandler::FILE_PER_YEAR ("Y"), or you can set one of the '.
76  'date formats using slashes, underscores and/or dots instead of dashes.',
77  E_USER_DEPRECATED
78  );
79  }
80  if (substr_count($filenameFormat, '{date}') === 0) {
81  trigger_error(
82  'Invalid filename format - format should contain at least `{date}`, because otherwise rotating is impossible.',
83  E_USER_DEPRECATED
84  );
85  }
86  $this->filenameFormat = $filenameFormat;
87  $this->dateFormat = $dateFormat;
88  $this->url = $this->getTimedFilename();
89  $this->close();
90  }
91 
95  protected function write(array $record)
96  {
97  // on the first record written, if the log is new, we should rotate (once per day)
98  if (null === $this->mustRotate) {
99  $this->mustRotate = !file_exists($this->url);
100  }
101 
102  if ($this->nextRotation < $record['datetime']) {
103  $this->mustRotate = true;
104  $this->close();
105  }
106 
107  parent::write($record);
108  }
109 
113  protected function rotate()
114  {
115  // update filename
116  $this->url = $this->getTimedFilename();
117  $this->nextRotation = new \DateTime('tomorrow');
118 
119  // skip GC of old logs if files are unlimited
120  if (0 === $this->maxFiles) {
121  return;
122  }
123 
124  $logFiles = glob($this->getGlobPattern());
125  if ($this->maxFiles >= count($logFiles)) {
126  // no files to remove
127  return;
128  }
129 
130  // Sorting the files by name to remove the older ones
131  usort($logFiles, function ($a, $b) {
132  return strcmp($b, $a);
133  });
134 
135  foreach (array_slice($logFiles, $this->maxFiles) as $file) {
136  if (is_writable($file)) {
137  // suppress errors here as unlink() might fail if two processes
138  // are cleaning up/rotating at the same time
139  set_error_handler(function ($errno, $errstr, $errfile, $errline) {});
140  unlink($file);
141  restore_error_handler();
142  }
143  }
144 
145  $this->mustRotate = false;
146  }
147 
148  protected function getTimedFilename()
149  {
150  $fileInfo = pathinfo($this->filename);
151  $timedFilename = str_replace(
152  array('{filename}', '{date}'),
153  array($fileInfo['filename'], date($this->dateFormat)),
154  $fileInfo['dirname'] . '/' . $this->filenameFormat
155  );
156 
157  if (!empty($fileInfo['extension'])) {
158  $timedFilename .= '.'.$fileInfo['extension'];
159  }
160 
161  return $timedFilename;
162  }
163 
164  protected function getGlobPattern()
165  {
166  $fileInfo = pathinfo($this->filename);
167  $glob = str_replace(
168  array('{filename}', '{date}'),
169  array($fileInfo['filename'], '*'),
170  $fileInfo['dirname'] . '/' . $this->filenameFormat
171  );
172  if (!empty($fileInfo['extension'])) {
173  $glob .= '.'.$fileInfo['extension'];
174  }
175 
176  return $glob;
177  }
178 }
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)
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
Create styles array
The data for the language used.
Stores to any stream resource.
setFilenameFormat($filenameFormat, $dateFormat)
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file