1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace yrc\components\log;
|
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerAwareInterface;
|
|
|
|
|
6
|
|
|
use Psr\Log\LoggerAwareTrait;
|
|
|
|
|
7
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
8
|
|
|
use Psr\Log\LogLevel;
|
|
|
|
|
9
|
|
|
use yii\base\InvalidConfigException;
|
|
|
|
|
10
|
|
|
use yii\helpers\VarDumper;
|
|
|
|
|
11
|
|
|
use yii\log\Logger;
|
|
|
|
|
12
|
|
|
use yii\log\Target;
|
|
|
|
|
13
|
|
|
|
14
|
|
|
final class PsrTarget extends Target
|
15
|
|
|
{
|
16
|
|
|
use LoggerAwareTrait;
|
17
|
|
|
|
18
|
|
|
private $_psrLevels = [
|
19
|
|
|
Logger::LEVEL_ERROR => LogLevel::ERROR,
|
20
|
|
|
Logger::LEVEL_WARNING => LogLevel::WARNING,
|
21
|
|
|
Logger::LEVEL_INFO => LogLevel::INFO,
|
22
|
|
|
Logger::LEVEL_TRACE => LogLevel::DEBUG,
|
23
|
|
|
Logger::LEVEL_PROFILE => LogLevel::DEBUG,
|
24
|
|
|
Logger::LEVEL_PROFILE_BEGIN => LogLevel::DEBUG,
|
25
|
|
|
Logger::LEVEL_PROFILE_END => LogLevel::DEBUG,
|
26
|
|
|
];
|
27
|
|
|
|
28
|
|
|
/**
|
29
|
|
|
* @return LoggerInterface
|
30
|
|
|
* @throws InvalidConfigException
|
31
|
|
|
*/
|
32
|
|
|
public function getLogger()
|
33
|
|
|
{
|
34
|
|
|
if ($this->logger === null) {
|
35
|
|
|
throw new InvalidConfigException('Logger should be configured with Psr\Log\LoggerInterface.');
|
36
|
|
|
}
|
37
|
|
|
return $this->logger;
|
38
|
|
|
}
|
39
|
|
|
|
40
|
|
|
/**
|
41
|
|
|
* @inheritdoc
|
42
|
|
|
*/
|
43
|
|
|
public function export()
|
44
|
|
|
{
|
45
|
|
|
foreach ($this->messages as $message) {
|
46
|
|
|
$context = [];
|
47
|
|
|
if (isset($message[4])) {
|
48
|
|
|
$context['trace'] = $message[4];
|
49
|
|
|
}
|
50
|
|
|
|
51
|
|
|
if (isset($message[5])) {
|
52
|
|
|
$context['memory'] = $message[5];
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
if (isset($message[2])) {
|
56
|
|
|
$context['category'] = $message[2];
|
57
|
|
|
}
|
58
|
|
|
|
59
|
|
|
$text = $message[0];
|
60
|
|
|
|
61
|
|
|
if (!is_string($text)) {
|
62
|
|
|
// exceptions may not be serializable if in the call stack somewhere is a Closure
|
63
|
|
|
if ($text instanceof \Throwable || $text instanceof \Exception) {
|
64
|
|
|
$text = (string)$text;
|
65
|
|
|
} elseif (\is_array($text)) {
|
66
|
|
|
$ctx = $text;
|
67
|
|
|
if (isset($ctx['message'])) {
|
68
|
|
|
$text = $ctx['message'];
|
69
|
|
|
unset($ctx['message']);
|
70
|
|
|
|
71
|
|
|
if (isset($ctx['exception'])) {
|
72
|
|
|
$e = $ctx['exception'];
|
73
|
|
|
unset($ctx['exception']);
|
74
|
|
|
$context['exception'] = [
|
75
|
|
|
'message' => $e->getMessage(),
|
76
|
|
|
'line' => $e->getLine(),
|
77
|
|
|
'file' => $e->getFile(),
|
78
|
|
|
'code' => $e->getCode(),
|
79
|
|
|
'trace' => $e->getTrace()
|
80
|
|
|
];
|
81
|
|
|
}
|
82
|
|
|
foreach ($ctx as $k => $v) {
|
83
|
|
|
$context[$k] = $v;
|
84
|
|
|
}
|
85
|
|
|
} else {
|
86
|
|
|
$text = VarDumper::export($text);
|
87
|
|
|
}
|
88
|
|
|
} else {
|
89
|
|
|
$text = VarDumper::export($text);
|
90
|
|
|
}
|
91
|
|
|
}
|
92
|
|
|
|
93
|
|
|
// If the user_id is not passed, dynamically set it from the user identity object
|
94
|
|
|
if (!isset($context['user_id'])) {
|
95
|
|
|
$context['user_id'] = Yii::$app->has('user') && Yii::$app->user->id != null ? Yii::$app->user->id : 'system';
|
|
|
|
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
// If the user_id of the event isn't the system, pre-load the policy number
|
99
|
|
|
if (!\in_array($context['user_id'], ['system', null])) {
|
100
|
|
|
if (Yii::$app->has('user') && Yii::$app->user->id) {
|
101
|
|
|
$model = Yii::$app->user->identity;
|
|
|
|
|
102
|
|
|
} else {
|
103
|
|
|
$model = Yii::$app->user->identityClass::find()->where(['id' => $context['user_id']])->one();
|
104
|
|
|
}
|
105
|
|
|
}
|
106
|
|
|
|
107
|
|
|
if (!isset($context['timestamp'])) {
|
108
|
|
|
$context['timestamp'] = \microtime(true);
|
109
|
|
|
}
|
110
|
|
|
|
111
|
|
|
$this->getLogger()->log($this->_psrLevels[$message[1]], $text, $context);
|
112
|
|
|
}
|
113
|
|
|
}
|
114
|
|
|
}
|
115
|
|
|
|
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