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