Skip to content

Commit

Permalink
Making it easier to use the logger without having to use mutators in
Browse files Browse the repository at this point in the history
logstash
  • Loading branch information
lorenzo committed Jun 21, 2013
1 parent f6bc13b commit b2b2564
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
17 changes: 11 additions & 6 deletions Log/Engine/LogstashLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,21 @@ class LogstashLog extends BaseLog {
* @return void
*/
public function write($type, $message) {
$log = array(
'@timestamp' => gmdate('c'),
'@type' => $type,
);

if (is_string($message)) {
$message = array('message' => $message);
$log['@message'] = $message;
} else {
$log['@fields'] = (array)$message;
}

$message['_type'] = $type;
$message = json_encode($message);

if ($this->_write($message) === false) {
$log = json_encode($log);
if ($this->_write($log) === false) {
$this->_close();
$this->_write($message);
$this->_write($log);
}
}

Expand Down
18 changes: 15 additions & 3 deletions Test/Case/Log/Engine/LogstashLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public function testWriteStringSuccess() {
->will($this->returnValue($file));

$engine->write('debug', 'This is a message');
$expected = json_encode(array('message' => 'This is a message', '_type' => 'debug'));
$expected = json_encode(array(
'@timestamp' => gmdate('c'),
'@type' => 'debug',
'@message' => 'This is a message'
));
fseek($file, 0);
$this->assertEquals($expected, fgets($file));
}
Expand All @@ -43,7 +47,11 @@ public function testWriteArraySuccess() {
->will($this->returnValue($file));

$engine->write('debug', array('key' => 'value'));
$expected = json_encode(array('key' => 'value', '_type' => 'debug'));
$expected = json_encode(array(
'@timestamp' => gmdate('c'),
'@type' => 'debug',
'@fields' => array('key' => 'value')
));
fseek($file, 0);
$this->assertEquals($expected, fgets($file));
}
Expand Down Expand Up @@ -71,7 +79,11 @@ public function testWriteLostConnection() {

fclose($file);
$engine->write('debug', array('key' => 'value'));
$expected = json_encode(array('key' => 'value', '_type' => 'debug'));
$expected = json_encode(array(
'@timestamp' => gmdate('c'),
'@type' => 'debug',
'@fields' => array('key' => 'value')
));
fseek($file2, 0);
$this->assertEquals($expected, fgets($file2));
}
Expand Down

0 comments on commit b2b2564

Please sign in to comment.