3 * log4php is a PHP port of the log4j java logging package.
5 * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
6 * <p>Design, strategies and part of the methods documentation are developed by log4j team
7 * (Ceki Gülcü as log4j project founder and
8 * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
10 * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
11 * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
13 * <p>This software is published under the terms of the LGPL License
14 * a copy of which has been included with this distribution in the LICENSE file.</p>
23 if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
25 if (!defined('LOG4PHP_LINE_SEP')) {
26 if (substr(php_uname(), 0, 7) == "Windows") {
30 define('LOG4PHP_LINE_SEP', "\r\n");
35 define('LOG4PHP_LINE_SEP', "\n");
41 require_once(LOG4PHP_DIR . '/LoggerLayout.php');
42 require_once(LOG4PHP_DIR . '/spi/LoggerLoggingEvent.php');
45 * This layout outputs events in a HTML table.
47 * Parameters are: {@link $title}, {@link $locationInfo}.
49 * @author VxR <vxr@vxr.it>
50 * @version $Revision: 2 $
54 class LoggerLayoutHtml extends LoggerLayout {
57 * The <b>LocationInfo</b> option takes a boolean value. By
58 * default, it is set to false which means there will be no location
59 * information output by this layout. If the the option is set to
60 * true, then the file name and line number of the statement
61 * at the origin of the log statement will be output.
63 * <p>If you are embedding this layout within a {@link LoggerAppenderMail}
64 * or a {@link LoggerAppenderMailEvent} then make sure to set the
65 * <b>LocationInfo</b> option of that appender as well.
68 var $locationInfo = false;
71 * The <b>Title</b> option takes a String value. This option sets the
72 * document title of the generated HTML document.
73 * Defaults to 'Log4php Log Messages'.
76 var $title = "Log4php Log Messages";
81 function LoggerLayoutHtml()
87 * The <b>LocationInfo</b> option takes a boolean value. By
88 * default, it is set to false which means there will be no location
89 * information output by this layout. If the the option is set to
90 * true, then the file name and line number of the statement
91 * at the origin of the log statement will be output.
93 * <p>If you are embedding this layout within a {@link LoggerAppenderMail}
94 * or a {@link LoggerAppenderMailEvent} then make sure to set the
95 * <b>LocationInfo</b> option of that appender as well.
97 function setLocationInfo($flag)
100 $this->locationInfo = $flag;
102 $this->locationInfo = (bool)(strtolower($flag) == 'true');
107 * Returns the current value of the <b>LocationInfo</b> option.
109 function getLocationInfo()
111 return $this->locationInfo;
115 * The <b>Title</b> option takes a String value. This option sets the
116 * document title of the generated HTML document.
117 * Defaults to 'Log4php Log Messages'.
119 function setTitle($title)
121 $this->title = $title;
125 * @return string Returns the current value of the <b>Title</b> option.
133 * @return string Returns the content type output by this layout, i.e "text/html".
135 function getContentType()
141 * No options to activate.
143 function activateOptions()
149 * @param LoggerLoggingEvent $event
152 function format($event)
154 $sbuf = LOG4PHP_LINE_SEP . "<tr>" . LOG4PHP_LINE_SEP;
158 $eventTime = (float)$event->getTimeStamp();
159 $eventStartTime = (float)LoggerLoggingEvent::getStartTime();
160 $sbuf .= number_format(($eventTime - $eventStartTime) * 1000, 0, '', '');
161 $sbuf .= "</td>" . LOG4PHP_LINE_SEP;
163 $sbuf .= "<td title=\"" . $event->getThreadName() . " thread\">";
164 $sbuf .= $event->getThreadName();
165 $sbuf .= "</td>" . LOG4PHP_LINE_SEP;
167 $sbuf .= "<td title=\"Level\">";
169 $level = $event->getLevel();
171 if ($level->equals(LoggerLevel::getLevelDebug())) {
172 $sbuf .= "<font color=\"#339933\">";
173 $sbuf .= $level->toString();
175 }elseif($level->equals(LoggerLevel::getLevelWarn())) {
176 $sbuf .= "<font color=\"#993300\"><strong>";
177 $sbuf .= $level->toString();
178 $sbuf .= "</strong></font>";
180 $sbuf .= $level->toString();
182 $sbuf .= "</td>" . LOG4PHP_LINE_SEP;
184 $sbuf .= "<td title=\"" . htmlentities($event->getLoggerName(), ENT_QUOTES) . " category\">";
185 $sbuf .= htmlentities($event->getLoggerName(), ENT_QUOTES);
186 $sbuf .= "</td>" . LOG4PHP_LINE_SEP;
188 if ($this->locationInfo) {
189 $locInfo = $event->getLocationInformation();
191 $sbuf .= htmlentities($locInfo->getFileName(), ENT_QUOTES). ':' . $locInfo->getLineNumber();
192 $sbuf .= "</td>" . LOG4PHP_LINE_SEP;
195 $sbuf .= "<td title=\"Message\">";
196 $sbuf .= htmlentities($event->getRenderedMessage(), ENT_QUOTES);
197 $sbuf .= "</td>" . LOG4PHP_LINE_SEP;
199 $sbuf .= "</tr>" . LOG4PHP_LINE_SEP;
201 if ($event->getNDC() != null) {
202 $sbuf .= "<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : xx-small;\" colspan=\"6\" title=\"Nested Diagnostic Context\">";
203 $sbuf .= "NDC: " . htmlentities($event->getNDC(), ENT_QUOTES);
204 $sbuf .= "</td></tr>" . LOG4PHP_LINE_SEP;
211 * @return string Returns appropriate HTML headers.
215 $sbuf = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">" . LOG4PHP_LINE_SEP;
216 $sbuf .= "<html>" . LOG4PHP_LINE_SEP;
217 $sbuf .= "<head>" . LOG4PHP_LINE_SEP;
218 $sbuf .= "<title>" . $this->title . "</title>" . LOG4PHP_LINE_SEP;
219 $sbuf .= "<style type=\"text/css\">" . LOG4PHP_LINE_SEP;
220 $sbuf .= "<!--" . LOG4PHP_LINE_SEP;
221 $sbuf .= "body, table {font-family: arial,sans-serif; font-size: x-small;}" . LOG4PHP_LINE_SEP;
222 $sbuf .= "th {background: #336699; color: #FFFFFF; text-align: left;}" . LOG4PHP_LINE_SEP;
223 $sbuf .= "-->" . LOG4PHP_LINE_SEP;
224 $sbuf .= "</style>" . LOG4PHP_LINE_SEP;
225 $sbuf .= "</head>" . LOG4PHP_LINE_SEP;
226 $sbuf .= "<body bgcolor=\"#FFFFFF\" topmargin=\"6\" leftmargin=\"6\">" . LOG4PHP_LINE_SEP;
227 $sbuf .= "<hr size=\"1\" noshade>" . LOG4PHP_LINE_SEP;
228 $sbuf .= "Log session start time " . strftime('%c', time()) . "<br>" . LOG4PHP_LINE_SEP;
229 $sbuf .= "<br>" . LOG4PHP_LINE_SEP;
230 $sbuf .= "<table cellspacing=\"0\" cellpadding=\"4\" border=\"1\" bordercolor=\"#224466\" width=\"100%\">" . LOG4PHP_LINE_SEP;
231 $sbuf .= "<tr>" . LOG4PHP_LINE_SEP;
232 $sbuf .= "<th>Time</th>" . LOG4PHP_LINE_SEP;
233 $sbuf .= "<th>Thread</th>" . LOG4PHP_LINE_SEP;
234 $sbuf .= "<th>Level</th>" . LOG4PHP_LINE_SEP;
235 $sbuf .= "<th>Category</th>" . LOG4PHP_LINE_SEP;
236 if ($this->locationInfo)
237 $sbuf .= "<th>File:Line</th>" . LOG4PHP_LINE_SEP;
238 $sbuf .= "<th>Message</th>" . LOG4PHP_LINE_SEP;
239 $sbuf .= "</tr>" . LOG4PHP_LINE_SEP;
245 * @return string Returns the appropriate HTML footers.
249 $sbuf = "</table>" . LOG4PHP_LINE_SEP;
250 $sbuf .= "<br>" . LOG4PHP_LINE_SEP;
251 $sbuf .= "</body></html>";