Permalink
Browse files

Added support for Graylog2

  • Loading branch information...
jbroadway committed Oct 3, 2012
1 parent cc76d7e commit 8b67e5101e2f9118c087ce4524bb07cf4f1bd09e
Showing with 65 additions and 0 deletions.
  1. +1 −0 README.md
  2. +16 −0 examples/gelf.php
  3. +48 −0 lib/Analog/Handler/GELF.php
View
@@ -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
View
@@ -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);
+
+?>
@@ -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.