|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Phossa Project |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.4 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Library |
|
8
|
|
|
* @package Phossa2\Logger |
|
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
|
10
|
|
|
* @license http://mit-license.org/ MIT License |
|
11
|
|
|
* @link http://www.phossa.com/ |
|
12
|
|
|
*/ |
|
13
|
|
|
/*# declare(strict_types=1); */ |
|
14
|
|
|
|
|
15
|
|
|
namespace Phossa2\Logger\Handler; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Logger\Entry\LogEntryInterface; |
|
18
|
|
|
use Phossa2\Logger\Formatter\FormatterInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* BrowserHandler |
|
22
|
|
|
* |
|
23
|
|
|
* Send logs to browser console. |
|
24
|
|
|
* |
|
25
|
|
|
* - User MUST add HTTP header 'browerhandler' to html page to works with |
|
26
|
|
|
* this handler. |
|
27
|
|
|
* |
|
28
|
|
|
* - Modified from Monolog Handler\BrowserConsoleHandler |
|
29
|
|
|
* |
|
30
|
|
|
* @package Phossa2\Logger |
|
31
|
|
|
* @author Hong Zhang <[email protected]> |
|
32
|
|
|
* @see HandlerAbstract |
|
33
|
|
|
* @version 2.0.0 |
|
34
|
|
|
* @since 2.0.0 added |
|
35
|
|
|
* @since 2.0.1 updated constructor, isHandling() etc. |
|
36
|
|
|
*/ |
|
37
|
|
|
class BrowserHandler extends HandlerAbstract |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* cached messages |
|
41
|
|
|
* |
|
42
|
|
|
* @static |
|
43
|
|
|
* @var string[] |
|
44
|
|
|
* @access protected |
|
45
|
|
|
*/ |
|
46
|
|
|
protected static $messages = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritDoc} |
|
50
|
|
|
* |
|
51
|
|
|
* @since 2.0.1 removed level param |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct( |
|
54
|
|
|
FormatterInterface $formatter = null, |
|
55
|
|
|
/*# bool */ $stopPropagation = false |
|
56
|
|
|
) { |
|
57
|
|
|
// non CLI mode only |
|
58
|
|
|
if (!$this->isCliMode()) { |
|
59
|
|
|
// register flush method |
|
60
|
|
|
register_shutdown_function([__CLASS__, 'flush']); |
|
61
|
|
|
|
|
62
|
|
|
// call parent constructor |
|
63
|
|
|
parent::__construct($formatter, $stopPropagation); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritDoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function write(LogEntryInterface $logEntry) |
|
71
|
|
|
{ |
|
72
|
|
|
// record all messages |
|
73
|
|
|
static::$messages[] = $logEntry->getFormatted(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Only if not in CLI mode |
|
78
|
|
|
* |
|
79
|
|
|
* {@inheritDoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function isHandling(LogEntryInterface $logEntry)/*# : bool */ |
|
82
|
|
|
{ |
|
83
|
|
|
return !$this->isCliMode(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* flush the messages to browser by adding to HTML page |
|
88
|
|
|
* |
|
89
|
|
|
* @return void |
|
90
|
|
|
* @access public |
|
91
|
|
|
* @static |
|
92
|
|
|
* @api |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function flush() |
|
95
|
|
|
{ |
|
96
|
|
|
if (static::hasHttpHeader() && count(static::$messages)) { |
|
97
|
|
|
echo '<script>' , static::generateScript() , '</script>'; |
|
98
|
|
|
} |
|
99
|
|
|
static::$messages = []; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Is 'X-BrowserHandler' header set ? |
|
104
|
|
|
* |
|
105
|
|
|
* @return bool |
|
106
|
|
|
* @access protected |
|
107
|
|
|
* @static |
|
108
|
|
|
*/ |
|
109
|
|
|
protected static function hasHttpHeader()/*# : bool */ |
|
110
|
|
|
{ |
|
111
|
|
|
foreach (headers_list() as $header) { |
|
112
|
|
|
if (false !== stripos($header, 'x-browserhandler')) { |
|
113
|
|
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
return false; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Generate the javascript with spooled messages |
|
121
|
|
|
* |
|
122
|
|
|
* @return string |
|
123
|
|
|
* @access protected |
|
124
|
|
|
*/ |
|
125
|
|
|
protected static function generateScript()/*# : string */ |
|
126
|
|
|
{ |
|
127
|
|
|
$script = array(); |
|
128
|
|
|
foreach (static::$messages as $msg) { |
|
129
|
|
|
$script[] = 'c.log(' . $msg . ');'; |
|
130
|
|
|
} |
|
131
|
|
|
return "(function (c) {if (c && c.groupCollapsed) {\n" . |
|
132
|
|
|
implode("\n", $script) . "\n}})(console);"; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|