From b587a00f8334acb122a845a0415f4b3394160b66 Mon Sep 17 00:00:00 2001 From: lux Date: Fri, 8 Dec 2023 19:08:15 -0600 Subject: [PATCH 1/3] Fixes support for nested loggers when using multiple Logger instances --- lib/Analog/Analog.php | 4 ++++ lib/Analog/Handler/Buffer.php | 18 ++++++++++++++++++ lib/Analog/Handler/LevelBuffer.php | 21 +++++++++++++++++++++ lib/Analog/Handler/LevelName.php | 12 ++++++++++++ lib/Analog/Handler/Multi.php | 25 +++++++++++++++++++++++++ lib/Analog/Handler/Threshold.php | 16 ++++++++++++++++ 6 files changed, 96 insertions(+) diff --git a/lib/Analog/Analog.php b/lib/Analog/Analog.php index cd51c14..ccfbf5a 100644 --- a/lib/Analog/Analog.php +++ b/lib/Analog/Analog.php @@ -174,6 +174,10 @@ private static function write ($struct) { $handler = self::handler (); if (! $handler instanceof \Closure) { + if (is_object ($handler) && method_exists ($handler, 'log')) { + return $handler->log ($struct); + } + $handler = \Analog\Handler\File::init ($handler); } return $handler ($struct); diff --git a/lib/Analog/Handler/Buffer.php b/lib/Analog/Handler/Buffer.php index 298ad6e..1277bd7 100644 --- a/lib/Analog/Handler/Buffer.php +++ b/lib/Analog/Handler/Buffer.php @@ -56,4 +56,22 @@ public static function close () { $handler = self::$handler; return $handler (self::$buffer, true); } + + /** + * For use as a class instance + */ + private $_handler; + private $_buffer = ''; + + public function __construct ($handler) { + $this->_handler = $handler; + } + + public function log ($info) { + $this->_buffer .= vsprintf (\Analog\Analog::$format, $info); + } + + public function __destruct () { + call_user_func ($this->_handler, $this->_buffer, true); + } } diff --git a/lib/Analog/Handler/LevelBuffer.php b/lib/Analog/Handler/LevelBuffer.php index 4ab6a71..35e0a9e 100644 --- a/lib/Analog/Handler/LevelBuffer.php +++ b/lib/Analog/Handler/LevelBuffer.php @@ -60,4 +60,25 @@ public static function flush () { $handler = self::$handler; return $handler (self::$buffer, true); } + + /** + * For use as a class instance + */ + private $_handler; + private $_until_level = 2; + private $_buffer = ''; + + public function __construct ($handler, $until_level = 2) { + $this->_handler = $handler; + $this->_until_level = $until_level; + } + + public function log ($info) { + $this->_buffer .= vsprintf (\Analog\Analog::$format, $info); + if ($info['level'] <= $this->_until_level) { + // flush and reset the buffer + call_user_func ($this->_handler, $this->_buffer, true); + $this->_buffer = ''; + } + } } \ No newline at end of file diff --git a/lib/Analog/Handler/LevelName.php b/lib/Analog/Handler/LevelName.php index b378f68..3ef6243 100644 --- a/lib/Analog/Handler/LevelName.php +++ b/lib/Analog/Handler/LevelName.php @@ -47,4 +47,16 @@ public static function init ($handler) { }; } + private $_handler; + + public function __construct ($handler) { + $this->_handler = $handler; + } + + public function log ($info) { + if (isset(self::$log_levels[$info['level']])) { + $info['level'] = self::$log_levels[$info['level']]; + } + call_user_func ($this->_handler, $info); + } } \ No newline at end of file diff --git a/lib/Analog/Handler/Multi.php b/lib/Analog/Handler/Multi.php index 332e5ed..910a5eb 100644 --- a/lib/Analog/Handler/Multi.php +++ b/lib/Analog/Handler/Multi.php @@ -52,4 +52,29 @@ public static function init ($handlers) { } }; } + + /** + * For use as a class instance + */ + private $_handlers; + + public function __construct ($handlers) { + $this->_handlers = $handlers; + } + + public function log ($info) { + $level = is_numeric ($info['level']) ? $info['level'] : 3; + while ($level <= 7) { + if (isset ($this->_handlers[$level])) { + if (! is_array ($this->_handlers[$level])) { + $this->_handlers[$level] = array ($this->_handlers[$level]); + } + + foreach ($this->_handlers[$level] as $handler) { + $handler ($info); + } + } + $level++; + } + } } \ No newline at end of file diff --git a/lib/Analog/Handler/Threshold.php b/lib/Analog/Handler/Threshold.php index f435cbf..0f132d1 100644 --- a/lib/Analog/Handler/Threshold.php +++ b/lib/Analog/Handler/Threshold.php @@ -42,4 +42,20 @@ public static function init ($handler, $until_level = 3) { }; } + /** + * For use as a class instance + */ + private $_handler; + private $_until_level = 3; + + public function __construct ($handler, $until_level = 3) { + $this->_handler = $handler; + $this->_until_level = $until_level; + } + + public function log ($info) { + if ($inf['level'] <= $this->_until_level) { + call_user_func ($this->_handler, $info); + } + } } \ No newline at end of file From b0a7f02206b7854ad06199a02a73f683d84e7d24 Mon Sep 17 00:00:00 2001 From: lux Date: Fri, 8 Dec 2023 19:15:06 -0600 Subject: [PATCH 2/3] Added comment --- lib/Analog/Handler/LevelName.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/Analog/Handler/LevelName.php b/lib/Analog/Handler/LevelName.php index 3ef6243..48043f1 100644 --- a/lib/Analog/Handler/LevelName.php +++ b/lib/Analog/Handler/LevelName.php @@ -47,6 +47,9 @@ public static function init ($handler) { }; } + /** + * For use as a class instance + */ private $_handler; public function __construct ($handler) { From e6f937dfe64808ab18c1abe9611bc87060e1b05b Mon Sep 17 00:00:00 2001 From: lux Date: Sun, 10 Dec 2023 13:28:39 -0600 Subject: [PATCH 3/3] These can be removed and rely on the instances instead now --- lib/Analog/Handler/Buffer.php | 30 +----------------------------- lib/Analog/Handler/LevelBuffer.php | 28 +--------------------------- lib/Analog/Handler/LevelName.php | 15 +-------------- lib/Analog/Handler/Multi.php | 19 +------------------ lib/Analog/Handler/Threshold.php | 15 ++------------- 5 files changed, 6 insertions(+), 101 deletions(-) diff --git a/lib/Analog/Handler/Buffer.php b/lib/Analog/Handler/Buffer.php index 1277bd7..28de1ce 100644 --- a/lib/Analog/Handler/Buffer.php +++ b/lib/Analog/Handler/Buffer.php @@ -21,40 +21,12 @@ * to the buffer. */ class Buffer { - /** - * This builds a log string of all messages logged. - */ - public static $buffer = ''; - - /** - * This contains the handler to send to on close. - */ - private static $handler; - - /** - * A copy of our destructor object that will call close() on our behalf, - * since static classes can't have their own __destruct() methods. - */ - private static $destructor; /** * Accepts another handler function to be used on close(). */ public static function init ($handler) { - self::$handler = $handler; - self::$destructor = new \Analog\Handler\Buffer\Destructor (); - - return function ($info) { - Buffer::$buffer .= vsprintf (\Analog\Analog::$format, $info); - }; - } - - /** - * Passes the buffered log to the final $handler. - */ - public static function close () { - $handler = self::$handler; - return $handler (self::$buffer, true); + return new Buffer ($handler); } /** diff --git a/lib/Analog/Handler/LevelBuffer.php b/lib/Analog/Handler/LevelBuffer.php index 35e0a9e..e6b0346 100644 --- a/lib/Analog/Handler/LevelBuffer.php +++ b/lib/Analog/Handler/LevelBuffer.php @@ -26,39 +26,13 @@ * 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\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 static function flush () { - $handler = self::$handler; - return $handler (self::$buffer, true); + return new LevelBuffer ($handler, $until_level); } /** diff --git a/lib/Analog/Handler/LevelName.php b/lib/Analog/Handler/LevelName.php index 48043f1..c05d424 100644 --- a/lib/Analog/Handler/LevelName.php +++ b/lib/Analog/Handler/LevelName.php @@ -30,21 +30,8 @@ class LevelName { \Analog\Analog::URGENT => 'URGENT' ); - /** - * This contains the handler to send to - */ - public static $handler; - public static function init ($handler) { - self::$handler = $handler; - - return function ($info) { - if (isset(self::$log_levels[$info['level']])) { - $info['level'] = self::$log_levels[$info['level']]; - } - $handler = LevelName::$handler; - $handler ($info); - }; + return new LevelName ($handler); } /** diff --git a/lib/Analog/Handler/Multi.php b/lib/Analog/Handler/Multi.php index 910a5eb..9bb1f6f 100644 --- a/lib/Analog/Handler/Multi.php +++ b/lib/Analog/Handler/Multi.php @@ -33,24 +33,7 @@ */ class Multi { public static function init ($handlers) { - return function ($info) use ($handlers) { - $level = is_numeric ($info['level']) ? $info['level'] : 3; - while ($level <= 7) { - if ( isset ( $handlers[ $level ] ) ) { - - if ( ! is_array( $handlers[ $level ] ) ) { - $handlers[ $level ] = array( $handlers[ $level ] ); - } - - foreach ( $handlers[ $level ] as $handler ) { - $handler( $info ); - } - - return; - } - $level++; - } - }; + return new Multi ($handlers); } /** diff --git a/lib/Analog/Handler/Threshold.php b/lib/Analog/Handler/Threshold.php index 0f132d1..d539bcb 100644 --- a/lib/Analog/Handler/Threshold.php +++ b/lib/Analog/Handler/Threshold.php @@ -22,24 +22,13 @@ * to the buffer. */ class Threshold { - /** - * This contains the handler to send to on close. - */ - public static $handler; /** * Accepts another handler function to be used on close(). * $until_level defaults to ERROR. */ public static function init ($handler, $until_level = 3) { - self::$handler = $handler; - - return function ($info) use ($until_level) { - if ($info['level'] <= $until_level) { - $handler = Threshold::$handler; - $handler ($info); - } - }; + return new Threshold ($handler, $until_level); } /** @@ -54,7 +43,7 @@ public function __construct ($handler, $until_level = 3) { } public function log ($info) { - if ($inf['level'] <= $this->_until_level) { + if ($info['level'] <= $this->_until_level) { call_user_func ($this->_handler, $info); } }