ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Monolog\Handler\RotatingFileHandler Class Reference

Stores logs to files that are rotated every day and a limited number of files are kept. More...

+ Inheritance diagram for Monolog\Handler\RotatingFileHandler:
+ Collaboration diagram for Monolog\Handler\RotatingFileHandler:

Public Member Functions

 __construct ($filename, $maxFiles=0, $level=Logger::DEBUG, $bubble=true, $filePermission=null, $useLocking=false)
 
 close ()
 {} More...
 
 reset ()
 {} More...
 
 setFilenameFormat ($filenameFormat, $dateFormat)
 
- Public Member Functions inherited from Monolog\Handler\StreamHandler
 __construct ($stream, $level=Logger::DEBUG, $bubble=true, $filePermission=null, $useLocking=false)
 
 close ()
 {} More...
 
 getStream ()
 Return the currently active stream if it is open. More...
 
 getUrl ()
 Return the stream URL if it was configured with a URL and not an active resource. More...
 
- Public Member Functions inherited from Monolog\Handler\AbstractProcessingHandler
 handle (array $record)
 {Handles a record.All records may be passed to this method, and the handler should discard those that it does not want to handle.The return value of this function controls the bubbling process of the handler stack. Unless the bubbling is interrupted (by returning true), the Logger class will keep on calling further handlers in the stack with a given log record.
Parameters
array$recordThe record to handle
Returns
bool true means that this handler handled the record, and that bubbling is not permitted. false means the record was either not processed or that this handler allows bubbling.
} More...
 
- Public Member Functions inherited from Monolog\Handler\AbstractHandler
 __construct ($level=Logger::DEBUG, $bubble=true)
 
 isHandling (array $record)
 {Checks whether the given record will be handled by this handler.This is mostly done for performance reasons, to avoid calling processors for nothing.Handlers should still check the record levels within handle(), returning false in isHandling() is no guarantee that handle() will not be called, and isHandling() might not be called for a given record.
Parameters
array$recordPartial log record containing only a level key
Returns
bool
} More...
 
 handleBatch (array $records)
 {Handles a set of records at once.
Parameters
array$recordsThe records to handle (an array of record arrays)
} More...
 
 close ()
 Closes the handler. More...
 
 pushProcessor ($callback)
 {Adds a processor in the stack.
Parameters
callable$callback
Returns
self
} More...
 
 popProcessor ()
 {Removes the processor on top of the stack and returns it.
Returns
callable
} More...
 
 setFormatter (FormatterInterface $formatter)
 {Sets the formatter.
Parameters
FormatterInterface$formatter
Returns
self
} More...
 
 getFormatter ()
 {Gets the formatter.
Returns
FormatterInterface
} More...
 
 setLevel ($level)
 Sets minimum logging level at which this handler will be triggered. More...
 
 getLevel ()
 Gets minimum logging level at which this handler will be triggered. More...
 
 setBubble ($bubble)
 Sets the bubbling behavior. More...
 
 getBubble ()
 Gets the bubbling behavior. More...
 
 __destruct ()
 
 reset ()
 

Data Fields

const FILE_PER_DAY = 'Y-m-d'
 
const FILE_PER_MONTH = 'Y-m'
 
const FILE_PER_YEAR = 'Y'
 

Protected Member Functions

 write (array $record)
 {} More...
 
 rotate ()
 Rotates the files. More...
 
 getTimedFilename ()
 
 getGlobPattern ()
 
- Protected Member Functions inherited from Monolog\Handler\StreamHandler
 write (array $record)
 {} More...
 
 streamWrite ($stream, array $record)
 Write to stream. More...
 
- Protected Member Functions inherited from Monolog\Handler\AbstractProcessingHandler
 write (array $record)
 Writes the record down to the log of the implementing handler. More...
 
 processRecord (array $record)
 Processes a record. More...
 
- Protected Member Functions inherited from Monolog\Handler\AbstractHandler
 getDefaultFormatter ()
 Gets the default formatter. More...
 

Protected Attributes

 $filename
 
 $maxFiles
 
 $mustRotate
 
 $nextRotation
 
 $filenameFormat
 
 $dateFormat
 
- Protected Attributes inherited from Monolog\Handler\StreamHandler
 $stream
 
 $url
 
 $filePermission
 
 $useLocking
 
- Protected Attributes inherited from Monolog\Handler\AbstractHandler
 $level = Logger::DEBUG
 
 $bubble = true
 
 $formatter
 
 $processors = array()
 

Detailed Description

Stores logs to files that are rotated every day and a limited number of files are kept.

This rotation is only intended to be used as a workaround. Using logrotate to handle the rotation is strongly encouraged when you can use it.

Author
Christophe Coevoet stof@.nosp@m.notk.nosp@m..org
Jordi Boggiano j.bog.nosp@m.gian.nosp@m.o@sel.nosp@m.d.be

Definition at line 25 of file RotatingFileHandler.php.

Constructor & Destructor Documentation

◆ __construct()

Monolog\Handler\RotatingFileHandler::__construct (   $filename,
  $maxFiles = 0,
  $level = Logger::DEBUG,
  $bubble = true,
  $filePermission = null,
  $useLocking = false 
)
Parameters
string$filename
int$maxFilesThe maximal amount of files to keep (0 means unlimited)
int$levelThe minimum logging level at which this handler will be triggered
bool$bubbleWhether the messages that are handled can bubble up the stack or not
int | null$filePermissionOptional file permissions (default (0644) are only for owner read/write)
bool$useLockingTry to lock log file before doing any writes

Definition at line 46 of file RotatingFileHandler.php.

References Monolog\Handler\AbstractHandler\$bubble, Monolog\Handler\RotatingFileHandler\$filename, Monolog\Handler\StreamHandler\$filePermission, Monolog\Handler\AbstractHandler\$level, Monolog\Handler\RotatingFileHandler\$maxFiles, Monolog\Handler\StreamHandler\$useLocking, and Monolog\Handler\RotatingFileHandler\getTimedFilename().

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  }
+ Here is the call graph for this function:

Member Function Documentation

◆ close()

Monolog\Handler\RotatingFileHandler::close ( )

{}

Definition at line 60 of file RotatingFileHandler.php.

References Monolog\Handler\RotatingFileHandler\rotate().

Referenced by Monolog\Handler\RotatingFileHandler\setFilenameFormat(), and Monolog\Handler\RotatingFileHandler\write().

61  {
62  parent::close();
63 
64  if (true === $this->mustRotate) {
65  $this->rotate();
66  }
67  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getGlobPattern()

Monolog\Handler\RotatingFileHandler::getGlobPattern ( )
protected

Definition at line 176 of file RotatingFileHandler.php.

Referenced by Monolog\Handler\RotatingFileHandler\rotate().

177  {
178  $fileInfo = pathinfo($this->filename);
179  $glob = str_replace(
180  array('{filename}', '{date}'),
181  array($fileInfo['filename'], '[0-9][0-9][0-9][0-9]*'),
182  $fileInfo['dirname'] . '/' . $this->filenameFormat
183  );
184  if (!empty($fileInfo['extension'])) {
185  $glob .= '.'.$fileInfo['extension'];
186  }
187 
188  return $glob;
189  }
+ Here is the caller graph for this function:

◆ getTimedFilename()

Monolog\Handler\RotatingFileHandler::getTimedFilename ( )
protected

Definition at line 160 of file RotatingFileHandler.php.

Referenced by Monolog\Handler\RotatingFileHandler\__construct(), Monolog\Handler\RotatingFileHandler\rotate(), and Monolog\Handler\RotatingFileHandler\setFilenameFormat().

161  {
162  $fileInfo = pathinfo($this->filename);
163  $timedFilename = str_replace(
164  array('{filename}', '{date}'),
165  array($fileInfo['filename'], date($this->dateFormat)),
166  $fileInfo['dirname'] . '/' . $this->filenameFormat
167  );
168 
169  if (!empty($fileInfo['extension'])) {
170  $timedFilename .= '.'.$fileInfo['extension'];
171  }
172 
173  return $timedFilename;
174  }
+ Here is the caller graph for this function:

◆ reset()

Monolog\Handler\RotatingFileHandler::reset ( )

{}

Implements Monolog\ResettableInterface.

Definition at line 72 of file RotatingFileHandler.php.

References Monolog\Handler\RotatingFileHandler\rotate().

73  {
74  parent::reset();
75 
76  if (true === $this->mustRotate) {
77  $this->rotate();
78  }
79  }
+ Here is the call graph for this function:

◆ rotate()

Monolog\Handler\RotatingFileHandler::rotate ( )
protected

Rotates the files.

Definition at line 125 of file RotatingFileHandler.php.

References Monolog\Handler\RotatingFileHandler\getGlobPattern(), and Monolog\Handler\RotatingFileHandler\getTimedFilename().

Referenced by Monolog\Handler\RotatingFileHandler\close(), and Monolog\Handler\RotatingFileHandler\reset().

126  {
127  // update filename
128  $this->url = $this->getTimedFilename();
129  $this->nextRotation = new \DateTime('tomorrow');
130 
131  // skip GC of old logs if files are unlimited
132  if (0 === $this->maxFiles) {
133  return;
134  }
135 
136  $logFiles = glob($this->getGlobPattern());
137  if ($this->maxFiles >= count($logFiles)) {
138  // no files to remove
139  return;
140  }
141 
142  // Sorting the files by name to remove the older ones
143  usort($logFiles, function ($a, $b) {
144  return strcmp($b, $a);
145  });
146 
147  foreach (array_slice($logFiles, $this->maxFiles) as $file) {
148  if (is_writable($file)) {
149  // suppress errors here as unlink() might fail if two processes
150  // are cleaning up/rotating at the same time
151  set_error_handler(function ($errno, $errstr, $errfile, $errline) {});
152  unlink($file);
153  restore_error_handler();
154  }
155  }
156 
157  $this->mustRotate = false;
158  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setFilenameFormat()

Monolog\Handler\RotatingFileHandler::setFilenameFormat (   $filenameFormat,
  $dateFormat 
)

Definition at line 81 of file RotatingFileHandler.php.

References Monolog\Handler\RotatingFileHandler\$dateFormat, Monolog\Handler\RotatingFileHandler\$filenameFormat, Monolog\Handler\RotatingFileHandler\close(), and Monolog\Handler\RotatingFileHandler\getTimedFilename().

82  {
83  if (!preg_match('{^Y(([/_.-]?m)([/_.-]?d)?)?$}', $dateFormat)) {
84  trigger_error(
85  'Invalid date format - format must be one of '.
86  'RotatingFileHandler::FILE_PER_DAY ("Y-m-d"), RotatingFileHandler::FILE_PER_MONTH ("Y-m") '.
87  'or RotatingFileHandler::FILE_PER_YEAR ("Y"), or you can set one of the '.
88  'date formats using slashes, underscores and/or dots instead of dashes.',
89  E_USER_DEPRECATED
90  );
91  }
92  if (substr_count($filenameFormat, '{date}') === 0) {
93  trigger_error(
94  'Invalid filename format - format should contain at least `{date}`, because otherwise rotating is impossible.',
95  E_USER_DEPRECATED
96  );
97  }
98  $this->filenameFormat = $filenameFormat;
99  $this->dateFormat = $dateFormat;
100  $this->url = $this->getTimedFilename();
101  $this->close();
102  }
+ Here is the call graph for this function:

◆ write()

Monolog\Handler\RotatingFileHandler::write ( array  $record)
protected

{}

Definition at line 107 of file RotatingFileHandler.php.

References Monolog\Handler\RotatingFileHandler\close(), and Sabre\VObject\write().

108  {
109  // on the first record written, if the log is new, we should rotate (once per day)
110  if (null === $this->mustRotate) {
111  $this->mustRotate = !file_exists($this->url);
112  }
113 
114  if ($this->nextRotation < $record['datetime']) {
115  $this->mustRotate = true;
116  $this->close();
117  }
118 
119  parent::write($record);
120  }
+ Here is the call graph for this function:

Field Documentation

◆ $dateFormat

Monolog\Handler\RotatingFileHandler::$dateFormat
protected

◆ $filename

Monolog\Handler\RotatingFileHandler::$filename
protected

◆ $filenameFormat

Monolog\Handler\RotatingFileHandler::$filenameFormat
protected

◆ $maxFiles

Monolog\Handler\RotatingFileHandler::$maxFiles
protected

◆ $mustRotate

Monolog\Handler\RotatingFileHandler::$mustRotate
protected

Definition at line 33 of file RotatingFileHandler.php.

◆ $nextRotation

Monolog\Handler\RotatingFileHandler::$nextRotation
protected

Definition at line 34 of file RotatingFileHandler.php.

◆ FILE_PER_DAY

◆ FILE_PER_MONTH

◆ FILE_PER_YEAR


The documentation for this class was generated from the following file: