Skip to content

Commit

Permalink
Fixes support for nested loggers when using multiple Logger instances
Browse files Browse the repository at this point in the history
  • Loading branch information
lux committed Dec 9, 2023
1 parent c0e16a9 commit b587a00
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/Analog/Analog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions lib/Analog/Handler/Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
21 changes: 21 additions & 0 deletions lib/Analog/Handler/LevelBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
}
}
}
12 changes: 12 additions & 0 deletions lib/Analog/Handler/LevelName.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
25 changes: 25 additions & 0 deletions lib/Analog/Handler/Multi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
}
}
16 changes: 16 additions & 0 deletions lib/Analog/Handler/Threshold.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit b587a00

Please sign in to comment.