From 3c0c213b414f89310d78837378bfe7a44db5a2a7 Mon Sep 17 00:00:00 2001 From: Bruno Pedro Date: Tue, 8 Apr 2014 09:25:42 +0200 Subject: [PATCH 1/2] Set @timestamp default value and ignore other empty values. --- src/Monolog/Formatter/LogstashFormatter.php | 49 +++++++++++++-------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/src/Monolog/Formatter/LogstashFormatter.php b/src/Monolog/Formatter/LogstashFormatter.php index b084c4a5..20d67b12 100644 --- a/src/Monolog/Formatter/LogstashFormatter.php +++ b/src/Monolog/Formatter/LogstashFormatter.php @@ -85,14 +85,17 @@ class LogstashFormatter extends NormalizerFormatter protected function formatV0(array $record) { + if (empty($record['datetime'])) { + $record['datetime'] = gmdate('c'); + } $message = array( '@timestamp' => $record['datetime'], - '@message' => $record['message'], - '@tags' => array($record['channel']), + '@message' => @$record['message'], + '@tags' => array(@$record['channel']), '@source' => $this->systemName, '@fields' => array( - 'channel' => $record['channel'], - 'level' => $record['level'] + 'channel' => @$record['channel'], + 'level' => @$record['level'] ) ); @@ -106,12 +109,15 @@ class LogstashFormatter extends NormalizerFormatter if (isset($record['extra']['url'])) { $message['@source_path'] = $record['extra']['url']; } - foreach ($record['extra'] as $key => $val) { - $message['@fields'][$this->extraPrefix . $key] = $val; + if (!empty($record['extra'])) { + foreach ($record['extra'] as $key => $val) { + $message['@fields'][$this->extraPrefix . $key] = $val; + } } - - foreach ($record['context'] as $key => $val) { - $message['@fields'][$this->contextPrefix . $key] = $val; + if (!empty($record['context'])) { + foreach ($record['context'] as $key => $val) { + $message['@fields'][$this->contextPrefix . $key] = $val; + } } return $message; @@ -119,26 +125,31 @@ class LogstashFormatter extends NormalizerFormatter protected function formatV1(array $record) { + if (empty($record['datetime'])) { + $record['datetime'] = gmdate('c'); + } $message = array( '@timestamp' => $record['datetime'], '@version' => 1, - 'message' => $record['message'], + 'message' => @$record['message'], 'host' => $this->systemName, - 'type' => $record['channel'], - 'channel' => $record['channel'], - 'level' => $record['level_name'] + 'type' => @$record['channel'], + 'channel' => @$record['channel'], + 'level' => @$record['level_name'] ); if ($this->applicationName) { $message['type'] = $this->applicationName; } - - foreach ($record['extra'] as $key => $val) { - $message[$this->extraPrefix . $key] = $val; + if (!empty($record['extra'])) { + foreach ($record['extra'] as $key => $val) { + $message[$this->extraPrefix . $key] = $val; + } } - - foreach ($record['context'] as $key => $val) { - $message[$this->contextPrefix . $key] = $val; + if (!empty($record['context'])) { + foreach ($record['context'] as $key => $val) { + $message[$this->contextPrefix . $key] = $val; + } } return $message; From ffb170183435df21b38bc24ba1fd20da153cd52e Mon Sep 17 00:00:00 2001 From: Bruno Pedro Date: Tue, 8 Apr 2014 10:43:04 +0200 Subject: [PATCH 2/2] Checking each entry individually instead of using @. --- src/Monolog/Formatter/LogstashFormatter.php | 34 +++++++++++++-------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/Monolog/Formatter/LogstashFormatter.php b/src/Monolog/Formatter/LogstashFormatter.php index 20d67b12..7a7b3b3c 100644 --- a/src/Monolog/Formatter/LogstashFormatter.php +++ b/src/Monolog/Formatter/LogstashFormatter.php @@ -90,19 +90,22 @@ class LogstashFormatter extends NormalizerFormatter } $message = array( '@timestamp' => $record['datetime'], - '@message' => @$record['message'], - '@tags' => array(@$record['channel']), '@source' => $this->systemName, - '@fields' => array( - 'channel' => @$record['channel'], - 'level' => @$record['level'] - ) + '@fields' => array() ); - + if (isset($record['message'])) { + $message['@message'] = $record['message']; + } + if (isset($record['channel'])) { + $message['@tags'] = array($record['channel']); + $message['@fields']['channel'] = $record['channel']; + } + if (isset($record['level'])) { + $message['@fields']['level'] = $record['level']; + } if ($this->applicationName) { $message['@type'] = $this->applicationName; } - if (isset($record['extra']['server'])) { $message['@source_host'] = $record['extra']['server']; } @@ -131,13 +134,18 @@ class LogstashFormatter extends NormalizerFormatter $message = array( '@timestamp' => $record['datetime'], '@version' => 1, - 'message' => @$record['message'], 'host' => $this->systemName, - 'type' => @$record['channel'], - 'channel' => @$record['channel'], - 'level' => @$record['level_name'] ); - + if (isset($record['message'])) { + $message['message'] = $record['message']; + } + if (isset($record['channel'])) { + $message['type'] = $record['channel']; + $message['channel'] = $record['channel']; + } + if (isset($record['level_name'])) { + $message['level'] = $record['level_name']; + } if ($this->applicationName) { $message['type'] = $this->applicationName; }