forked from Seldaek/monolog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClosureContextProcessor.php
51 lines (44 loc) · 1.48 KB
/
ClosureContextProcessor.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
<?php declare(strict_types=1);
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Processor;
use Monolog\LogRecord;
/**
* Generates a context from a Closure if the Closure is the only value
* in the context
*
* It helps reduce the performance impact of debug logs if they do
* need to create lots of context information. If this processor is added
* on the correct handler the context data will only be generated
* when the logs are actually logged to that handler, which is useful when
* using FingersCrossedHandler or other filtering handlers to conditionally
* log records.
*/
class ClosureContextProcessor implements ProcessorInterface
{
public function __invoke(LogRecord $record): LogRecord
{
$context = $record->context;
if (isset($context[0]) && 1 === \count($context) && $context[0] instanceof \Closure) {
try {
$context = $context[0]();
} catch (\Throwable $e) {
$context = [
'error_on_context_generation' => $e->getMessage(),
'exception' => $e,
];
}
if (!\is_array($context)) {
$context = [$context];
}
$record = $record->with(context: $context);
}
return $record;
}
}