Permalink
Browse files

Added LevelBuffer handler

  • Loading branch information...
1 parent 9c72ed9 commit 3c904f9a2224375bd104bf15d73415cbbdf4b6e7 @jbroadway committed Jan 8, 2012
Showing with 86 additions and 0 deletions.
  1. +63 −0 Analog/Handler/LevelBuffer.php
  2. +1 −0 README.md
  3. +22 −0 examples/levelbuffer.php
@@ -0,0 +1,63 @@
+<?php
+
+namespace Analog\Handler;
+
+/**
+ * Buffers messages to be sent as a batch to another handler only when a
+ * message of a certain level threshold has been received. This means for
+ * example that you can trigger a handler only if an error has occurred.
+ * Currently only works with the Mail handler.
+ *
+ * Inspired by the Monolog FingersCrossedHandler.
+ *
+ * Usage:
+ *
+ * Analog::handler (Analog\Handler\LevelBuffer::init (
+ * Analog\Handler\Mail::init ($to, $subject, $from),
+ * Analog::ERROR
+ * ));
+ *
+ * // will all be buffered until something ERROR or above is logged
+ * Analog::log ('Message one', Analog::DEBUG);
+ * Analog::log ('Message two', Analog::WARNING);
+ * Analog::log ('Message three', Analog::URGENT);
+ *
+ * Note: Uses Analog::$format to format the messages as they're appended
+ * to the buffer.
+ */
+class LevelBuffer {
+ /**
+ * This builds a log string of all messages logged.
+ */
+ public static $buffer = '';
+
+ /**
+ * This contains the handler to send to on close.
+ */
+ private static $handler;
+
+ /**
+ * Accepts another handler function to be used on close().
+ * $until_level defaults to CRITICAL.
+ */
+ public static function init ($handler, $until_level = 2) {
+ self::$handler = $handler;
+
+ return function ($info) use ($until_level) {
+ LevelBuffer::$buffer .= vsprintf (\Analog::$format, $info);
+ if ($info['level'] <= $until_level) {
+ // flush and reset the buffer
+ LevelBuffer::flush ();
+ LevelBuffer::$buffer = '';
+ }
+ };
+ }
+
+ /**
+ * Passes the buffered log to the final $handler.
+ */
+ public function flush () {
+ $handler = self::$handler;
+ return $handler (self::$buffer, true);
+ }
+}
View
@@ -18,6 +18,7 @@ examples for each in the examples folder. These include:
* Buffer - Buffer messages to send all at once (works with Mail handler)
* File - Append messages to a file
* FirePHP - Send messages to [FirePHP](http://www.firephp.org/) browser plugin
+* LevelBuffer - Buffer messages and send only if sufficient error level reached
* Mail - Send email notices
* Mongo - Save to MongoDB collection
* Multi - Send different log levels to different handlers
View
@@ -0,0 +1,22 @@
+<?php
+
+require 'autoload.php';
+
+Analog::handler (Analog\Handler\LevelBuffer::init (
+ Analog\Handler\Mail::init (
+ 'john.luxford@gmail.com',
+ 'Attention: A critical error has occurred',
+ 'noreply@example.com'
+ ),
+ Analog::CRITICAL
+));
+
+// none of these will trigger sending the log
+Analog::log ('Debugging...', Analog::DEBUG);
+Analog::log ('Minor warning...', Analog::WARNING);
+Analog::log ('An error...', Analog::ERROR);
+
+// but this will, and will include all the others in the log
+Analog::log ('Oh noes!', Analog::URGENT);
+
+?>

0 comments on commit 3c904f9

Please sign in to comment.