Skip to content

Commit 9ec168a

Browse files
committed
Merging with origin
2 parents 48579aa + 606a21c commit 9ec168a

6 files changed

Lines changed: 88 additions & 84 deletions

File tree

lib/Analog/Analog.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ private static function write ($struct) {
174174
$handler = self::handler ();
175175

176176
if (! $handler instanceof \Closure) {
177+
if (is_object ($handler) && method_exists ($handler, 'log')) {
178+
return $handler->log ($struct);
179+
}
180+
177181
$handler = \Analog\Handler\File::init ($handler);
178182
}
179183
return $handler ($struct);

lib/Analog/Handler/Buffer.php

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,29 @@
2121
* to the buffer.
2222
*/
2323
class Buffer {
24-
/**
25-
* This builds a log string of all messages logged.
26-
*/
27-
public static $buffer = '';
28-
29-
/**
30-
* This contains the handler to send to on close.
31-
*/
32-
private static $handler;
33-
34-
/**
35-
* A copy of our destructor object that will call close() on our behalf,
36-
* since static classes can't have their own __destruct() methods.
37-
*/
38-
private static $destructor;
3924

4025
/**
4126
* Accepts another handler function to be used on close().
4227
*/
4328
public static function init ($handler) {
44-
self::$handler = $handler;
45-
self::$destructor = new \Analog\Handler\Buffer\Destructor ();
46-
47-
return function ($info) {
48-
Buffer::$buffer .= vsprintf (\Analog\Analog::$format, $info);
49-
};
29+
return new Buffer ($handler);
5030
}
5131

5232
/**
53-
* Passes the buffered log to the final $handler.
33+
* For use as a class instance
5434
*/
55-
public static function close () {
56-
$handler = self::$handler;
57-
return $handler (self::$buffer, true);
35+
private $_handler;
36+
private $_buffer = '';
37+
38+
public function __construct ($handler) {
39+
$this->_handler = $handler;
40+
}
41+
42+
public function log ($info) {
43+
$this->_buffer .= vsprintf (\Analog\Analog::$format, $info);
44+
}
45+
46+
public function __destruct () {
47+
call_user_func ($this->_handler, $this->_buffer, true);
5848
}
5949
}

lib/Analog/Handler/LevelBuffer.php

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,33 @@
2626
* to the buffer.
2727
*/
2828
class LevelBuffer {
29-
/**
30-
* This builds a log string of all messages logged.
31-
*/
32-
public static $buffer = '';
33-
34-
/**
35-
* This contains the handler to send to on close.
36-
*/
37-
private static $handler;
3829

3930
/**
4031
* Accepts another handler function to be used on close().
4132
* $until_level defaults to CRITICAL.
4233
*/
4334
public static function init ($handler, $until_level = 2) {
44-
self::$handler = $handler;
45-
46-
return function ($info) use ($until_level) {
47-
LevelBuffer::$buffer .= vsprintf (\Analog\Analog::$format, $info);
48-
if ($info['level'] <= $until_level) {
49-
// flush and reset the buffer
50-
LevelBuffer::flush ();
51-
LevelBuffer::$buffer = '';
52-
}
53-
};
35+
return new LevelBuffer ($handler, $until_level);
5436
}
5537

5638
/**
57-
* Passes the buffered log to the final $handler.
39+
* For use as a class instance
5840
*/
59-
public static function flush () {
60-
$handler = self::$handler;
61-
return $handler (self::$buffer, true);
41+
private $_handler;
42+
private $_until_level = 2;
43+
private $_buffer = '';
44+
45+
public function __construct ($handler, $until_level = 2) {
46+
$this->_handler = $handler;
47+
$this->_until_level = $until_level;
48+
}
49+
50+
public function log ($info) {
51+
$this->_buffer .= vsprintf (\Analog\Analog::$format, $info);
52+
if ($info['level'] <= $this->_until_level) {
53+
// flush and reset the buffer
54+
call_user_func ($this->_handler, $this->_buffer, true);
55+
$this->_buffer = '';
56+
}
6257
}
6358
}

lib/Analog/Handler/LevelName.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,23 @@ class LevelName {
3030
\Analog\Analog::URGENT => 'URGENT'
3131
);
3232

33+
public static function init ($handler) {
34+
return new LevelName ($handler);
35+
}
36+
3337
/**
34-
* This contains the handler to send to
38+
* For use as a class instance
3539
*/
36-
public static $handler;
40+
private $_handler;
3741

38-
public static function init ($handler) {
39-
self::$handler = $handler;
40-
41-
return function ($info) {
42-
if (isset(self::$log_levels[$info['level']])) {
43-
$info['level'] = self::$log_levels[$info['level']];
44-
}
45-
$handler = LevelName::$handler;
46-
$handler ($info);
47-
};
42+
public function __construct ($handler) {
43+
$this->_handler = $handler;
4844
}
4945

46+
public function log ($info) {
47+
if (isset(self::$log_levels[$info['level']])) {
48+
$info['level'] = self::$log_levels[$info['level']];
49+
}
50+
call_user_func ($this->_handler, $info);
51+
}
5052
}

lib/Analog/Handler/Multi.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,31 @@
3333
*/
3434
class Multi {
3535
public static function init ($handlers) {
36-
return function ($info) use ($handlers) {
37-
$level = is_numeric ($info['level']) ? $info['level'] : 3;
38-
while ($level <= 7) {
39-
if ( isset ( $handlers[ $level ] ) ) {
36+
return new Multi ($handlers);
37+
}
38+
39+
/**
40+
* For use as a class instance
41+
*/
42+
private $_handlers;
4043

41-
if ( ! is_array( $handlers[ $level ] ) ) {
42-
$handlers[ $level ] = array( $handlers[ $level ] );
43-
}
44+
public function __construct ($handlers) {
45+
$this->_handlers = $handlers;
46+
}
4447

45-
foreach ( $handlers[ $level ] as $handler ) {
46-
$handler( $info );
47-
}
48+
public function log ($info) {
49+
$level = is_numeric ($info['level']) ? $info['level'] : 3;
50+
while ($level <= 7) {
51+
if (isset ($this->_handlers[$level])) {
52+
if (! is_array ($this->_handlers[$level])) {
53+
$this->_handlers[$level] = array ($this->_handlers[$level]);
54+
}
4855

49-
return;
56+
foreach ($this->_handlers[$level] as $handler) {
57+
$handler ($info);
5058
}
51-
$level++;
5259
}
53-
};
60+
$level++;
61+
}
5462
}
5563
}

lib/Analog/Handler/Threshold.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,29 @@
2222
* to the buffer.
2323
*/
2424
class Threshold {
25-
/**
26-
* This contains the handler to send to on close.
27-
*/
28-
public static $handler;
2925

3026
/**
3127
* Accepts another handler function to be used on close().
3228
* $until_level defaults to ERROR.
3329
*/
3430
public static function init ($handler, $until_level = 3) {
35-
self::$handler = $handler;
31+
return new Threshold ($handler, $until_level);
32+
}
3633

37-
return function ($info) use ($until_level) {
38-
if ($info['level'] <= $until_level) {
39-
$handler = Threshold::$handler;
40-
$handler ($info);
41-
}
42-
};
34+
/**
35+
* For use as a class instance
36+
*/
37+
private $_handler;
38+
private $_until_level = 3;
39+
40+
public function __construct ($handler, $until_level = 3) {
41+
$this->_handler = $handler;
42+
$this->_until_level = $until_level;
4343
}
4444

45+
public function log ($info) {
46+
if ($info['level'] <= $this->_until_level) {
47+
call_user_func ($this->_handler, $info);
48+
}
49+
}
4550
}

0 commit comments

Comments
 (0)