-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Logger.php
101 lines (90 loc) · 2.78 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
<?php
namespace Neos\Flow\Log\Psr;
/*
* 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\Backend\BackendInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
use Psr\Log\LogLevel;
/**
* PSR-3 supporting logger.
* This is the primary Logger implementation of the Neos.Flow.Log package.
*
* @api
*/
class Logger implements LoggerInterface
{
use LoggerTrait;
const LOGLEVEL_MAPPING = [
LogLevel::EMERGENCY => LOG_EMERG,
LogLevel::DEBUG => LOG_DEBUG,
LogLevel::INFO => LOG_INFO,
LogLevel::NOTICE => LOG_NOTICE,
LogLevel::WARNING => LOG_WARNING,
LogLevel::ERROR => LOG_ERR,
LogLevel::CRITICAL => LOG_CRIT,
LogLevel::ALERT => LOG_ALERT
];
/**
* @var BackendInterface[]
*/
protected $backends = [];
/**
* Constructs the PSR-3 Logger.
*
* @param iterable $backends
*/
public function __construct(iterable $backends)
{
foreach ($backends as $backend) {
if (!$backend instanceof BackendInterface) {
throw new \InvalidArgumentException(sprintf('The provided backend was not an instance of the "%s" interface', BackendInterface::class), 1515338089289);
}
$backend->open();
$this->backends[] = $backend;
}
}
/**
* @param mixed $level
* @param string $message
* @param array $context
* @api
*/
public function log($level, $message, array $context = [])
{
$backendLogLevel = self::LOGLEVEL_MAPPING[$level];
list($packageKey, $className, $methodName) = $this->extractLegacyDataFromContext($context);
$additionalData = $this->removeLegacyDataFromContext($context);
foreach ($this->backends as $backend) {
$backend->append($message, $backendLogLevel, $additionalData, $packageKey, $className, $methodName);
}
}
/**
* @param array $context
* @return array list of packageKey, className and methodName either string or null
*/
protected function extractLegacyDataFromContext(array $context): array
{
return [
$context['FLOW_LOG_ENVIRONMENT']['packageKey'] ?? null,
$context['FLOW_LOG_ENVIRONMENT']['className'] ?? null,
$context['FLOW_LOG_ENVIRONMENT']['methodName'] ?? null
];
}
/**
* @param array $context
* @return array
*/
protected function removeLegacyDataFromContext(array $context): array
{
unset($context['FLOW_LOG_ENVIRONMENT']);
return $context;
}
}