1
|
|
|
<?php |
2
|
|
|
namespace nebula\component\debug\log\logger; |
3
|
|
|
|
4
|
|
|
use nebula\component\debug\log\LogLevel; |
5
|
|
|
use nebula\component\debug\log\AbstractLogger; |
6
|
|
|
use nebula\component\debug\log\logger\filelogger\AttachTrait; |
|
|
|
|
7
|
|
|
use nebula\component\debug\log\logger\filelogger\AttachAttribute; |
|
|
|
|
8
|
|
|
|
9
|
|
|
class FileLogger extends AbstractLogger |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* 配置信息 |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $config = [ |
19
|
|
|
'save-path' => './logs', |
20
|
|
|
'save-zip-path' => './logs/zip', |
21
|
|
|
'save-pack-path' => './logs/dump', |
22
|
|
|
'max-file-size' => 2097152, |
23
|
|
|
'file-name' => 'latest.log', |
24
|
|
|
'log-level' => 'debug', |
25
|
|
|
'log-format' => '[%level%] %message%', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* 文件 |
30
|
|
|
* |
31
|
|
|
* @var resource |
32
|
|
|
*/ |
33
|
|
|
protected $temp; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* 临时文件名 |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $tempname; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* 移除文件 |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $removeFiles = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* 最后的日志 |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $latest; |
55
|
|
|
|
56
|
|
|
public function __construct(array $config=[]) |
57
|
|
|
{ |
58
|
|
|
$this->applyConfig($config); |
59
|
|
|
$this->temp = tmpfile(); |
|
|
|
|
60
|
|
|
if ($this->temp === false) { |
61
|
|
|
$this->tempname = $this->config['save-path'].'/log-'. microtime(true).'.tmp'; |
62
|
|
|
$this->temp = fopen($this->tempname, 'w+'); |
63
|
|
|
} |
64
|
|
|
$this->latest = $this->config['save-path'].'/'.$this->config['file-name']; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function packLogFile() |
68
|
|
|
{ |
69
|
|
|
$logFile= $this->latest; |
70
|
|
|
$path=preg_replace('/[\\\\]+/', '/', $this->config['save-zip-path'] .'/'.date('Y-m-d').'.zip'); |
71
|
|
|
$zip = new ZipArchive; |
|
|
|
|
72
|
|
|
$res = $zip->open($path, ZipArchive::CREATE); |
73
|
|
|
if ($res === true) { |
74
|
|
|
if ($zip->addFile($logFile, date('Y-m-d'). '-'. $zip->numFiles .'.log')) { |
75
|
|
|
array_push($this->removeFiles, $logFile); |
76
|
|
|
} |
77
|
|
|
$it = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->config['save-pack-path'])); |
78
|
|
|
foreach ($it as $dumpLog) { |
79
|
|
|
if ($zip->addFile($dumpLog, 'pack/'.basename($dumpLog))) { |
80
|
|
|
array_push($this->removeFiles, $dumpLog); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
$zip->close(); |
84
|
|
|
} else { |
85
|
|
|
if (is_file($logFile) && file_exists($logFile)) { |
86
|
|
|
rename($logFile, $this->config['save-path'] . '/' . date('Y-m-d'). '-'. substr(md5_file($logFile), 0, 8).'.log'); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function applyConfig(array $config) |
92
|
|
|
{ |
93
|
|
|
foreach ($config as $name => $value) { |
94
|
|
|
if (in_array($name, array_keys($this->config))) { |
95
|
|
|
$this->config[$name] = $config[$name] ?? $this->config[$name]; |
96
|
|
|
} else { |
97
|
|
|
throw new \Exception(sprintf(__CLASS__.' : unknown config property %s', $name)); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* 检查日志文件大小 |
104
|
|
|
* |
105
|
|
|
* @return boolean |
106
|
|
|
*/ |
107
|
|
|
protected function checkSize():bool |
108
|
|
|
{ |
109
|
|
|
$logFile= $this->latest; |
110
|
|
|
if (file_exists($logFile)) { |
111
|
|
|
if (filesize($logFile) > $this->config['max-file-size']) { |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
public function log($level, string $message, array $context = []) |
120
|
|
|
{ |
121
|
|
|
if (LogLevel::compare($level, $this->config['log-level']) >=0) { |
122
|
|
|
$replace = []; |
123
|
|
|
$message = $this->interpolate($message, $context); |
124
|
|
|
$replace['%level%'] = $level; |
125
|
|
|
$replace['%message%'] = $message; |
126
|
|
|
$write = \strtr($this->config['log-format'], $replace); |
127
|
|
|
\fwrite($this->temp, $write.PHP_EOL); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
public function interpolate(string $message, array $context) |
133
|
|
|
{ |
134
|
|
|
$replace = []; |
135
|
|
|
foreach ($context as $key => $val) { |
136
|
|
|
$replace['{' . $key . '}'] = $val; |
137
|
|
|
} |
138
|
|
|
return strtr($message, $replace); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function save() |
142
|
|
|
{ |
143
|
|
|
if ($this->checkSize()) { |
144
|
|
|
$this->packLogFile(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (isset($this->latest)) { |
148
|
|
|
$size=ftell($this->temp); |
149
|
|
|
fseek($this->temp, 0); |
150
|
|
|
if ($size > 0) { |
151
|
|
|
$body=fread($this->temp, $size); |
152
|
|
|
file_put_contents($this->latest, $body, FILE_APPEND); |
153
|
|
|
} |
154
|
|
|
fclose($this->temp); |
155
|
|
|
if (isset($this->tempname)) { |
156
|
|
|
@unlink($this->tempname); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
foreach ($this->removeFiles as $file) { |
161
|
|
|
if (\is_file($file) && \file_exists($file)) { |
162
|
|
|
@unlink($file); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths