-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOneLog.php
120 lines (102 loc) · 2.97 KB
/
OneLog.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
<?php declare(strict_types=1);
namespace KoderHut\OnelogBundle;
use KoderHut\OnelogBundle\Exceptions\LoggerNotFoundException;
use KoderHut\OnelogBundle\Helper\NullLogger;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
/**
* Class OneLog
*
* @author Denis-Florin Rendler <[email protected]>
* @author Joao Jacome <[email protected]>
*/
class OneLog implements LoggerInterface
{
use LoggerTrait;
public const DEFAULT_LOGGER = 'default';
/**
* @var LoggerInterface|NullLogger
*/
private $defaultLogger;
/**
* @var <array>LoggerInterface
*/
private $loggers;
/**
* @var MiddlewareProcessor
*/
private $middlewareProcessor;
/**
* OneLog constructor.
*
* @param LoggerInterface $default
* @param LoggerInterface ...$logger
*/
public function __construct(LoggerInterface $default = null, LoggerInterface ...$logger)
{
$this->defaultLogger = $default ?? new NullLogger();
$this->registerLogger($this->defaultLogger, self::DEFAULT_LOGGER);
array_unshift($logger, $this->defaultLogger);
foreach ($logger as $loggerInstance) {
$this->registerLogger($loggerInstance);
}
}
/**
* @param MiddlewareProcessor $middlewareProcessor
*/
public function setMiddlewareProcessor(MiddlewareProcessor $middlewareProcessor)
{
$this->middlewareProcessor = $middlewareProcessor;
}
/**
* Retrieves a registered logger based on the logger name as a
* public property of the class
*
* @param string $logger
*
* @return LoggerInterface
*/
public function __get($logger)
{
if (isset($this->loggers[$logger])) {
return $this->loggers[$logger];
}
throw new LoggerNotFoundException('Unable to find logger', ['logger' => $logger]);
}
/**
* Returns the loggers registered
*
* @return array <array>LoggerInterface
*/
public function loggers(): array
{
return $this->loggers;
}
/**
* Register a logger instance
*
* @param LoggerInterface $logger
* @param string|null $name
*/
public function registerLogger(LoggerInterface $logger, $name = null): void
{
$loggerName = $name ?? spl_object_hash($logger);
if (null === $name && ($logger instanceof Logger || $logger instanceof NamedLoggerInterface)) {
$loggerName = $logger->getName();
}
$this->loggers[$loggerName] = $logger;
}
/**
* @param mixed $level
* @param mixed $message
* @param array $context
*/
public function log($level, $message, array $context = [])
{
if (null !== $this->middlewareProcessor) {
[$message, $context] = $this->middlewareProcessor->process($level, $message, $context);
}
$this->defaultLogger->log($level, $message, $context);
}
}