|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Deamon\LoggerExtraBundle\Processors\Monolog; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bridge\Monolog\Processor\WebProcessor as BaseWebProcessor; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\Container; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
8
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
9
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
10
|
|
|
|
|
11
|
|
|
class DeamonLoggerExtraWebProcessor extends BaseWebProcessor |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Container |
|
15
|
|
|
*/ |
|
16
|
|
|
private $container = null; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array|null |
|
20
|
|
|
*/ |
|
21
|
|
|
private $displayConfig; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $channelPrefix; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private $record; |
|
32
|
|
|
|
|
33
|
6 |
|
public function __construct($container = null, array $config = null) |
|
34
|
|
|
{ |
|
35
|
6 |
|
parent::__construct(); |
|
36
|
6 |
|
$this->container = $container; |
|
37
|
6 |
|
$this->channelPrefix = $config['channel_prefix']; |
|
38
|
6 |
|
$this->displayConfig = $config['display']; |
|
39
|
6 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $record |
|
43
|
|
|
* |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
6 |
|
public function __invoke(array $record) |
|
47
|
|
|
{ |
|
48
|
6 |
|
$this->record = parent::__invoke($record); |
|
49
|
|
|
|
|
50
|
6 |
|
if ($this->container === null) { |
|
51
|
1 |
|
return $this->record; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
5 |
|
$this->addContextInfo(); |
|
55
|
5 |
|
$this->addRequestInfo(); |
|
56
|
5 |
|
$this->addUserInfo(); |
|
57
|
5 |
|
$this->addChannelInfo(); |
|
58
|
|
|
|
|
59
|
5 |
|
return $this->record; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
public function setContainer($container) |
|
63
|
|
|
{ |
|
64
|
1 |
|
$this->container = $container; |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
5 |
|
private function addContextInfo() |
|
68
|
|
|
{ |
|
69
|
5 |
|
$this->addInfo('env', $this->container->get('kernel')->getEnvironment()); |
|
70
|
|
|
|
|
71
|
5 |
|
$context = $this->container->get('deamon.logger_extra.context'); |
|
72
|
|
|
|
|
73
|
5 |
|
$this->addInfo('locale', $context->getLocale()); |
|
74
|
5 |
|
if ($this->configShowExtraInfo('application_name')) { |
|
75
|
1 |
|
$this->record['extra']['application'] = $context->getApplicationName(); |
|
76
|
1 |
|
} |
|
77
|
5 |
|
} |
|
78
|
|
|
|
|
79
|
5 |
|
private function addRequestInfo() |
|
80
|
|
|
{ |
|
81
|
5 |
|
if (null !== $request_stack = $this->container->get('request_stack')) { |
|
82
|
5 |
|
$request = $request_stack->getCurrentRequest(); |
|
83
|
5 |
|
if ($request instanceof Request) { |
|
84
|
5 |
|
$this->addInfo('url', $request->getRequestUri()); |
|
85
|
5 |
|
$this->addInfo('route', $request->get('_route')); |
|
86
|
5 |
|
$this->addInfo('user_agent', $request->server->get('HTTP_USER_AGENT')); |
|
87
|
5 |
|
$this->addInfo('accept_encoding', $request->headers->get('Accept-Encoding')); |
|
88
|
5 |
|
$this->addInfo('client_ip', $request->getClientIp()); |
|
89
|
|
|
|
|
90
|
5 |
|
} |
|
91
|
5 |
|
} |
|
92
|
5 |
|
} |
|
93
|
|
|
|
|
94
|
5 |
|
private function addUserInfo() |
|
95
|
|
|
{ |
|
96
|
5 |
|
if ($this->configShowExtraInfo('user')) { |
|
97
|
1 |
|
$token = $this->container->get('security.token_storage')->getToken(); |
|
98
|
1 |
|
if (($token instanceof TokenInterface) && ($token->getUser() instanceof UserInterface) && null !== $user = $token->getUser()) { |
|
99
|
1 |
|
if ($this->configShowExtraInfo('user_id') && method_exists($user, 'getId')) { |
|
100
|
1 |
|
$this->record['extra']['user_id'] = $user->getId(); |
|
|
|
|
|
|
101
|
1 |
|
} |
|
102
|
1 |
|
if ($this->configShowExtraInfo('user_email') && method_exists($user, 'getEmail')) { |
|
103
|
1 |
|
$this->record['extra']['user_email'] = $user->getEmail(); |
|
|
|
|
|
|
104
|
1 |
|
} |
|
105
|
1 |
|
if ($this->configShowExtraInfo('user_name') && method_exists($user, 'getUsername')) { |
|
106
|
1 |
|
$this->record['extra']['user_name'] = $user->getUsername(); |
|
107
|
1 |
|
} |
|
108
|
1 |
|
} |
|
109
|
1 |
|
} |
|
110
|
5 |
|
} |
|
111
|
|
|
|
|
112
|
5 |
|
private function addChannelInfo() |
|
113
|
|
|
{ |
|
114
|
5 |
|
$this->addInfo('global_channel', $this->record['channel']); |
|
115
|
|
|
|
|
116
|
5 |
|
if ($this->channelPrefix !== null) { |
|
117
|
1 |
|
$this->record['channel'] = sprintf('%s.%s', $this->channelPrefix, $this->record['channel']); |
|
118
|
1 |
|
} |
|
119
|
5 |
|
} |
|
120
|
|
|
|
|
121
|
5 |
|
private function addInfo($key, $value) |
|
122
|
|
|
{ |
|
123
|
5 |
|
if ($this->configShowExtraInfo($key)) { |
|
124
|
4 |
|
$this->record['extra'][$key] = $value; |
|
125
|
4 |
|
} |
|
126
|
5 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param string $extraInfo |
|
130
|
|
|
* |
|
131
|
|
|
* @return bool |
|
132
|
|
|
*/ |
|
133
|
5 |
|
private function configShowExtraInfo($extraInfo) |
|
134
|
|
|
{ |
|
135
|
5 |
|
return isset($this->displayConfig[$extraInfo]) && $this->displayConfig[$extraInfo]; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.