forked from illuminate/log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.php
executable file
·310 lines (279 loc) · 7.2 KB
/
Logger.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
namespace Illuminate\Log;
use Closure;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Log\Events\MessageLogged;
use Psr\Log\LoggerInterface;
use RuntimeException;
class Logger implements LoggerInterface
{
/**
* The underlying logger implementation.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* The event dispatcher instance.
*
* @var \Illuminate\Contracts\Events\Dispatcher|null
*/
protected $dispatcher;
/**
* Any context to be added to logs.
*
* @var array
*/
protected $context = [];
/**
* Create a new log writer instance.
*
* @param \Psr\Log\LoggerInterface $logger
* @param \Illuminate\Contracts\Events\Dispatcher|null $dispatcher
* @return void
*/
public function __construct(LoggerInterface $logger, Dispatcher $dispatcher = null)
{
$this->logger = $logger;
$this->dispatcher = $dispatcher;
}
/**
* Log an emergency message to the logs.
*
* @param string $message
* @param array $context
* @return void
*/
public function emergency($message, array $context = [])
{
$this->writeLog(__FUNCTION__, $message, $context);
}
/**
* Log an alert message to the logs.
*
* @param string $message
* @param array $context
* @return void
*/
public function alert($message, array $context = [])
{
$this->writeLog(__FUNCTION__, $message, $context);
}
/**
* Log a critical message to the logs.
*
* @param string $message
* @param array $context
* @return void
*/
public function critical($message, array $context = [])
{
$this->writeLog(__FUNCTION__, $message, $context);
}
/**
* Log an error message to the logs.
*
* @param string $message
* @param array $context
* @return void
*/
public function error($message, array $context = [])
{
$this->writeLog(__FUNCTION__, $message, $context);
}
/**
* Log a warning message to the logs.
*
* @param string $message
* @param array $context
* @return void
*/
public function warning($message, array $context = [])
{
$this->writeLog(__FUNCTION__, $message, $context);
}
/**
* Log a notice to the logs.
*
* @param string $message
* @param array $context
* @return void
*/
public function notice($message, array $context = [])
{
$this->writeLog(__FUNCTION__, $message, $context);
}
/**
* Log an informational message to the logs.
*
* @param string $message
* @param array $context
* @return void
*/
public function info($message, array $context = [])
{
$this->writeLog(__FUNCTION__, $message, $context);
}
/**
* Log a debug message to the logs.
*
* @param string $message
* @param array $context
* @return void
*/
public function debug($message, array $context = [])
{
$this->writeLog(__FUNCTION__, $message, $context);
}
/**
* Log a message to the logs.
*
* @param string $level
* @param string $message
* @param array $context
* @return void
*/
public function log($level, $message, array $context = [])
{
$this->writeLog($level, $message, $context);
}
/**
* Dynamically pass log calls into the writer.
*
* @param string $level
* @param string $message
* @param array $context
* @return void
*/
public function write($level, $message, array $context = [])
{
$this->writeLog($level, $message, $context);
}
/**
* Write a message to the log.
*
* @param string $level
* @param string $message
* @param array $context
* @return void
*/
protected function writeLog($level, $message, $context)
{
$this->logger->{$level}(
$message = $this->formatMessage($message),
$context = array_merge($this->context, $context)
);
$this->fireLogEvent($level, $message, $context);
}
/**
* Add context to all future logs.
*
* @param array $context
* @return $this
*/
public function withContext(array $context = [])
{
$this->context = array_merge($this->context, $context);
return $this;
}
/**
* Flush the existing context array.
*
* @return $this
*/
public function withoutContext()
{
$this->context = [];
return $this;
}
/**
* Register a new callback handler for when a log event is triggered.
*
* @param \Closure $callback
* @return void
*
* @throws \RuntimeException
*/
public function listen(Closure $callback)
{
if (! isset($this->dispatcher)) {
throw new RuntimeException('Events dispatcher has not been set.');
}
$this->dispatcher->listen(MessageLogged::class, $callback);
}
/**
* Fires a log event.
*
* @param string $level
* @param string $message
* @param array $context
* @return void
*/
protected function fireLogEvent($level, $message, array $context = [])
{
// If the event dispatcher is set, we will pass along the parameters to the
// log listeners. These are useful for building profilers or other tools
// that aggregate all of the log messages for a given "request" cycle.
if (isset($this->dispatcher)) {
$this->dispatcher->dispatch(new MessageLogged($level, $message, $context));
}
}
/**
* Format the parameters for the logger.
*
* @param mixed $message
* @return mixed
*/
protected function formatMessage($message)
{
if (is_array($message)) {
return var_export($message, true);
} elseif ($message instanceof Jsonable) {
return $message->toJson();
} elseif ($message instanceof Arrayable) {
return var_export($message->toArray(), true);
}
return $message;
}
/**
* Get the underlying logger implementation.
*
* @return \Psr\Log\LoggerInterface
*/
public function getLogger()
{
return $this->logger;
}
/**
* Get the event dispatcher instance.
*
* @return \Illuminate\Contracts\Events\Dispatcher
*/
public function getEventDispatcher()
{
return $this->dispatcher;
}
/**
* Set the event dispatcher instance.
*
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
* @return void
*/
public function setEventDispatcher(Dispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}
/**
* Dynamically proxy method calls to the underlying logger.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return $this->logger->{$method}(...$parameters);
}
}