media-cloud-sync
/
includes
/
sdk
/
google
/
monolog
/
monolog
/
src
/
Monolog
/
Handler
/
ErrorLogHandler.php
ErrorLogHandler.php in Media Cloud Sync 1.2.5, at includes/sdk/google/monolog/monolog/src/Monolog/Handler/ErrorLogHandler.php
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | /* |
| 5 | * This file is part of the Monolog package. |
| 6 | * |
| 7 | * (c) Jordi Boggiano <[email protected]> |
| 8 | * |
| 9 | * For the full copyright and license information, please view the LICENSE |
| 10 | * file that was distributed with this source code. |
| 11 | */ |
| 12 | namespace Dudlewebs\WPMCS\Monolog\Handler; |
| 13 | |
| 14 | use Dudlewebs\WPMCS\Monolog\Formatter\LineFormatter; |
| 15 | use Dudlewebs\WPMCS\Monolog\Formatter\FormatterInterface; |
| 16 | use Dudlewebs\WPMCS\Monolog\Logger; |
| 17 | use Dudlewebs\WPMCS\Monolog\Utils; |
| 18 | /** |
| 19 | * Stores to PHP error_log() handler. |
| 20 | * |
| 21 | * @author Elan Ruusamäe <[email protected]> |
| 22 | */ |
| 23 | class ErrorLogHandler extends AbstractProcessingHandler |
| 24 | { |
| 25 | public const OPERATING_SYSTEM = 0; |
| 26 | public const SAPI = 4; |
| 27 | /** @var int */ |
| 28 | protected $messageType; |
| 29 | /** @var bool */ |
| 30 | protected $expandNewlines; |
| 31 | /** |
| 32 | * @param int $messageType Says where the error should go. |
| 33 | * @param bool $expandNewlines If set to true, newlines in the message will be expanded to be take multiple log entries |
| 34 | */ |
| 35 | public function __construct(int $messageType = self::OPERATING_SYSTEM, $level = Logger::DEBUG, bool $bubble = \true, bool $expandNewlines = \false) |
| 36 | { |
| 37 | parent::__construct($level, $bubble); |
| 38 | if (\false === in_array($messageType, self::getAvailableTypes(), \true)) { |
| 39 | $message = sprintf('The given message type "%s" is not supported', print_r($messageType, \true)); |
| 40 | throw new \InvalidArgumentException($message); |
| 41 | } |
| 42 | $this->messageType = $messageType; |
| 43 | $this->expandNewlines = $expandNewlines; |
| 44 | } |
| 45 | /** |
| 46 | * @return int[] With all available types |
| 47 | */ |
| 48 | public static function getAvailableTypes(): array |
| 49 | { |
| 50 | return [self::OPERATING_SYSTEM, self::SAPI]; |
| 51 | } |
| 52 | /** |
| 53 | * {@inheritDoc} |
| 54 | */ |
| 55 | protected function getDefaultFormatter(): FormatterInterface |
| 56 | { |
| 57 | return new LineFormatter('[%datetime%] %channel%.%level_name%: %message% %context% %extra%'); |
| 58 | } |
| 59 | /** |
| 60 | * {@inheritDoc} |
| 61 | */ |
| 62 | protected function write(array $record): void |
| 63 | { |
| 64 | if (!$this->expandNewlines) { |
| 65 | error_log((string) $record['formatted'], $this->messageType); |
| 66 | return; |
| 67 | } |
| 68 | $lines = preg_split('{[\r\n]+}', (string) $record['formatted']); |
| 69 | if ($lines === \false) { |
| 70 | $pcreErrorCode = preg_last_error(); |
| 71 | throw new \RuntimeException('Failed to preg_split formatted string: ' . $pcreErrorCode . ' / ' . Utils::pcreLastErrorMessage($pcreErrorCode)); |
| 72 | } |
| 73 | foreach ($lines as $line) { |
| 74 | error_log($line, $this->messageType); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 |