1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elgg\Logger; |
4
|
|
|
|
5
|
|
|
use Elgg\EntityDirLocator; |
6
|
|
|
use Elgg\Exceptions\InvalidArgumentException; |
7
|
|
|
use Elgg\Project\Paths; |
8
|
|
|
use Monolog\Handler\RotatingFileHandler; |
9
|
|
|
use Monolog\Handler\StreamHandler; |
10
|
|
|
use Monolog\Processor\MemoryPeakUsageProcessor; |
11
|
|
|
use Monolog\Processor\MemoryUsageProcessor; |
12
|
|
|
use Monolog\Processor\ProcessIdProcessor; |
13
|
|
|
use Monolog\Processor\PsrLogMessageProcessor; |
14
|
|
|
use Monolog\Processor\TagProcessor; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Custom logger for an individual cron interval |
18
|
|
|
* |
19
|
|
|
* @since 5.1 |
20
|
|
|
*/ |
21
|
|
|
class Cron extends \Monolog\Logger { |
22
|
|
|
|
23
|
|
|
protected const CHANNEL = 'CRON'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param array $params additional params: |
27
|
|
|
* - (string) interval: a valid cron interval |
28
|
|
|
* - (\DateTime) date: the start date of the cron |
29
|
|
|
* |
30
|
|
|
* @return static |
31
|
|
|
* @throws InvalidArgumentException |
32
|
|
|
*/ |
33
|
9 |
|
public static function factory(array $params = []): static { |
34
|
9 |
|
$interval = elgg_extract('interval', $params); |
35
|
9 |
|
$cron = _elgg_services()->cron; |
36
|
9 |
|
if (empty($interval) || !in_array($interval, $cron->getConfiguredIntervals(true))) { |
37
|
|
|
throw new InvalidArgumentException('Please specify a valid cron interval'); |
38
|
|
|
} |
39
|
|
|
|
40
|
9 |
|
$filename = elgg_extract('filename', $params); |
41
|
9 |
|
if (empty($filename)) { |
42
|
|
|
throw new InvalidArgumentException('Please provide a log filename'); |
43
|
|
|
} |
44
|
|
|
|
45
|
9 |
|
$logger = new static(self::CHANNEL); |
46
|
|
|
|
47
|
9 |
|
$handler = new StreamHandler($filename); |
48
|
|
|
|
49
|
9 |
|
$formatter = new ElggLogFormatter(); |
50
|
9 |
|
$formatter->allowInlineLineBreaks(); |
51
|
9 |
|
$formatter->ignoreEmptyContextAndExtra(); |
52
|
|
|
|
53
|
9 |
|
$handler->setFormatter($formatter); |
54
|
|
|
|
55
|
9 |
|
$handler->pushProcessor(new MemoryUsageProcessor()); |
56
|
9 |
|
$handler->pushProcessor(new MemoryPeakUsageProcessor()); |
57
|
9 |
|
$handler->pushProcessor(new ProcessIdProcessor()); |
58
|
9 |
|
$handler->pushProcessor(new TagProcessor([$interval])); |
59
|
9 |
|
$handler->pushProcessor(new PsrLogMessageProcessor()); |
60
|
|
|
|
61
|
9 |
|
$logger->pushHandler($handler); |
62
|
|
|
|
63
|
9 |
|
return $logger; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|