1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TLogRouter, TLogRoute, TFileLogRoute, TEmailLogRoute class file |
4
|
|
|
* |
5
|
|
|
* @author Qiang Xue <[email protected]> |
6
|
|
|
* @link https://github.com/pradosoft/prado |
7
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Prado\Util; |
11
|
|
|
|
12
|
|
|
use Prado\TPropertyValue; |
13
|
|
|
use Prado\Web\THttpUtility; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* TBrowserLogRoute class. |
17
|
|
|
* |
18
|
|
|
* TBrowserLogRoute prints selected log messages in the response. |
19
|
|
|
* |
20
|
|
|
* @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com> |
21
|
|
|
* @since 3.0 |
22
|
|
|
*/ |
23
|
|
|
class TBrowserLogRoute extends TLogRoute implements IOutputLogRoute |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string css class for identifying the table structure in the dom tree |
27
|
|
|
*/ |
28
|
|
|
private $_cssClass; |
29
|
|
|
/** |
30
|
|
|
* @var bool colorize the deltas. |
31
|
|
|
* @since 4.2.3 |
32
|
|
|
*/ |
33
|
|
|
private bool $_colorizeDelta = true; |
34
|
|
|
/** |
35
|
|
|
* @var bool add the prefix to the message |
36
|
|
|
* @since 4.2.3 |
37
|
|
|
*/ |
38
|
|
|
private bool $_addPrefix = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Renders the logs in HTML to the browser. |
42
|
|
|
* @param array $logs list of log messages |
43
|
|
|
* @param bool $final is the final flush |
44
|
|
|
* @param array $meta the meta data for the logs. |
45
|
|
|
*/ |
46
|
|
|
protected function processLogs(array $logs, bool $final, array $meta) |
47
|
|
|
{ |
48
|
|
|
$app = $this->getApplication(); |
49
|
|
|
if (empty($logs) || $app->getMode() === 'Performance' || $app instanceof \Prado\Shell\TShellApplication) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
$response = $this->getApplication()->getResponse(); |
53
|
|
|
$response->write($this->renderHeader()); |
54
|
|
|
$even = false; |
55
|
|
|
for ($i = 0, $n = count($logs); $i < $n; ++$i) { |
56
|
|
|
$logs[$i]['even'] = ($even = !$even); |
|
|
|
|
57
|
|
|
$response->write($this->renderMessage($logs[$i], $meta)); |
58
|
|
|
} |
59
|
|
|
$response->write($this->renderFooter()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string the css class of the control |
64
|
|
|
*/ |
65
|
|
|
public function getCssClass() |
66
|
|
|
{ |
67
|
|
|
return $this->_cssClass; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $value the css class of the control |
72
|
|
|
*/ |
73
|
|
|
public function setCssClass($value): static |
74
|
|
|
{ |
75
|
|
|
$this->_cssClass = TPropertyValue::ensureString($value); |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return bool Colorize the Deltas |
81
|
|
|
* @since 4.2.3 |
82
|
|
|
*/ |
83
|
|
|
public function getColorizeDelta(): bool |
84
|
|
|
{ |
85
|
|
|
return $this->_colorizeDelta; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param bool|string $value Colorize the Deltas |
90
|
|
|
* @since 4.2.3 |
91
|
|
|
*/ |
92
|
|
|
public function setColorizeDelta($value): static |
93
|
|
|
{ |
94
|
|
|
$this->_colorizeDelta = TPropertyValue::ensureBoolean($value); |
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return bool Adds the prefix to the message |
100
|
|
|
* @since 4.2.3 |
101
|
|
|
*/ |
102
|
|
|
public function getAddPrefix(): bool |
103
|
|
|
{ |
104
|
|
|
return $this->_addPrefix; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param bool|string $value Adds the prefix to the message |
109
|
|
|
* @since 4.2.3 |
110
|
|
|
*/ |
111
|
|
|
public function setAddPrefix($value): static |
112
|
|
|
{ |
113
|
|
|
$this->_addPrefix = TPropertyValue::ensureBoolean($value); |
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
protected function renderHeader() |
121
|
|
|
{ |
122
|
|
|
$string = ''; |
123
|
|
|
if ($className = $this->getCssClass()) { |
124
|
|
|
$string = |
125
|
|
|
<<<EOD |
126
|
|
|
<table class="$className"> |
127
|
|
|
<tr class="header"> |
128
|
|
|
<th colspan="5"> |
129
|
|
|
Application Log |
130
|
|
|
</th> |
131
|
|
|
</tr><tr class="description"> |
132
|
|
|
<th> </th> |
133
|
|
|
<th>Category</th><th>Message</th><th>Time Spent (s)</th><th>Cumulated Time Spent (s)</th> |
134
|
|
|
</tr> |
135
|
|
|
EOD; |
136
|
|
|
} else { |
137
|
|
|
$string = |
138
|
|
|
<<<EOD |
139
|
|
|
<table cellspacing="0" cellpadding="2" border="0" width="100%" style="table-layout:auto"> |
140
|
|
|
<tr> |
141
|
|
|
<th style="background-color: black; color:white;" colspan="5"> |
142
|
|
|
Application Log |
143
|
|
|
</th> |
144
|
|
|
</tr><tr style="background-color: #ccc; color:black"> |
145
|
|
|
<th style="width: 15px"> </th> |
146
|
|
|
<th style="width: auto">Category</th><th style="width: auto">Message</th><th style="width: 120px">Time Spent (s)</th><th style="width: 150px">Cumulated Time Spent (s)</th> |
147
|
|
|
</tr> |
148
|
|
|
EOD; |
149
|
|
|
} |
150
|
|
|
return $string; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param array $log |
155
|
|
|
* @param array $meta |
156
|
|
|
* @return string |
157
|
|
|
* @author Brad Anderson <[email protected]> Colorization/weight of time deltas. |
158
|
|
|
*/ |
159
|
|
|
protected function renderMessage($log, $meta) |
160
|
|
|
{ |
161
|
|
|
$string = ''; |
162
|
|
|
$total = sprintf('%0.6f', $log['total']); |
163
|
|
|
$delta = sprintf('%0.6f', $log['delta']); |
164
|
|
|
if ($this->_addPrefix) { |
165
|
|
|
$msg = $this->formatLogMessage($log); |
166
|
|
|
} else { |
167
|
|
|
$msg = $log[0]; |
168
|
|
|
} |
169
|
|
|
$msg = preg_replace('/\(line[^\)]+\)$/', '', $msg); //remove line number info |
170
|
|
|
|
171
|
|
|
$msg = THttpUtility::htmlEncode($msg); |
172
|
|
|
if ($this->getCssClass()) { |
173
|
|
|
$colorCssClass = $log[TLogger::LOG_LEVEL]; |
174
|
|
|
$messageCssClass = $log['even'] ? 'even' : 'odd'; |
175
|
|
|
$category = $log[TLogger::LOG_CATEGORY]; |
176
|
|
|
$string = <<<EOD |
177
|
|
|
<tr class="message level$colorCssClass $messageCssClass"> |
178
|
|
|
<td class="code"> </td> |
179
|
|
|
<td class="category">{$category}</td> |
180
|
|
|
<td class="message">{$msg}</td> |
181
|
|
|
<td class="time">{$delta}</td> |
182
|
|
|
<td class="cumulatedtime">{$total}</td> |
183
|
|
|
</tr> |
184
|
|
|
EOD; |
185
|
|
|
} else { |
186
|
|
|
$bgcolor = $log['even'] ? "#fff" : "#e8e8e8"; |
187
|
|
|
if ($this->getColorizeDelta()) { |
188
|
|
|
$normalizedTime = $delta / ($meta['maxdelta'] === 0.0 ? 1 : $meta['maxdelta']); |
189
|
|
|
$textColor = 'color:' . TPropertyValue::ensureHexColor(static::getLogColor($normalizedTime)); |
190
|
|
|
$weightCutOff = 0.75; |
191
|
|
|
$weightLightCutOff = 0.4; |
192
|
|
|
if ($normalizedTime > $weightCutOff) { |
193
|
|
|
$weight = '; font-weight: ' . round(400 + 500 * ($normalizedTime - $weightCutOff) / (1 - $weightCutOff)); |
194
|
|
|
} elseif($normalizedTime < $weightLightCutOff) { |
195
|
|
|
$weight = '; font-weight: ' . round(400 - 300 * ($weightLightCutOff - $normalizedTime) / ($weightLightCutOff)); |
196
|
|
|
} else { |
197
|
|
|
$weight = ''; |
198
|
|
|
} |
199
|
|
|
} else { |
200
|
|
|
$textColor = ''; |
201
|
|
|
$weight = ''; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$color = $this->getColorLevel($log[TLogger::LOG_LEVEL]); |
205
|
|
|
$category = $log[TLogger::LOG_CATEGORY]; |
206
|
|
|
$string = <<<EOD |
207
|
|
|
<tr style="background-color: {$bgcolor}; color:#000"> |
208
|
|
|
<td style="border:1px solid silver;background-color: {$color}"> </td> |
209
|
|
|
<td>{$category}</td> |
210
|
|
|
<td>{$msg}</td> |
211
|
|
|
<td style="text-align:center; {$textColor}{$weight}">{$delta}</td> |
212
|
|
|
<td style="text-align:center">{$total}</td> |
213
|
|
|
</tr> |
214
|
|
|
EOD; |
215
|
|
|
} |
216
|
|
|
return $string; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
protected function getColorLevel($level) |
220
|
|
|
{ |
221
|
|
|
switch ($level) { |
222
|
|
|
case TLogger::PROFILE: |
223
|
|
|
case TLogger::PROFILE_BEGIN_SELECT: |
224
|
|
|
case TLogger::PROFILE_BEGIN: |
225
|
|
|
case TLogger::PROFILE_END_SELECT: |
226
|
|
|
case TLogger::PROFILE_END: return 'lime'; |
227
|
|
|
case TLogger::DEBUG: return 'green'; |
228
|
|
|
case TLogger::INFO: return 'black'; |
229
|
|
|
case TLogger::NOTICE: return '#3333FF'; |
230
|
|
|
case TLogger::WARNING: return '#33FFFF'; |
231
|
|
|
case TLogger::ERROR: return '#ff9933'; |
232
|
|
|
case TLogger::ALERT: return '#ff00ff'; |
233
|
|
|
case TLogger::FATAL: return 'red'; |
234
|
|
|
} |
235
|
|
|
return ''; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
protected function renderFooter() |
239
|
|
|
{ |
240
|
|
|
$string = ''; |
241
|
|
|
if ($this->getCssClass()) { |
242
|
|
|
$string .= '<tr class="footer"><td colspan="5">'; |
243
|
|
|
foreach (self::$_levelValues as $name => $level) { |
244
|
|
|
$string .= '<span class="level' . $level . '">' . strtoupper($name) . "</span>"; |
245
|
|
|
} |
246
|
|
|
} else { |
247
|
|
|
$string .= "<tr><td colspan=\"5\" style=\"text-align:center; background-color:black; border-top: 1px solid #ccc; padding:0.2em;\">"; |
248
|
|
|
foreach (self::$_levelValues as $name => $level) { |
249
|
|
|
$string .= "<span style=\"color:white; border:1px solid white; background-color:" . $this->getColorLevel($level); |
250
|
|
|
$string .= ";margin: 0.5em; padding:0.01em;\">" . strtoupper($name) . "</span>"; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
$string .= '</td></tr></table>'; |
254
|
|
|
return $string; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|