Blame view

sources/apps/files_external/3rdparty/aws-sdk-php/Monolog/Handler/RotatingFileHandler.php 3.5 KB
31b7f2792   Kload   Upgrade to ownclo...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  <?php
  
  /*
   * This file is part of the Monolog package.
   *
   * (c) Jordi Boggiano <j.boggiano@seld.be>
   *
   * For the full copyright and license information, please view the LICENSE
   * file that was distributed with this source code.
   */
  
  namespace Monolog\Handler;
  
  use Monolog\Logger;
  
  /**
   * 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@notk.org>
   * @author Jordi Boggiano <j.boggiano@seld.be>
   */
  class RotatingFileHandler extends StreamHandler
  {
      protected $filename;
      protected $maxFiles;
      protected $mustRotate;
      protected $nextRotation;
  
      /**
       * @param string  $filename
       * @param integer $maxFiles The maximal amount of files to keep (0 means unlimited)
       * @param integer $level    The minimum logging level at which this handler will be triggered
       * @param Boolean $bubble   Whether the messages that are handled can bubble up the stack or not
       */
      public function __construct($filename, $maxFiles = 0, $level = Logger::DEBUG, $bubble = true)
      {
          $this->filename = $filename;
          $this->maxFiles = (int) $maxFiles;
          $this->nextRotation = new \DateTime('tomorrow');
  
          parent::__construct($this->getTimedFilename(), $level, $bubble);
      }
  
      /**
       * {@inheritdoc}
       */
      public function close()
      {
          parent::close();
  
          if (true === $this->mustRotate) {
              $this->rotate();
          }
      }
  
      /**
       * {@inheritdoc}
       */
      protected function write(array $record)
      {
          // on the first record written, if the log is new, we should rotate (once per day)
          if (null === $this->mustRotate) {
              $this->mustRotate = !file_exists($this->url);
          }
  
          if ($this->nextRotation < $record['datetime']) {
              $this->mustRotate = true;
              $this->close();
          }
  
          parent::write($record);
      }
  
      /**
       * Rotates the files.
       */
      protected function rotate()
      {
          // update filename
          $this->url = $this->getTimedFilename();
          $this->nextRotation = new \DateTime('tomorrow');
  
          // skip GC of old logs if files are unlimited
          if (0 === $this->maxFiles) {
              return;
          }
  
          $fileInfo = pathinfo($this->filename);
          $glob = $fileInfo['dirname'].'/'.$fileInfo['filename'].'-*';
          if (!empty($fileInfo['extension'])) {
              $glob .= '.'.$fileInfo['extension'];
          }
          $iterator = new \GlobIterator($glob);
          $count = $iterator->count();
          if ($this->maxFiles >= $count) {
              // no files to remove
              return;
          }
  
          // Sorting the files by name to remove the older ones
          $array = iterator_to_array($iterator);
          usort($array, function($a, $b) {
              return strcmp($b->getFilename(), $a->getFilename());
          });
  
          foreach (array_slice($array, $this->maxFiles) as $file) {
              if ($file->isWritable()) {
                  unlink($file->getRealPath());
              }
          }
      }
  
      protected function getTimedFilename()
      {
          $fileInfo = pathinfo($this->filename);
          $timedFilename = $fileInfo['dirname'].'/'.$fileInfo['filename'].'-'.date('Y-m-d');
          if (!empty($fileInfo['extension'])) {
              $timedFilename .= '.'.$fileInfo['extension'];
          }
  
          return $timedFilename;
      }
  }