1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elgg\Logger; |
4
|
|
|
|
5
|
|
|
use Elgg\Application; |
6
|
|
|
use Elgg\Cli\CronLogHandler; |
7
|
|
|
use Elgg\Exceptions\InvalidArgumentException; |
8
|
|
|
use Monolog\Handler\StreamHandler; |
9
|
|
|
use Monolog\Processor\MemoryPeakUsageProcessor; |
10
|
|
|
use Monolog\Processor\MemoryUsageProcessor; |
11
|
|
|
use Monolog\Processor\ProcessIdProcessor; |
12
|
|
|
use Monolog\Processor\PsrLogMessageProcessor; |
13
|
|
|
use Monolog\Processor\TagProcessor; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Custom logger for an individual cron interval |
17
|
|
|
* |
18
|
|
|
* @since 5.1 |
19
|
|
|
*/ |
20
|
|
|
class Cron extends \Monolog\Logger { |
21
|
|
|
|
22
|
|
|
protected const CHANNEL = 'CRON'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param array $params additional params: |
26
|
|
|
* - (string) interval: a valid cron interval |
27
|
|
|
* - (\DateTime) date: the start date of the cron |
28
|
|
|
* |
29
|
|
|
* @return static |
30
|
|
|
* @throws InvalidArgumentException |
31
|
|
|
*/ |
32
|
9 |
|
public static function factory(array $params = []): static { |
33
|
9 |
|
$interval = elgg_extract('interval', $params); |
34
|
9 |
|
$cron = _elgg_services()->cron; |
35
|
9 |
|
if (empty($interval) || !in_array($interval, $cron->getConfiguredIntervals(true))) { |
36
|
|
|
throw new InvalidArgumentException('Please specify a valid cron interval'); |
37
|
|
|
} |
38
|
|
|
|
39
|
9 |
|
$filename = elgg_extract('filename', $params); |
40
|
9 |
|
if (empty($filename)) { |
41
|
|
|
throw new InvalidArgumentException('Please provide a log filename'); |
42
|
|
|
} |
43
|
|
|
|
44
|
9 |
|
$logger = new static(self::CHANNEL); |
45
|
|
|
|
46
|
|
|
// default file output handler |
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
|
|
|
// in cli add handler to log to stdout |
64
|
9 |
|
if (Application::isCli() && !_elgg_services()->config->testing_mode) { |
65
|
3 |
|
$cli_output = new CronLogHandler(); |
66
|
|
|
|
67
|
3 |
|
$cli_output->pushProcessor(new MemoryUsageProcessor()); |
68
|
3 |
|
$cli_output->pushProcessor(new MemoryPeakUsageProcessor()); |
69
|
3 |
|
$cli_output->pushProcessor(new ProcessIdProcessor()); |
70
|
3 |
|
$cli_output->pushProcessor(new TagProcessor([$interval])); |
71
|
3 |
|
$cli_output->pushProcessor(new PsrLogMessageProcessor()); |
72
|
|
|
|
73
|
3 |
|
$logger->pushHandler($cli_output); |
74
|
|
|
} |
75
|
|
|
|
76
|
9 |
|
return $logger; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|