source: vendor/monolog/monolog/src/Monolog/Handler/ErrorLogHandler.php

Last change on this file was e3d4e0a, checked in by Vlado 222039 <vlado.popovski@…>, 7 days ago

Upload project files

  • Property mode set to 100644
File size: 2.6 KB
Line 
1<?php declare(strict_types=1);
2
3/*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Monolog\Handler;
13
14use Monolog\Formatter\LineFormatter;
15use Monolog\Formatter\FormatterInterface;
16use Monolog\Level;
17use Monolog\Utils;
18use Monolog\LogRecord;
19
20/**
21 * Stores to PHP error_log() handler.
22 *
23 * @author Elan Ruusamäe <glen@delfi.ee>
24 */
25class ErrorLogHandler extends AbstractProcessingHandler
26{
27 public const OPERATING_SYSTEM = 0;
28 public const SAPI = 4;
29
30 /** @var 0|1|3|4 */
31 protected int $messageType;
32 protected bool $expandNewlines;
33
34 /**
35 * @param 0|1|3|4 $messageType Says where the error should go.
36 * @param bool $expandNewlines If set to true, newlines in the message will be expanded to be take multiple log entries
37 *
38 * @throws \InvalidArgumentException If an unsupported message type is set
39 */
40 public function __construct(int $messageType = self::OPERATING_SYSTEM, int|string|Level $level = Level::Debug, bool $bubble = true, bool $expandNewlines = false)
41 {
42 parent::__construct($level, $bubble);
43
44 if (false === \in_array($messageType, self::getAvailableTypes(), true)) {
45 $message = sprintf('The given message type "%s" is not supported', print_r($messageType, true));
46
47 throw new \InvalidArgumentException($message);
48 }
49
50 $this->messageType = $messageType;
51 $this->expandNewlines = $expandNewlines;
52 }
53
54 /**
55 * @return int[] With all available types
56 */
57 public static function getAvailableTypes(): array
58 {
59 return [
60 self::OPERATING_SYSTEM,
61 self::SAPI,
62 ];
63 }
64
65 /**
66 * @inheritDoc
67 */
68 protected function getDefaultFormatter(): FormatterInterface
69 {
70 return new LineFormatter('[%datetime%] %channel%.%level_name%: %message% %context% %extra%');
71 }
72
73 /**
74 * @inheritDoc
75 */
76 protected function write(LogRecord $record): void
77 {
78 if (!$this->expandNewlines) {
79 error_log((string) $record->formatted, $this->messageType);
80
81 return;
82 }
83
84 $lines = preg_split('{[\r\n]+}', (string) $record->formatted);
85 if ($lines === false) {
86 $pcreErrorCode = preg_last_error();
87
88 throw new \RuntimeException('Failed to preg_split formatted string: ' . $pcreErrorCode . ' / '. preg_last_error_msg());
89 }
90 foreach ($lines as $line) {
91 error_log($line, $this->messageType);
92 }
93 }
94}
Note: See TracBrowser for help on using the repository browser.