2626 * to the buffer.
2727 */
2828class 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}
0 commit comments