Skip to content

Commit

Permalink
Merging with origin
Browse files Browse the repository at this point in the history
  • Loading branch information
lux committed Dec 10, 2023
2 parents 48579aa + 606a21c commit 9ec168a
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 84 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
40 changes: 15 additions & 25 deletions lib/Analog/Handler/Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,29 @@
* 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);
};
return new Buffer ($handler);
}

/**
* Passes the buffered log to the final $handler.
* For use as a class instance
*/
public static function close () {
$handler = self::$handler;
return $handler (self::$buffer, true);
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);
}
}
41 changes: 18 additions & 23 deletions lib/Analog/Handler/LevelBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,33 @@
* 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 = '';
}
};
return new LevelBuffer ($handler, $until_level);
}

/**
* Passes the buffered log to the final $handler.
* For use as a class instance
*/
public static function flush () {
$handler = self::$handler;
return $handler (self::$buffer, true);
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 = '';
}
}
}
26 changes: 14 additions & 12 deletions lib/Analog/Handler/LevelName.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@ class LevelName {
\Analog\Analog::URGENT => 'URGENT'
);

public static function init ($handler) {
return new LevelName ($handler);
}

/**
* This contains the handler to send to
* For use as a class instance
*/
public static $handler;
private $_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);
};
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);
}
}
34 changes: 21 additions & 13 deletions lib/Analog/Handler/Multi.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,31 @@
*/
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 ] ) ) {
return new Multi ($handlers);
}

/**
* For use as a class instance
*/
private $_handlers;

if ( ! is_array( $handlers[ $level ] ) ) {
$handlers[ $level ] = array( $handlers[ $level ] );
}
public function __construct ($handlers) {
$this->_handlers = $handlers;
}

foreach ( $handlers[ $level ] as $handler ) {
$handler( $info );
}
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]);
}

return;
foreach ($this->_handlers[$level] as $handler) {
$handler ($info);
}
$level++;
}
};
$level++;
}
}
}
27 changes: 16 additions & 11 deletions lib/Analog/Handler/Threshold.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,29 @@
* 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 new Threshold ($handler, $until_level);
}

return function ($info) use ($until_level) {
if ($info['level'] <= $until_level) {
$handler = Threshold::$handler;
$handler ($info);
}
};
/**
* 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 ($info['level'] <= $this->_until_level) {
call_user_func ($this->_handler, $info);
}
}
}

0 comments on commit 9ec168a

Please sign in to comment.