mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-06 13:16:39 +02:00
Add option to allow multiline logs in error_log, fixes #366
This commit is contained in:
@@ -25,13 +25,15 @@ class ErrorLogHandler extends AbstractProcessingHandler
|
|||||||
const SAPI = 4;
|
const SAPI = 4;
|
||||||
|
|
||||||
protected $messageType;
|
protected $messageType;
|
||||||
|
protected $expandNewlines;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param integer $messageType Says where the error should go.
|
* @param integer $messageType Says where the error should go.
|
||||||
* @param integer $level The minimum logging level at which this handler will be triggered
|
* @param integer $level The minimum logging level at which this handler will be triggered
|
||||||
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
|
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
|
||||||
|
* @param Boolean $expandNewlines If set to true, newlines in the message will be expanded to be take multiple log entries
|
||||||
*/
|
*/
|
||||||
public function __construct($messageType = self::OPERATING_SYSTEM, $level = Logger::DEBUG, $bubble = true)
|
public function __construct($messageType = self::OPERATING_SYSTEM, $level = Logger::DEBUG, $bubble = true, $expandNewlines = false)
|
||||||
{
|
{
|
||||||
parent::__construct($level, $bubble);
|
parent::__construct($level, $bubble);
|
||||||
|
|
||||||
@@ -41,6 +43,7 @@ class ErrorLogHandler extends AbstractProcessingHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->messageType = $messageType;
|
$this->messageType = $messageType;
|
||||||
|
$this->expandNewlines = $expandNewlines;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -67,6 +70,13 @@ class ErrorLogHandler extends AbstractProcessingHandler
|
|||||||
*/
|
*/
|
||||||
protected function write(array $record)
|
protected function write(array $record)
|
||||||
{
|
{
|
||||||
error_log((string) $record['formatted'], $this->messageType);
|
if ($this->expandNewlines) {
|
||||||
|
$lines = preg_split('{[\r\n]+}', (string) $record['formatted']);
|
||||||
|
foreach ($lines as $line) {
|
||||||
|
error_log($line, $this->messageType);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
error_log((string) $record['formatted'], $this->messageType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -4,10 +4,11 @@ namespace Monolog\Handler;
|
|||||||
|
|
||||||
use Monolog\TestCase;
|
use Monolog\TestCase;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
use Monolog\Formatter\LineFormatter;
|
||||||
|
|
||||||
function error_log()
|
function error_log()
|
||||||
{
|
{
|
||||||
$GLOBALS['error_log'] = func_get_args();
|
$GLOBALS['error_log'][] = func_get_args();
|
||||||
}
|
}
|
||||||
|
|
||||||
class ErrorLogHandlerTest extends TestCase
|
class ErrorLogHandlerTest extends TestCase
|
||||||
@@ -35,9 +36,23 @@ class ErrorLogHandlerTest extends TestCase
|
|||||||
{
|
{
|
||||||
$type = ErrorLogHandler::OPERATING_SYSTEM;
|
$type = ErrorLogHandler::OPERATING_SYSTEM;
|
||||||
$handler = new ErrorLogHandler($type);
|
$handler = new ErrorLogHandler($type);
|
||||||
$handler->handle($this->getRecord(Logger::ERROR));
|
$handler->setFormatter(new LineFormatter('%channel%.%level_name%: %message% %context% %extra%', null, true));
|
||||||
|
$handler->handle($this->getRecord(Logger::ERROR, "Foo\nBar\r\n\r\nBaz"));
|
||||||
|
|
||||||
$this->assertStringMatchesFormat('[%s] test.ERROR: test [] []', $GLOBALS['error_log'][0]);
|
$this->assertSame("test.ERROR: Foo\nBar\r\n\r\nBaz [] []", $GLOBALS['error_log'][0][0]);
|
||||||
$this->assertSame($GLOBALS['error_log'][1], $type);
|
$this->assertSame($GLOBALS['error_log'][0][1], $type);
|
||||||
|
|
||||||
|
$handler = new ErrorLogHandler($type, Logger::DEBUG, true, true);
|
||||||
|
$handler->setFormatter(new LineFormatter(null, null, true));
|
||||||
|
$handler->handle($this->getRecord(Logger::ERROR, "Foo\nBar\r\n\r\nBaz"));
|
||||||
|
|
||||||
|
$this->assertStringMatchesFormat('[%s] test.ERROR: Foo', $GLOBALS['error_log'][1][0]);
|
||||||
|
$this->assertSame($GLOBALS['error_log'][1][1], $type);
|
||||||
|
|
||||||
|
$this->assertStringMatchesFormat('Bar', $GLOBALS['error_log'][2][0]);
|
||||||
|
$this->assertSame($GLOBALS['error_log'][2][1], $type);
|
||||||
|
|
||||||
|
$this->assertStringMatchesFormat('Baz [] []', $GLOBALS['error_log'][3][0]);
|
||||||
|
$this->assertSame($GLOBALS['error_log'][3][1], $type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user