LoggerLayoutHtml.php
7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @package log4php
*/
/**
* This layout outputs events in a HTML table.
*
* Configurable parameters for this layout are:
*
* - title
* - locationInfo
*
* An example for this layout:
*
* {@example ../../examples/php/layout_html.php 19}<br>
*
* The corresponding XML file:
*
* {@example ../../examples/resources/layout_html.properties 18}
*
* The above will print a HTML table that looks, converted back to plain text, like the following:<br>
* <pre>
* Log session start time Wed Sep 9 00:11:30 2009
*
* Time Thread Level Category Message
* 0 8318 INFO root Hello World!
* </pre>
*
* @version $Revision: 883108 $
* @package log4php
* @subpackage layouts
*/
class LoggerLayoutHtml extends LoggerLayout {
/**
* The <b>LocationInfo</b> option takes a boolean value. By
* default, it is set to false which means there will be no location
* information output by this layout. If the the option is set to
* true, then the file name and line number of the statement
* at the origin of the log statement will be output.
*
* <p>If you are embedding this layout within a {@link LoggerAppenderMail}
* or a {@link LoggerAppenderMailEvent} then make sure to set the
* <b>LocationInfo</b> option of that appender as well.
* @var boolean
*/
private $locationInfo = false;
/**
* The <b>Title</b> option takes a String value. This option sets the
* document title of the generated HTML document.
* Defaults to 'Log4php Log Messages'.
* @var string
*/
private $title = "Log4php Log Messages";
/**
* Constructor
*/
public function __construct() {
}
/**
* The <b>LocationInfo</b> option takes a boolean value. By
* default, it is set to false which means there will be no location
* information output by this layout. If the the option is set to
* true, then the file name and line number of the statement
* at the origin of the log statement will be output.
*
* <p>If you are embedding this layout within a {@link LoggerAppenderMail}
* or a {@link LoggerAppenderMailEvent} then make sure to set the
* <b>LocationInfo</b> option of that appender as well.
*/
public function setLocationInfo($flag) {
if (is_bool($flag)) {
$this->locationInfo = $flag;
} else {
$this->locationInfo = (bool)(strtolower($flag) == 'true');
}
}
/**
* Returns the current value of the <b>LocationInfo</b> option.
*/
public function getLocationInfo() {
return $this->locationInfo;
}
/**
* The <b>Title</b> option takes a String value. This option sets the
* document title of the generated HTML document.
* Defaults to 'Log4php Log Messages'.
*/
public function setTitle($title) {
$this->title = $title;
}
/**
* @return string Returns the current value of the <b>Title</b> option.
*/
public function getTitle() {
return $this->title;
}
/**
* @return string Returns the content type output by this layout, i.e "text/html".
*/
public function getContentType() {
return "text/html";
}
/**
* @param LoggerLoggingEvent $event
* @return string
*/
public function format(LoggerLoggingEvent $event) {
$sbuf = PHP_EOL . "<tr>" . PHP_EOL;
$sbuf .= "<td>";
$sbuf .= $event->getTime();
$sbuf .= "</td>" . PHP_EOL;
$sbuf .= "<td title=\"" . $event->getThreadName() . " thread\">";
$sbuf .= $event->getThreadName();
$sbuf .= "</td>" . PHP_EOL;
$sbuf .= "<td title=\"Level\">";
$level = $event->getLevel();
if ($level->equals(LoggerLevel::getLevelDebug())) {
$sbuf .= "<font color=\"#339933\">";
$sbuf .= $level->toString();
$sbuf .= "</font>";
} else if ($level->equals(LoggerLevel::getLevelWarn())) {
$sbuf .= "<font color=\"#993300\"><strong>";
$sbuf .= $level->toString();
$sbuf .= "</strong></font>";
} else {
$sbuf .= $level->toString();
}
$sbuf .= "</td>" . PHP_EOL;
$sbuf .= "<td title=\"" . htmlentities($event->getLoggerName(), ENT_QUOTES) . " category\">";
$sbuf .= htmlentities($event->getLoggerName(), ENT_QUOTES);
$sbuf .= "</td>" . PHP_EOL;
if ($this->locationInfo) {
$locInfo = $event->getLocationInformation();
$sbuf .= "<td>";
$sbuf .= htmlentities($locInfo->getFileName(), ENT_QUOTES). ':' . $locInfo->getLineNumber();
$sbuf .= "</td>" . PHP_EOL;
}
$sbuf .= "<td title=\"Message\">";
$sbuf .= htmlentities($event->getRenderedMessage(), ENT_QUOTES);
$sbuf .= "</td>" . PHP_EOL;
$sbuf .= "</tr>" . PHP_EOL;
if ($event->getNDC() != null) {
$sbuf .= "<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : xx-small;\" colspan=\"6\" title=\"Nested Diagnostic Context\">";
$sbuf .= "NDC: " . htmlentities($event->getNDC(), ENT_QUOTES);
$sbuf .= "</td></tr>" . PHP_EOL;
}
return $sbuf;
}
/**
* @return string Returns appropriate HTML headers.
*/
public function getHeader() {
$sbuf = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">" . PHP_EOL;
$sbuf .= "<html>" . PHP_EOL;
$sbuf .= "<head>" . PHP_EOL;
$sbuf .= "<title>" . $this->title . "</title>" . PHP_EOL;
$sbuf .= "<style type=\"text/css\">" . PHP_EOL;
$sbuf .= "<!--" . PHP_EOL;
$sbuf .= "body, table {font-family: arial,sans-serif; font-size: x-small;}" . PHP_EOL;
$sbuf .= "th {background: #336699; color: #FFFFFF; text-align: left;}" . PHP_EOL;
$sbuf .= "-->" . PHP_EOL;
$sbuf .= "</style>" . PHP_EOL;
$sbuf .= "</head>" . PHP_EOL;
$sbuf .= "<body bgcolor=\"#FFFFFF\" topmargin=\"6\" leftmargin=\"6\">" . PHP_EOL;
$sbuf .= "<hr size=\"1\" noshade>" . PHP_EOL;
$sbuf .= "Log session start time " . strftime('%c', time()) . "<br>" . PHP_EOL;
$sbuf .= "<br>" . PHP_EOL;
$sbuf .= "<table cellspacing=\"0\" cellpadding=\"4\" border=\"1\" bordercolor=\"#224466\" width=\"100%\">" . PHP_EOL;
$sbuf .= "<tr>" . PHP_EOL;
$sbuf .= "<th>Time</th>" . PHP_EOL;
$sbuf .= "<th>Thread</th>" . PHP_EOL;
$sbuf .= "<th>Level</th>" . PHP_EOL;
$sbuf .= "<th>Category</th>" . PHP_EOL;
if ($this->locationInfo)
$sbuf .= "<th>File:Line</th>" . PHP_EOL;
$sbuf .= "<th>Message</th>" . PHP_EOL;
$sbuf .= "</tr>" . PHP_EOL;
return $sbuf;
}
/**
* @return string Returns the appropriate HTML footers.
*/
public function getFooter() {
$sbuf = "</table>" . PHP_EOL;
$sbuf .= "<br>" . PHP_EOL;
$sbuf .= "</body></html>";
return $sbuf;
}
}