]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/monolog/monolog/src/Monolog/Handler/ErrorLogHandler.php
MediaWiki 1.30.2-scripts2
[autoinstallsdev/mediawiki.git] / vendor / monolog / monolog / src / Monolog / Handler / ErrorLogHandler.php
1 <?php
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\Logger;
16
17 /**
18  * Stores to PHP error_log() handler.
19  *
20  * @author Elan Ruusamäe <glen@delfi.ee>
21  */
22 class ErrorLogHandler extends AbstractProcessingHandler
23 {
24     const OPERATING_SYSTEM = 0;
25     const SAPI = 4;
26
27     protected $messageType;
28     protected $expandNewlines;
29
30     /**
31      * @param int     $messageType    Says where the error should go.
32      * @param int     $level          The minimum logging level at which this handler will be triggered
33      * @param Boolean $bubble         Whether the messages that are handled can bubble up the stack or not
34      * @param Boolean $expandNewlines If set to true, newlines in the message will be expanded to be take multiple log entries
35      */
36     public function __construct($messageType = self::OPERATING_SYSTEM, $level = Logger::DEBUG, $bubble = true, $expandNewlines = false)
37     {
38         parent::__construct($level, $bubble);
39
40         if (false === in_array($messageType, self::getAvailableTypes())) {
41             $message = sprintf('The given message type "%s" is not supported', print_r($messageType, true));
42             throw new \InvalidArgumentException($message);
43         }
44
45         $this->messageType = $messageType;
46         $this->expandNewlines = $expandNewlines;
47     }
48
49     /**
50      * @return array With all available types
51      */
52     public static function getAvailableTypes()
53     {
54         return array(
55             self::OPERATING_SYSTEM,
56             self::SAPI,
57         );
58     }
59
60     /**
61      * {@inheritDoc}
62      */
63     protected function getDefaultFormatter()
64     {
65         return new LineFormatter('[%datetime%] %channel%.%level_name%: %message% %context% %extra%');
66     }
67
68     /**
69      * {@inheritdoc}
70      */
71     protected function write(array $record)
72     {
73         if ($this->expandNewlines) {
74             $lines = preg_split('{[\r\n]+}', (string) $record['formatted']);
75             foreach ($lines as $line) {
76                 error_log($line, $this->messageType);
77             }
78         } else {
79             error_log((string) $record['formatted'], $this->messageType);
80         }
81     }
82 }