-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
DefaultLogger.php
164 lines (150 loc) · 4.96 KB
/
DefaultLogger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Neos\Flow\Log;
/*
* This file is part of the Neos.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Log\Exception\NoSuchBackendException;
/**
* The default logger of the Flow framework
*
* @api
*/
class DefaultLogger implements LoggerInterface
{
/**
* @var \SplObjectStorage
*/
protected $backends;
/**
* @var \closure
*/
protected $requestInfoCallback = null;
/**
* @var \closure
*/
protected $renderBacktraceCallback = null;
/**
* Constructs the logger
*
*/
public function __construct()
{
$this->backends = new \SplObjectStorage();
}
/**
* @param \Closure $closure
*/
public function setRequestInfoCallback(\Closure $closure)
{
$this->requestInfoCallback = $closure;
}
/**
* @param \Closure $closure
*/
public function setRenderBacktraceCallback(\Closure $closure)
{
$this->renderBacktraceCallback = $closure;
}
/**
* Sets the given backend as the only backend for this Logger.
*
* This method allows for conveniently injecting a backend through some Objects.yaml configuration.
*
* @param Backend\BackendInterface $backend A backend implementation
* @return void
* @api
*/
public function setBackend(Backend\BackendInterface $backend)
{
foreach ($this->backends as $backend) {
$backend->close();
}
$this->backends = new \SplObjectStorage();
$this->backends->attach($backend);
}
/**
* Adds the backend to which the logger sends the logging data
*
* @param Backend\BackendInterface $backend A backend implementation
* @return void
* @api
*/
public function addBackend(Backend\BackendInterface $backend)
{
$this->backends->attach($backend);
$backend->open();
}
/**
* Runs the close() method of a backend and removes the backend
* from the logger.
*
* @param Backend\BackendInterface $backend The backend to remove
* @return void
* @throws NoSuchBackendException if the given backend is unknown to this logger
* @api
*/
public function removeBackend(Backend\BackendInterface $backend)
{
if (!$this->backends->contains($backend)) {
throw new NoSuchBackendException('Backend is unknown to this logger.', 1229430381);
}
$backend->close();
$this->backends->detach($backend);
}
/**
* Writes the given message along with the additional information into the log.
*
* @param string $message The message to log
* @param integer $severity An integer value, one of the LOG_* constants
* @param mixed $additionalData A variable containing more information about the event to be logged
* @param string $packageKey Key of the package triggering the log (determined automatically if not specified)
* @param string $className Name of the class triggering the log (determined automatically if not specified)
* @param string $methodName Name of the method triggering the log (determined automatically if not specified)
* @return void
* @api
*/
public function log($message, $severity = LOG_INFO, $additionalData = null, $packageKey = null, $className = null, $methodName = null)
{
if ($packageKey === null) {
$backtrace = debug_backtrace(false);
$className = isset($backtrace[1]['class']) ? $backtrace[1]['class'] : null;
$methodName = isset($backtrace[1]['function']) ? $backtrace[1]['function'] : null;
$explodedClassName = explode('\\', $className);
// FIXME: This is not really the package key:
$packageKey = isset($explodedClassName[1]) ? $explodedClassName[1] : '';
}
foreach ($this->backends as $backend) {
$backend->append($message, $severity, $additionalData, $packageKey, $className, $methodName);
}
}
/**
* @param \Exception $exception
* @param array $additionalData
*/
public function logException(\Exception $exception, array $additionalData = [])
{
$backtrace = debug_backtrace(false);
$className = isset($backtrace[1]['class']) ? $backtrace[1]['class'] : null;
$methodName = isset($backtrace[1]['function']) ? $backtrace[1]['function'] : null;
foreach ($this->backends as $backend) {
$backend->append($exception->getMessage(), LOG_ERR, $additionalData, '', $className, $methodName);
}
}
/**
* Cleanly closes all registered backends before destructing this Logger
*
* @return void
*/
public function shutdownObject()
{
foreach ($this->backends as $backend) {
$backend->close();
}
}
}