Elgg  Version 6.3
CronLogHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cli;
4 
8 use Monolog\Formatter\FormatterInterface;
9 use Monolog\Handler\AbstractProcessingHandler;
10 use Monolog\Level;
11 use Monolog\LogRecord;
12 use Symfony\Component\Console\Output\NullOutput;
13 use Symfony\Component\Console\Output\OutputInterface;
14 
22 class CronLogHandler extends AbstractProcessingHandler {
23 
24  protected ?OutputInterface $stdout;
25 
29  public function __construct(bool $bubble = true) {
30  if (!ElggApplication::isCli()) {
31  throw new Exception(__CLASS__ . ' can only be used during CLI');
32  }
33 
34  $this->stdout = _elgg_services()->cli_output;
35 
36  $level = Level::Emergency;
37  if ($this->stdout->getVerbosity() !== OutputInterface::VERBOSITY_QUIET) {
38  $level = Level::Debug;
39  }
40 
41  parent::__construct($level, $bubble);
42  }
43 
47  protected function write(LogRecord $record): void {
48  $this->stdout?->write($record->formatted);
49  }
50 
54  public function getDefaultFormatter(): FormatterInterface {
55  $formatter = new ElggLogFormatter();
56  $formatter->allowInlineLineBreaks();
57  $formatter->ignoreEmptyContextAndExtra();
58 
59  return $formatter;
60  }
61 
65  public function close(): void {
66  $this->stdout = new NullOutput();
67  }
68 }
Additional output handler for the \Elgg\Logger\Cron which outputs to the stdout.
OutputInterface $stdout
write(LogRecord $record)
{}
__construct(bool $bubble=true)
{}
Base exception of exceptions in the Elgg system.
Definition: Exception.php:11
_elgg_services()
Get the global service provider.
Definition: elgglib.php:337