Skip to content

Commit

Permalink
Added support for Graylog2
Browse files Browse the repository at this point in the history
  • Loading branch information
jbroadway committed Oct 3, 2012
1 parent cc76d7e commit 8b67e51
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ with examples for each in the examples folder. These include:
* Buffer - Buffer messages to send all at once (works with File, Mail, Stderr, and Variable handlers)
* File - Append messages to a file
* FirePHP - Send messages to [FirePHP](http://www.firephp.org/) browser plugin
* GELF - Send message to the [Graylog2](http://www.graylog2.org/) log management server
* LevelBuffer - Buffer messages and send only if sufficient error level reached
* Mail - Send email notices
* Mongo - Save to MongoDB collection
Expand Down
16 changes: 16 additions & 0 deletions examples/gelf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

// 1. Install the GELF classes from https://github.com/Graylog2/gelf-php
require 'GELFMessage.php';
require 'GELFMessagePublisher.php';

require '../lib/Analog.php';

Analog::handler (Analog\Handler\GELF::init (
'localhost'
));

Analog::log ('Error message');
Analog::log (array ('Debug info', __FILE__, __LINE__), Analog::DEBUG);

?>
48 changes: 48 additions & 0 deletions lib/Analog/Handler/GELF.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Analog\Handler;

/**
* Send the log message to a Graylog2 server (http://graylog2.org/).
*
* Usage:
*
* // First include the GELF classes from
* // https://github.com/Graylog2/gelf-php
* require 'GELFMessage.php';
* require 'GELFMessagePublisher.php';
*
* // Initialize the Analog GELF handler
* Analog::handler (Analog\Handler\GELF::init (
* '172.16.22.30'
* ));
*
* // Send a message with file and line number
* Analog::log (array ('Log me', __FILE__, __LINE__), Analog::DEBUG);
*
* // Send an ordinary message
* Analog::log ('An error message');
*/
class GELF {
public static function init ($host = '127.0.0.1', $port = \GELFMessagePublisher::GRAYLOG2_DEFAULT_PORT) {
$publisher = new \GELFMessagePublisher ($host, $port);

return function ($info) use ($publisher) {
$message = new \GELFMessage ();
$message->setHost ($info['machine']);
$message->setLevel ($info['level']);

if (is_array ($info['message'])) {
$message->setShortMessage ($info['message'][0]);
$message->setFullMessage ($info['message'][0]);
$message->setFile ($info['message'][1]);
$message->setLine ($info['message'][2]);
} else {
$message->setShortMessage ($info['message']);
$message->setFullMessage ($info['message']);
}

$publisher->publish ($message);
};
}
}

0 comments on commit 8b67e51

Please sign in to comment.