| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\ErrorHandler;
|
|
|
13 |
|
|
|
14 |
use Psr\Log\AbstractLogger;
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* A buffering logger that stacks logs for later.
|
|
|
18 |
*
|
|
|
19 |
* @author Nicolas Grekas <p@tchwork.com>
|
|
|
20 |
*/
|
|
|
21 |
class BufferingLogger extends AbstractLogger
|
|
|
22 |
{
|
|
|
23 |
private array $logs = [];
|
|
|
24 |
|
|
|
25 |
public function log($level, $message, array $context = []): void
|
|
|
26 |
{
|
|
|
27 |
$this->logs[] = [$level, $message, $context];
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
public function cleanLogs(): array
|
|
|
31 |
{
|
|
|
32 |
$logs = $this->logs;
|
|
|
33 |
$this->logs = [];
|
|
|
34 |
|
|
|
35 |
return $logs;
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
public function __sleep(): array
|
|
|
39 |
{
|
|
|
40 |
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
public function __wakeup()
|
|
|
44 |
{
|
|
|
45 |
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
public function __destruct()
|
|
|
49 |
{
|
|
|
50 |
foreach ($this->logs as [$level, $message, $context]) {
|
|
|
51 |
if (str_contains($message, '{')) {
|
|
|
52 |
foreach ($context as $key => $val) {
|
|
|
53 |
if (null === $val || \is_scalar($val) || (\is_object($val) && \is_callable([$val, '__toString']))) {
|
|
|
54 |
$message = str_replace("{{$key}}", $val, $message);
|
|
|
55 |
} elseif ($val instanceof \DateTimeInterface) {
|
|
|
56 |
$message = str_replace("{{$key}}", $val->format(\DateTimeInterface::RFC3339), $message);
|
|
|
57 |
} elseif (\is_object($val)) {
|
|
|
58 |
$message = str_replace("{{$key}}", '[object '.get_debug_type($val).']', $message);
|
|
|
59 |
} else {
|
|
|
60 |
$message = str_replace("{{$key}}", '['.\gettype($val).']', $message);
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
error_log(sprintf('%s [%s] %s', date(\DateTimeInterface::RFC3339), $level, $message));
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
}
|