4 * This file is part of the Monolog package.
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Monolog\Handler;
14 use Monolog\Formatter\LineFormatter;
18 * Stores to PHP error_log() handler.
20 * @author Elan Ruusamäe <glen@delfi.ee>
22 class ErrorLogHandler extends AbstractProcessingHandler
24 const OPERATING_SYSTEM = 0;
27 protected $messageType;
28 protected $expandNewlines;
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
36 public function __construct($messageType = self::OPERATING_SYSTEM, $level = Logger::DEBUG, $bubble = true, $expandNewlines = false)
38 parent::__construct($level, $bubble);
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);
45 $this->messageType = $messageType;
46 $this->expandNewlines = $expandNewlines;
50 * @return array With all available types
52 public static function getAvailableTypes()
55 self::OPERATING_SYSTEM,
63 protected function getDefaultFormatter()
65 return new LineFormatter('[%datetime%] %channel%.%level_name%: %message% %context% %extra%');
71 protected function write(array $record)
73 if ($this->expandNewlines) {
74 $lines = preg_split('{[\r\n]+}', (string) $record['formatted']);
75 foreach ($lines as $line) {
76 error_log($line, $this->messageType);
79 error_log((string) $record['formatted'], $this->messageType);