-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathRotateLog.php
175 lines (151 loc) · 3.96 KB
/
RotateLog.php
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace Robo\Task\Logfile;
use Robo\Result;
use Symfony\Component\Finder\Finder;
/**
* Rotates a log (or any other) file
*
* ``` php
* <?php
* $this->taskRotateLog(['logfile.log'])->run();
* // or use shortcut
* $this->_rotateLog(['logfile.log']);
*
* ?>
* ```
*/
class RotateLog extends BaseLogfile
{
/**
* @var int Number of copies to keep, default is 3.
*/
protected $keep = 3;
/**
* @var string|string[] Logfile to rotate.
*/
private $logfile;
/**
* @param string|string[] $logfiles
*/
public function __construct($logfiles)
{
parent::__construct($logfiles);
}
/**
* @param int $keep
* @return RotateLog
* @throws \Exception
*/
public function keep(int $keep): self
{
if ($keep < 1) {
throw new \InvalidArgumentException(
'Keep should be greater than one, to truncate a logfile use taskTruncateLog($logfile).'
);
}
$this->keep = $keep;
return $this;
}
/**
* {@inheritdoc}
*/
public function run(): Result
{
foreach ($this->logfiles as $logfile) {
$this->loadLogfile($logfile)
->process();
}
return Result::success($this);
}
/**
* @param string $logfile
* @return RotateLog
*/
private function loadLogfile(string $logfile): self
{
$this->logfile = new \SplFileInfo($logfile);
return $this;
}
/**
* @return RotateLog
*/
private function process(): self
{
$rotation = 0;
foreach ($this->createFinder() as $origin) {
if ($origin->isFile() && $this->isLogfile($origin)) {
if ($this->version($origin) < $this->keep) {
$rotated = $this->rotate($origin);
$this->printTaskInfo(
'Rotated from {origin} to {rotated}',
[
'origin' => $origin->getPathname(),
'rotated' => $rotated
]
);
} elseif ($this->version($origin) > $this->keep) {
$this->filesystem->remove($origin->getPathname());
}
}
$rotation++;
}
$this->filesystem->dumpFile($this->logfile->getPathname(), false);
if ($this->chmod) {
$this->filesystem->chmod($this->logfile->getPathname(), $this->chmod);
}
return $this;
}
/**
* @param \SplFileInfo $origin
* @return bool
*/
private function isLogfile(\SplFileInfo $origin): bool
{
if (substr($origin->getFilename(), 0, strlen($this->logfile->getFilename())) != $this->logfile->getFilename()) {
return false;
}
return true;
}
/**
* @param \SplFileInfo $origin
* @return int
*/
private function version(\SplFileInfo $origin): int
{
return $origin->getExtension() === $this->logfile->getExtension()
? 0
: $origin->getExtension();
}
/**
* @param \SplFileInfo $origin
* @return int
*/
private function next(\SplFileInfo $origin): int
{
return $this->version($origin) + 1;
}
/**
* @param \SplFileInfo $origin
* @return string
*/
private function rotate(\SplFileInfo $origin): string
{
$rotated = $this->logfile->getPathname().'.'.$this->next($origin);
if ($this->next($origin) === $this->keep) {
$this->filesystem->remove($rotated);
}
$this->filesystem->rename($origin->getPathname(), $rotated);
return $rotated;
}
/**
* @return Finder
*/
private function createFinder(): Finder
{
return (new Finder())->files()
->depth(0)
->sortByName()
->reverseSorting()
->in($this->logfile->getPath());
}
}