PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/monolog/monolog/src/Monolog/Handler/RotatingFileHandler.php

https://bitbucket.org/gruenwaldt/loquitur-web
PHP | 124 lines | 69 code | 18 blank | 37 comment | 8 complexity | d3417c441326a457fb4ca52d920761f6 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\Logger;
  12. /**
  13. * Stores logs to files that are rotated every day and a limited number of files are kept.
  14. *
  15. * This rotation is only intended to be used as a workaround. Using logrotate to
  16. * handle the rotation is strongly encouraged when you can use it.
  17. *
  18. * @author Christophe Coevoet <stof@notk.org>
  19. * @author Jordi Boggiano <j.boggiano@seld.be>
  20. */
  21. class RotatingFileHandler extends StreamHandler
  22. {
  23. protected $filename;
  24. protected $maxFiles;
  25. protected $mustRotate;
  26. protected $nextRotation;
  27. /**
  28. * @param string $filename
  29. * @param integer $maxFiles The maximal amount of files to keep (0 means unlimited)
  30. * @param integer $level The minimum logging level at which this handler will be triggered
  31. * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
  32. */
  33. public function __construct($filename, $maxFiles = 0, $level = Logger::DEBUG, $bubble = true)
  34. {
  35. $this->filename = $filename;
  36. $this->maxFiles = (int) $maxFiles;
  37. $this->nextRotation = new \DateTime('tomorrow');
  38. parent::__construct($this->getTimedFilename(), $level, $bubble);
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function close()
  44. {
  45. parent::close();
  46. if (true === $this->mustRotate) {
  47. $this->rotate();
  48. }
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. protected function write(array $record)
  54. {
  55. // on the first record written, if the log is new, we should rotate (once per day)
  56. if (null === $this->mustRotate) {
  57. $this->mustRotate = !file_exists($this->url);
  58. }
  59. if ($this->nextRotation < $record['datetime']) {
  60. $this->mustRotate = true;
  61. $this->close();
  62. }
  63. parent::write($record);
  64. }
  65. /**
  66. * Rotates the files.
  67. */
  68. protected function rotate()
  69. {
  70. // update filename
  71. $this->url = $this->getTimedFilename();
  72. $this->nextRotation = new \DateTime('tomorrow');
  73. // skip GC of old logs if files are unlimited
  74. if (0 === $this->maxFiles) {
  75. return;
  76. }
  77. $fileInfo = pathinfo($this->filename);
  78. $glob = $fileInfo['dirname'].'/'.$fileInfo['filename'].'-*';
  79. if (!empty($fileInfo['extension'])) {
  80. $glob .= '.'.$fileInfo['extension'];
  81. }
  82. $logFiles = glob($glob);
  83. if ($this->maxFiles >= count($logFiles)) {
  84. // no files to remove
  85. return;
  86. }
  87. // Sorting the files by name to remove the older ones
  88. usort($logFiles, function($a, $b) {
  89. return strcmp($b, $a);
  90. });
  91. foreach (array_slice($logFiles, $this->maxFiles) as $file) {
  92. if (is_writable($file)) {
  93. unlink($file);
  94. }
  95. }
  96. }
  97. protected function getTimedFilename()
  98. {
  99. $fileInfo = pathinfo($this->filename);
  100. $timedFilename = $fileInfo['dirname'].'/'.$fileInfo['filename'].'-'.date('Y-m-d');
  101. if (!empty($fileInfo['extension'])) {
  102. $timedFilename .= '.'.$fileInfo['extension'];
  103. }
  104. return $timedFilename;
  105. }
  106. }