-
Notifications
You must be signed in to change notification settings - Fork 691
Expand file tree
/
Copy pathMonologTarget.php
More file actions
380 lines (333 loc) Β· 9.62 KB
/
MonologTarget.php
File metadata and controls
380 lines (333 loc) Β· 9.62 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
namespace craft\log;
use Craft;
use craft\helpers\App;
use craft\helpers\ArrayHelper;
use DateTimeZone;
use Illuminate\Support\Collection;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Monolog\Processor\ProcessorInterface;
use Monolog\Processor\PsrLogMessageProcessor;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use samdark\log\PsrTarget;
use yii\base\InvalidConfigException;
use yii\i18n\PhpMessageSource;
use yii\web\HttpException;
/**
* Class MonologTarget
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @property-read string $contextMessage
* @since 4.0.0
*/
class MonologTarget extends PsrTarget
{
/**
* @inheritdoc
*/
public $except = [
PhpMessageSource::class . ':*',
HttpException::class . ':404',
];
/**
* @var bool Whether to log request context
*/
public bool $logContext = true;
/**
* @var bool
*/
protected bool $allowLineBreaks = false;
/**
* @var string
* @see Logger::$name
*/
protected string $name;
/**
* @var string The PSR-3 log level to use.
* @phpstan-var LogLevel::*
*/
protected string $level = LogLevel::WARNING;
/**
* @var int The maximum number of files to keep in rotation.
* @see RotatingFileHandler::$maxFiles
*/
protected int $maxFiles = 5;
/**
* @see Logger::useMicrosecondTimestamps
* @var bool
*/
protected bool $useMicrosecondTimestamps = false;
/**
* @var FormatterInterface|null The Monolog formatter to use. Defaults to `LineFormatter`.
*/
protected ?FormatterInterface $formatter = null;
/**
* @var ProcessorInterface|null The Monolog processor to use. Defaults to `PsrLogMessageProcessor`.
*/
protected ?ProcessorInterface $processor = null;
public function __construct($config = [])
{
// Store and unset logger, so we can create it with a closure
$logger = ArrayHelper::remove($config, 'logger');
parent::__construct($config);
$this->formatter ??= new LineFormatter(
format: "%datetime% [%channel%.%level_name%] [%extra.yii_category%] %message% %context% %extra%\n",
dateFormat: 'Y-m-d H:i:s',
allowInlineLineBreaks: $this->allowLineBreaks,
ignoreEmptyContextAndExtra: true,
);
$this->logger = match (true) {
$logger instanceof Logger => $logger,
is_callable($logger) => $logger($this),
default => $this->_createDefaultLogger(),
};
}
/**
* @return Logger
*/
public function getLogger(): Logger
{
/** @var Logger */
return $this->logger ?? $this->_createDefaultLogger();
}
/**
* @inheritdoc
* @throws InvalidConfigException
*/
public function setLogger(Logger|LoggerInterface $logger): void
{
throw new InvalidConfigException('Logger may not be configured directly.');
}
/**
* Log additional request context.
* @inheritdoc
*/
public function export(): void
{
$this->messages = $this->_filterMessagesByPsrLevel($this->messages, $this->level);
/** @var Logger $logger */
$logger = $this->logger;
$logger->setTimezone(new DateTimeZone(Craft::$app->getTimeZone()));
parent::export();
if (!$this->logContext || empty($this->messages)) {
return;
}
$logger->pushProcessor(new ContextProcessor(
vars: $this->logVars,
dumpVars: $this->allowLineBreaks,
));
// Log at default level, so it doesn't get filtered
$logger->log($this->level, 'Request context:');
$logger->popProcessor();
}
/**
* Context is logged via {@see self::export} method, so it can be added using Monolog.
* @inheritdoc
*/
protected function getContextMessage(): string
{
return '';
}
/**
* @param array $messages
* @param string $level
* @phpstan-param LogLevel::* $level
* @return array
*/
private function _filterMessagesByPsrLevel(array $messages, string $level): array
{
$levelMap = Collection::make((array) $this->getLevels());
$monologLevel = Logger::toMonologLevel($level);
$messages = Collection::make($messages)
->filter(function($message) use ($levelMap, $monologLevel) {
$level = $message[1];
$psrLevel = is_int($level) ? $levelMap->get($level) : $level;
return Logger::toMonologLevel($psrLevel)->value >= $monologLevel->value;
});
return $messages->all();
}
private function _createDefaultLogger(): Logger
{
$logger = (new Logger($this->name))->useMicrosecondTimestamps($this->useMicrosecondTimestamps);
if ($this->processor) {
$logger->pushProcessor($this->processor);
} else {
$logger
->pushProcessor(new PsrLogMessageProcessor())
->pushProcessor(new MessageProcessor());
}
if (App::isStreamLog()) {
$logger->pushHandler((new StreamHandler(
'php://stderr',
Logger::WARNING,
bubble: false,
))->setFormatter($this->formatter));
$logger->pushHandler((new StreamHandler(
'php://stdout',
$this->level,
bubble: false,
))->setFormatter($this->formatter));
} else {
$logger->pushHandler((new RotatingFileHandler(
App::parseEnv(sprintf('@storage/logs/%s.log', $this->name)),
$this->maxFiles,
$this->level,
filePermission: Craft::$app->getConfig()->getGeneral()->defaultFileMode,
))->setFormatter($this->formatter));
}
return $logger;
}
/**
* @param string $name
* @throws InvalidConfigException
*/
public function setName(string $name): void
{
$this->_setLoggerProperty('name', $name);
}
/**
* Returns the log targetβs name.
*
* @return string
* @since 5.5.0
*/
public function getName(): string
{
return $this->name;
}
/**
* Sets whether the log target should allow line breaks.
*
* @param bool $allowLineBreaks
* @throws InvalidConfigException
*/
public function setAllowLineBreaks(bool $allowLineBreaks): void
{
$this->_setLoggerProperty('allowLineBreaks', $allowLineBreaks);
}
/**
* Returns whether the log target should allow line breaks.
*
* @return bool
* @since 5.5.0
*/
public function getAllowLineBreaks(): bool
{
return $this->allowLineBreaks;
}
/**
* Sets the log level.
*
* @param string|null $level
* @throws InvalidConfigException
*/
public function setLevel(?string $level): void
{
$this->_setLoggerProperty('level', $level);
}
/**
* Returns the log level.
*
* @return string
* @since 5.5.0
*/
public function getLevel(): string
{
return $this->level;
}
/**
* Sets the maximum number of log files to store.
*
* @param int $maxFiles
* @throws InvalidConfigException
*/
public function setMaxFiles(int $maxFiles): void
{
$this->_setLoggerProperty('maxFiles', $maxFiles);
}
/**
* Returns the maximum number of log files to store.
*
* @return int
* @since 5.5.0
*/
public function getMaxFiles(): int
{
return $this->maxFiles;
}
/**
* Sets whether logs should show microseconds in timestamps.
*
* @param bool $useMicrosecondTimestamps
* @throws InvalidConfigException
*/
public function setUseMicrosecondTimestamps(bool $useMicrosecondTimestamps): void
{
$this->_setLoggerProperty('useMicrosecondTimestamps', $useMicrosecondTimestamps);
}
/**
* Returns whether logs should show microseconds in timestamps.
*
* @return bool
* @since 5.5.0
*/
public function getUseMicrosecondTimestamps(): bool
{
return $this->useMicrosecondTimestamps;
}
/**
* Sets the log formatter.
*
* @param FormatterInterface|null $formatter
* @throws InvalidConfigException
*/
public function setFormatter(?FormatterInterface $formatter): void
{
$this->_setLoggerProperty('formatter', $formatter);
}
/**
* Returns the log formatter.
*
* @return FormatterInterface|null
* @since 5.5.0
*/
public function getFormatter(): ?FormatterInterface
{
return $this->formatter;
}
/**
* Sets the log processor.
*
* @param ProcessorInterface|null $processor
* @throws InvalidConfigException
*/
public function setProcessor(?ProcessorInterface $processor): void
{
$this->_setLoggerProperty('processor', $processor);
}
/**
* Returns the log processor.
*
* @return ProcessorInterface|null
* @since 5.5.0
*/
public function getProcessor(): ?ProcessorInterface
{
return $this->processor;
}
/**
* @throws InvalidConfigException
*/
private function _setLoggerProperty(string $property, mixed $value): void
{
if (isset($this->logger)) {
throw new InvalidConfigException("The property β{$property}β may not be set after logger is initialized.");
}
$this->$property = $value;
}
}