From d84a448781aaf8926eb3c050e2f38684c8e30104 Mon Sep 17 00:00:00 2001 From: Boy Baukema Date: Fri, 17 Oct 2014 16:20:06 +0200 Subject: [PATCH] Fix for issue #87, MessageParser is too greedy --- .../Log/Writer/Syslog/MessageParser.php | 29 ++++++++++++++----- .../Log/Writer/Syslog/MessageSplitter.php | 5 ++-- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/library/EngineBlock/Log/Writer/Syslog/MessageParser.php b/library/EngineBlock/Log/Writer/Syslog/MessageParser.php index 3cbe6d1271..9bf4a429c5 100644 --- a/library/EngineBlock/Log/Writer/Syslog/MessageParser.php +++ b/library/EngineBlock/Log/Writer/Syslog/MessageParser.php @@ -7,17 +7,32 @@ class EngineBlock_Log_Writer_Syslog_MessageParser */ public function parse(array $event) { - $message = isset($event['message']) - ? $this->_normalizeMessage($event['message']) : ''; + $message = isset($event['message']) ? $this->_normalizeMessage($event['message']) : ''; preg_match_all( - '/([^\]]*\[[a-zA-Z0-9 ]+\]\[[a-zA-Z0-9 ]+\](\[DUMP[^\]]*\])?)( .*)/', - $message, $matches + '/' . + '('. + '[^\]]*'. // .... (anything but a ]), followed by: + '\[[a-zA-Z0-9 ]+\]'. // [ ... ] + '\[[a-zA-Z0-9 ]+\]'. // [ ... ] + '(\[DUMP[^\]]*\])?'. // Optionally: [DUMP ...] + ')'. + '( .*)'. // Anything, starting with a space. + '/', + $message, + $matches ); + if (!isset($matches[1][0]) || !isset($matches[3][0])) { + return array( + 'prefix' => 'P[' . time() . '][' . rand(0, 1000000) . ']', + 'message' => $message + ); + } + return array( - 'prefix' => isset($matches[1][0]) ? $matches[1][0] : 'PREFIX REMOVED BY PARSER', - 'message' => isset($matches[3][0]) ? $matches[3][0] : 'MESSAGE REMOVED BY PARSER', + 'prefix' => $matches[1][0], + 'message' => $matches[3][0], ); } @@ -30,7 +45,7 @@ public function parse(array $event) * Serialized content is prepended with '!FORMAT_[type]', this * notation is parsed by logparse.sh * - * @param mixed data structure to dump + * @param string $message structure to dump * @return string */ protected function _normalizeMessage($message) diff --git a/library/EngineBlock/Log/Writer/Syslog/MessageSplitter.php b/library/EngineBlock/Log/Writer/Syslog/MessageSplitter.php index d286362f01..b65e157040 100644 --- a/library/EngineBlock/Log/Writer/Syslog/MessageSplitter.php +++ b/library/EngineBlock/Log/Writer/Syslog/MessageSplitter.php @@ -14,9 +14,10 @@ public function __construct($messageSplitSize) * messages and logs them to parent::log(). Message size is determined * by EngineBlock_Log::MESSAGE_SPLIT_SIZE. * - * @param string $prefix Message prefix - * @param string $message Message to log + * @param string $prefix Message prefix + * @param string $message Message to log * @return array + * @throws InvalidArgumentException */ public function split($prefix, $message) {