1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace suda\framework\debug\log\logger; |
4
|
|
|
|
5
|
|
|
use ZipArchive; |
6
|
|
|
use RecursiveIteratorIterator; |
7
|
|
|
use RecursiveDirectoryIterator; |
8
|
|
|
use suda\framework\debug\ConfigTrait; |
9
|
|
|
use suda\framework\debug\log\LogLevel; |
10
|
|
|
use suda\framework\debug\ConfigInterface; |
11
|
|
|
use suda\framework\filesystem\FileSystem; |
12
|
|
|
use suda\framework\debug\log\AbstractLogger; |
13
|
|
|
use suda\framework\debug\log\logger\exception\FileLoggerException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class FileLogger |
17
|
|
|
* @package suda\framework\debug\log\logger |
18
|
|
|
*/ |
19
|
|
|
class FileLogger extends FileLoggerBase |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* 构建文件日志 |
24
|
|
|
* |
25
|
|
|
* @param array $config |
26
|
|
|
*/ |
27
|
|
|
public function __construct(array $config = []) |
28
|
|
|
{ |
29
|
|
|
$this->set($config); |
30
|
|
|
register_shutdown_function([$this, 'shutdown']); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* 设置配置 |
35
|
|
|
* @param array $config |
36
|
|
|
*/ |
37
|
|
|
public function set(array $config) |
38
|
|
|
{ |
39
|
|
|
$this->applyConfig($config); |
40
|
|
|
FileSystem::make($this->getConfig('save-path')); |
41
|
|
|
FileSystem::make($this->getConfig('save-dump-path')); |
42
|
|
|
FileSystem::make($this->getConfig('save-zip-path')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $level |
47
|
|
|
* @param string $message |
48
|
|
|
* @param array $context |
49
|
|
|
* @return mixed|void |
50
|
|
|
* @throws FileLoggerException |
51
|
|
|
*/ |
52
|
|
|
public function log($level, string $message, array $context = []) |
53
|
|
|
{ |
54
|
|
|
if (LogLevel::compare($level, $this->getConfig('log-level')) >= 0) { |
55
|
|
|
$replace = []; |
56
|
|
|
$message = $this->interpolate($message, $context); |
57
|
|
|
$replace['%level%'] = $level; |
58
|
|
|
$replace['%message%'] = $message; |
59
|
|
|
$write = strtr($this->getConfig('log-format'), $replace); |
60
|
|
|
fwrite($this->getAvailableWrite(), $write . PHP_EOL); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* 即时写入日志 |
67
|
|
|
*/ |
68
|
|
|
public function write() |
69
|
|
|
{ |
70
|
|
|
if ($this->checkSize()) { |
71
|
|
|
$this->packLogFile(); |
72
|
|
|
$this->removePackFiles(); |
73
|
|
|
} |
74
|
|
|
$this->rollLatest(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* 程序关闭时调用 |
79
|
|
|
*/ |
80
|
|
|
public function shutdown() |
81
|
|
|
{ |
82
|
|
|
if (function_exists('fastcgi_finish_request')) { |
83
|
|
|
fastcgi_finish_request(); |
84
|
|
|
} |
85
|
|
|
$this->write(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $message |
90
|
|
|
* @param array $context |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function interpolate(string $message, array $context) |
94
|
|
|
{ |
95
|
|
|
$replace = []; |
96
|
|
|
foreach ($context as $key => $val) { |
97
|
|
|
if (is_bool($val)) { |
98
|
|
|
$val = $val ? 'true' : 'false'; |
99
|
|
|
} elseif (null === $val) { |
100
|
|
|
$val = 'null'; |
101
|
|
|
} |
102
|
|
|
$replace['{' . $key . '}'] = $val; |
103
|
|
|
} |
104
|
|
|
return strtr($message, $replace); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|