-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathLogger.php
More file actions
211 lines (181 loc) · 6.68 KB
/
Copy pathLogger.php
File metadata and controls
211 lines (181 loc) · 6.68 KB
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
<?php
/**
* This file is part of the Cloudinary PHP package.
*
* (c) Cloudinary
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cloudinary\Log;
use Cloudinary\ArrayUtils;
use Cloudinary\Configuration\LoggingConfig;
use Exception;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Handler\HandlerInterface;
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\TestHandler;
use Monolog\Logger as Monolog;
/**
* Class Logger
*
* @api
*/
class Logger
{
public const LOGGER_NAME = 'cloudinary';
private Monolog $entity;
/**
* @var int $defaultLogLevel The default log level. Is set during initialization.
*/
private int $defaultLogLevel;
/**
* @var array Map of PHP error levels to PSR-3 log levels
*/
private array $errorLevelMap
= [
E_ERROR => Monolog::CRITICAL,
E_WARNING => Monolog::WARNING,
E_PARSE => Monolog::ALERT,
E_NOTICE => Monolog::NOTICE,
E_CORE_ERROR => Monolog::CRITICAL,
E_CORE_WARNING => Monolog::WARNING,
E_COMPILE_ERROR => Monolog::ALERT,
E_COMPILE_WARNING => Monolog::WARNING,
E_USER_ERROR => Monolog::ERROR,
E_USER_WARNING => Monolog::WARNING,
E_USER_NOTICE => Monolog::NOTICE,
E_RECOVERABLE_ERROR => Monolog::ERROR,
E_DEPRECATED => Monolog::NOTICE,
E_USER_DEPRECATED => Monolog::NOTICE,
];
/**
* Logger constructor.
*
*/
public function __construct(LoggingConfig $config)
{
$this->entity = new Monolog(self::LOGGER_NAME);
$this->init($config);
}
private function init(LoggingConfig $config): ?Logger
{
if ($config->enabled === false) {
return null;
}
try {
$defaultLogLevel = $config->level ?: $this->getDefaultLogLevel();
if (is_array($config->errorLog)) {
$logLevel = ArrayUtils::get($config->errorLog, 'level', $defaultLogLevel);
$this->addHandler(new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, $logLevel));
}
if ($config->errorLog === true) {
$this->addHandler(new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, $defaultLogLevel));
}
if (is_array($config->file)) {
foreach ($config->file as $loggingFileConfig) {
if (empty($loggingFileConfig['path']) || ! is_string($loggingFileConfig['path'])) {
continue;
}
$logLevel = ArrayUtils::get($loggingFileConfig, 'level', $defaultLogLevel);
$this->addHandler(new StreamHandler($loggingFileConfig['path'], $logLevel));
}
}
if (is_array($config->test)) {
$logLevel = ArrayUtils::get($config->test, 'level', $defaultLogLevel);
$this->addHandler(new TestHandler($logLevel));
}
// If no handlers defined, fallback to default error log handler.
if ($config->errorLog !== false && empty($this->getHandlers())) {
$this->addHandler(new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, $defaultLogLevel));
}
/**
* If no handlers defined and errorLog disabled, use NullHandler
* to avoid Monolog's default behavior that creates php://stderr StreamHandler on empty handlers list
*/
if (empty($this->getHandlers())) {
$this->addHandler(new NullHandler(Monolog::DEBUG));
}
} catch (Exception $e) {
trigger_error($e->getMessage(), E_USER_WARNING);
return null;
}
return $this;
}
private function addHandler(HandlerInterface $handler): Monolog
{
foreach ($this->entity->getHandlers() as $entityHandler) {
if ($entityHandler instanceof HandlerInterface && $entityHandler->getLevel() === $handler->getLevel()
) {
return $this->entity;
}
}
return $this->entity->pushHandler($handler);
}
/**
* @return HandlerInterface[]
*/
public function getHandlers(): array
{
return $this->entity->getHandlers();
}
/**
* Gets TestHandler
*
*/
public function getTestHandler(): ?TestHandler
{
foreach ($this->entity->getHandlers() as $handler) {
if ($handler instanceof TestHandler) {
return $handler;
}
}
return null;
}
/**
* Adds a log record at an arbitrary level.
*
* This method allows for compatibility with common interfaces.
*
* @param mixed $level The log level (a Monolog, PSR-3 or RFC 5424 level)
* @param string|\Stringable $message The log message
* @param array $context The log context
*
*/
public function log(mixed $level, string|\Stringable $message, array $context = []): void
{
$this->entity->log($level, $message, $context);
}
/**
* Gets a PSR-3 log level based on the error level set in PHP
*
* Note: An error level of E_ALL will result in log messages of level NOTICE and higher being displayed.
*
* @return int PSR-3 log level
*/
public function getDefaultLogLevel(): int
{
if (isset($this->defaultLogLevel)) {
return $this->defaultLogLevel;
}
$this->defaultLogLevel = Monolog::EMERGENCY;
$phpErrorReportingLevel = error_reporting();
if ($phpErrorReportingLevel === E_ALL) {
$this->defaultLogLevel = Monolog::NOTICE;
return $this->defaultLogLevel;
}
// Convert PHP's error reporting level from an integer to a bitmask, go over each error level that is on
// and translate that level to a matching PSR-3 log level, finally return the lowest matching PSR-3 log level.
// For example, if E_ERROR and E_NOTICE are on, the function will return the PSR-3 NOTICE level.
// Decimal to bitmask conversion code based on https://www.php.net/manual/en/errorfunc.constants.php#121911
$pot = 0;
foreach (array_reverse(str_split(decbin($phpErrorReportingLevel))) as $bit) {
if ($bit === 1) {
$this->defaultLogLevel = min($this->defaultLogLevel, $this->errorLevelMap[2 ** $pot]);
}
$pot++;
}
return $this->defaultLogLevel;
}
}