1
|
|
|
<?php |
2
|
|
|
namespace nebula\component\debug\log\logger; |
3
|
|
|
|
4
|
|
|
use nebula\component\debug\log\logger\filelogger\AttachTrait; |
5
|
|
|
use nebula\component\debug\log\logger\filelogger\AttachAttribute; |
6
|
|
|
|
7
|
|
|
class FileLogger extends AbstractLogger |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
protected $config = [ |
10
|
|
|
'save-path' => './logs', |
11
|
|
|
'max-file-size' => 2097152, |
12
|
|
|
'file-name' => 'latest.log', |
13
|
|
|
'log-level' => 'debug', |
14
|
|
|
'log-format' => '%time-format% - %memory-format% [%level%] %file%:%line% %message%', |
15
|
|
|
'debug-path' => null, |
16
|
|
|
'start-time' => null, |
17
|
|
|
'start-memory' => null, |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
protected $file; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* 属性数组 |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $attribute; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* 附加数组 |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $attach; |
35
|
|
|
|
36
|
|
|
public function __construct(array $config=[], array $attribute=[]) |
37
|
|
|
{ |
38
|
|
|
$this->applyConfig($config); |
39
|
|
|
$this->attribute = $attribute; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
public function applyConfig(array $value) |
44
|
|
|
{ |
45
|
|
|
foreach ($config as $name => $value) { |
|
|
|
|
46
|
|
|
if (in_array($name, array_keys($defaultConfig))) { |
|
|
|
|
47
|
|
|
$this->config[$name] = $config[$name] ?? $this->defaultConfig[$name]; |
|
|
|
|
48
|
|
|
} else { |
49
|
|
|
throw new \Exception(sprintf(__CLASS__.' : unknown config property %s', $name)); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function mergeAttribute(array $value) |
55
|
|
|
{ |
56
|
|
|
$this->attribute = array_merge($this->attribute, $value); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function log($level, string $message, array $context = []) |
60
|
|
|
{ |
61
|
|
|
$this->attribute['level'] = $level; |
62
|
|
|
$this->analyse($context); |
63
|
|
|
$this->attribute['message'] = $this->interpolate($message, $context); |
64
|
|
|
$write = $this->interpolate($this->config['log-format'], $replace); |
|
|
|
|
65
|
|
|
foreach ($this->attach as $name => $value) { |
66
|
|
|
$write.=PHP_EOL.$name .'<' . get_class($val).'>'.PHP_EOL; |
|
|
|
|
67
|
|
|
if ($value instanceof AttachAttribute) { |
68
|
|
|
$write.= $value->getLogAttach().PHP_EOL; |
69
|
|
|
} elseif ($value instanceof \Exception) { |
70
|
|
|
$write.= AttachTrait::dumpException($value).PHP_EOL; |
71
|
|
|
} else { |
72
|
|
|
$write.= AttachTrait::parameterToString($value).PHP_EOL; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
// write file |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function analyse(array $context) |
79
|
|
|
{ |
80
|
|
|
$replace = []; |
81
|
|
|
foreach ($context as $key => $val) { |
82
|
|
|
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { |
83
|
|
|
$replace['{' . $key . '}'] = $val; |
84
|
|
|
} else { |
85
|
|
|
$this->attach[$key] = $val; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
return $replace; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function interpolate(string $message, array $context) |
92
|
|
|
{ |
93
|
|
|
$replace = []; |
94
|
|
|
foreach ($context as $key => $val) { |
95
|
|
|
$replace['{' . $key . '}'] = $val; |
96
|
|
|
} |
97
|
|
|
foreach ($this->attribute as $key => $val) { |
98
|
|
|
$replace['%' . $key . '%'] = $val; |
99
|
|
|
} |
100
|
|
|
return strtr($message, $replace); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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