diff options
author | Thomas Steur <tsteur@users.noreply.github.com> | 2015-09-10 10:50:02 +0300 |
---|---|---|
committer | diosmosis <benaka@piwik.pro> | 2015-09-22 02:20:58 +0300 |
commit | 8a910fb2f4e413f8dd0786ddb04d1d244cbd9ae9 (patch) | |
tree | eb993f0f4be5691572178176426c9f03938b5c30 /plugins/Monolog | |
parent | 949bc2b86f28ec8f8c070535422ab54d9ed3eb53 (diff) |
Split multi-line log messages into multiple log messages if file handler
is used
Diffstat (limited to 'plugins/Monolog')
-rw-r--r-- | plugins/Monolog/Formatter/LineMessageFormatter.php | 34 | ||||
-rw-r--r-- | plugins/Monolog/config/config.php | 5 | ||||
-rw-r--r-- | plugins/Monolog/tests/Unit/Formatter/LineMessageFormatterTest.php | 28 |
3 files changed, 57 insertions, 10 deletions
diff --git a/plugins/Monolog/Formatter/LineMessageFormatter.php b/plugins/Monolog/Formatter/LineMessageFormatter.php index 6b81f2daec..8224ff71f6 100644 --- a/plugins/Monolog/Formatter/LineMessageFormatter.php +++ b/plugins/Monolog/Formatter/LineMessageFormatter.php @@ -23,12 +23,16 @@ class LineMessageFormatter implements FormatterInterface */ private $logMessageFormat; + private $allowInlineLineBreaks; + /** * @param string $logMessageFormat + * @param bool $allowInlineLineBreaks If disabled, a log message will be created for each line */ - public function __construct($logMessageFormat) + public function __construct($logMessageFormat, $allowInlineLineBreaks = true) { $this->logMessageFormat = $logMessageFormat; + $this->allowInlineLineBreaks = $allowInlineLineBreaks; } public function format(array $record) @@ -36,18 +40,34 @@ class LineMessageFormatter implements FormatterInterface $class = isset($record['extra']['class']) ? $record['extra']['class'] : ''; $date = $record['datetime']->format('Y-m-d H:i:s'); - $message = $this->prefixMessageWithRequestId($record); + $message = $record['message']; + + if ($this->allowInlineLineBreaks) { + $message = str_replace("\n", "\n ", $message); // intend lines + $messages = array($message); + } else { + $messages = explode("\n", $message); + } + + $total = ''; + + foreach ($messages as $message) { + $message = $this->prefixMessageWithRequestId($record, $message); + $total .= $this->formatMessage($class, $message, $date, $record); + } + return $total; + } + + private function formatMessage($class, $message, $date, $record) + { $message = str_replace( array('%tag%', '%message%', '%datetime%', '%level%'), array($class, $message, $date, $record['level_name']), $this->logMessageFormat ); - $message = str_replace("\n", "\n ", $message); - $message .= "\n"; - return $message; } @@ -60,11 +80,11 @@ class LineMessageFormatter implements FormatterInterface return $records; } - private function prefixMessageWithRequestId(array $record) + private function prefixMessageWithRequestId(array $record, $message) { $requestId = isset($record['extra']['request_id']) ? $record['extra']['request_id'] : ''; - $message = trim($record['message']); + $message = trim($message); if ($requestId) { $message = '[' . $requestId . '] ' . $message; diff --git a/plugins/Monolog/config/config.php b/plugins/Monolog/config/config.php index 6a7fb0130f..95463b91de 100644 --- a/plugins/Monolog/config/config.php +++ b/plugins/Monolog/config/config.php @@ -46,7 +46,10 @@ return array( 'Piwik\Plugins\Monolog\Handler\FileHandler' => DI\object() ->constructor(DI\get('log.file.filename'), DI\get('log.level')) - ->method('setFormatter', DI\get('Piwik\Plugins\Monolog\Formatter\LineMessageFormatter')), + ->method('setFormatter', DI\get('log.lineMessageFormatter.file')), + + 'log.lineMessageFormatter.file' => DI\object('Piwik\Plugins\Monolog\Formatter\LineMessageFormatter') + ->constructorParameter('allowInlineLineBreaks', false), 'Piwik\Plugins\Monolog\Handler\DatabaseHandler' => DI\object() ->constructor(DI\get('log.level')) diff --git a/plugins/Monolog/tests/Unit/Formatter/LineMessageFormatterTest.php b/plugins/Monolog/tests/Unit/Formatter/LineMessageFormatterTest.php index eac798e9c7..c98182e357 100644 --- a/plugins/Monolog/tests/Unit/Formatter/LineMessageFormatterTest.php +++ b/plugins/Monolog/tests/Unit/Formatter/LineMessageFormatterTest.php @@ -64,7 +64,7 @@ class LineMessageFormatterTest extends \PHPUnit_Framework_TestCase */ public function it_should_indent_multiline_message() { - $formatter = new LineMessageFormatter('%message%'); + $formatter = new LineMessageFormatter('%level% %message%'); $record = array( 'message' => "Hello world\ntest\ntest", @@ -73,7 +73,7 @@ class LineMessageFormatterTest extends \PHPUnit_Framework_TestCase ); $formatted = <<<LOG -Hello world +ERROR Hello world test test @@ -81,4 +81,28 @@ LOG; $this->assertEquals($formatted, $formatter->format($record)); } + + /** + * @test + */ + public function it_should_split_inline_line_breaks_into_many_messages_if_disabled() + { + $formatter = new LineMessageFormatter('%level% %message%', $allowInlineLineBreaks = false); + + $record = array( + 'message' => "Hello world\ntest\ntest", + 'datetime' => DateTime::createFromFormat('U', 0), + 'level_name' => 'ERROR', + 'extra' => array('request_id' => '1234') + ); + + $formatted = <<<LOG +ERROR [1234] Hello world +ERROR [1234] test +ERROR [1234] test + +LOG; + + $this->assertEquals($formatted, $formatter->format($record)); + } } |