Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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
 
12
namespace Monolog\Handler;
13
 
14
use Monolog\Formatter\LineFormatter;
15
use Monolog\Formatter\FormatterInterface;
16
use Monolog\Logger;
17
use Monolog\Utils;
18
 
19
/**
20
 * Stores to PHP error_log() handler.
21
 *
22
 * @author Elan Ruusamäe <glen@delfi.ee>
23
 */
24
class ErrorLogHandler extends AbstractProcessingHandler
25
{
26
    public const OPERATING_SYSTEM = 0;
27
    public const SAPI = 4;
28
 
29
    /** @var int */
30
    protected $messageType;
31
    /** @var bool */
32
    protected $expandNewlines;
33
 
34
    /**
35
     * @param int  $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
    public function __construct(int $messageType = self::OPERATING_SYSTEM, $level = Logger::DEBUG, bool $bubble = true, bool $expandNewlines = false)
39
    {
40
        parent::__construct($level, $bubble);
41
 
42
        if (false === in_array($messageType, self::getAvailableTypes(), true)) {
43
            $message = sprintf('The given message type "%s" is not supported', print_r($messageType, true));
44
 
45
            throw new \InvalidArgumentException($message);
46
        }
47
 
48
        $this->messageType = $messageType;
49
        $this->expandNewlines = $expandNewlines;
50
    }
51
 
52
    /**
53
     * @return int[] With all available types
54
     */
55
    public static function getAvailableTypes(): array
56
    {
57
        return [
58
            self::OPERATING_SYSTEM,
59
            self::SAPI,
60
        ];
61
    }
62
 
63
    /**
64
     * {@inheritDoc}
65
     */
66
    protected function getDefaultFormatter(): FormatterInterface
67
    {
68
        return new LineFormatter('[%datetime%] %channel%.%level_name%: %message% %context% %extra%');
69
    }
70
 
71
    /**
72
     * {@inheritDoc}
73
     */
74
    protected function write(array $record): void
75
    {
76
        if (!$this->expandNewlines) {
77
            error_log((string) $record['formatted'], $this->messageType);
78
 
79
            return;
80
        }
81
 
82
        $lines = preg_split('{[\r\n]+}', (string) $record['formatted']);
83
        if ($lines === false) {
84
            $pcreErrorCode = preg_last_error();
85
            throw new \RuntimeException('Failed to preg_split formatted string: ' . $pcreErrorCode . ' / '. Utils::pcreLastErrorMessage($pcreErrorCode));
86
        }
87
        foreach ($lines as $line) {
88
            error_log($line, $this->messageType);
89
        }
90
    }
91
}