diff --git a/library/log4php/layouts/LoggerLayoutHtml.php b/library/log4php/layouts/LoggerLayoutHtml.php new file mode 100644 --- /dev/null +++ b/library/log4php/layouts/LoggerLayoutHtml.php @@ -0,0 +1,214 @@ + + * + * 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:
+ *
+ *    Log session start time Wed Sep 9 00:11:30 2009
+ *
+ *    Time Thread Level Category   Message
+ *    0    8318   INFO  root       Hello World!
+ * 
+ * + * @version $Revision: 1379731 $ + * @package log4php + * @subpackage layouts + */ +class LoggerLayoutHtml extends LoggerLayout { + /** + * The LocationInfo 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. + * + *

If you are embedding this layout within a {@link LoggerAppenderMail} + * or a {@link LoggerAppenderMailEvent} then make sure to set the + * LocationInfo option of that appender as well. + * @var boolean + */ + protected $locationInfo = false; + + /** + * The Title option takes a String value. This option sets the + * document title of the generated HTML document. + * Defaults to 'Log4php Log Messages'. + * @var string + */ + protected $title = "Log4php Log Messages"; + + /** + * The LocationInfo 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. + * + *

If you are embedding this layout within a {@link LoggerAppenderMail} + * or a {@link LoggerAppenderMailEvent} then make sure to set the + * LocationInfo option of that appender as well. + */ + public function setLocationInfo($flag) { + $this->setBoolean('locationInfo', $flag); + } + + /** + * Returns the current value of the LocationInfo option. + */ + public function getLocationInfo() { + return $this->locationInfo; + } + + /** + * The Title 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->setString('title', $title); + } + + /** + * @return string Returns the current value of the Title 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 . "" . PHP_EOL; + + $sbuf .= ""; + $sbuf .= round(1000 * $event->getRelativeTime()); + $sbuf .= "" . PHP_EOL; + + $sbuf .= "getThreadName() . " thread\">"; + $sbuf .= $event->getThreadName(); + $sbuf .= "" . PHP_EOL; + + $sbuf .= ""; + + $level = $event->getLevel(); + + if ($level->equals(LoggerLevel::getLevelDebug())) { + $sbuf .= "$level"; + } else if ($level->equals(LoggerLevel::getLevelWarn())) { + $sbuf .= "$level"; + } else { + $sbuf .= $level; + } + $sbuf .= "" . PHP_EOL; + + $sbuf .= "getLoggerName(), ENT_QUOTES) . " category\">"; + $sbuf .= htmlentities($event->getLoggerName(), ENT_QUOTES); + $sbuf .= "" . PHP_EOL; + + if ($this->locationInfo) { + $locInfo = $event->getLocationInformation(); + $sbuf .= ""; + $sbuf .= htmlentities($locInfo->getFileName(), ENT_QUOTES) . ':' . $locInfo->getLineNumber(); + $sbuf .= "" . PHP_EOL; + } + + $sbuf .= ""; + $sbuf .= htmlentities($event->getRenderedMessage(), ENT_QUOTES); + $sbuf .= "" . PHP_EOL; + + $sbuf .= "" . PHP_EOL; + + if ($event->getNDC() != null) { + $sbuf .= ""; + $sbuf .= "NDC: " . htmlentities($event->getNDC(), ENT_QUOTES); + $sbuf .= "" . PHP_EOL; + } + return $sbuf; + } + + /** + * @return string Returns appropriate HTML headers. + */ + public function getHeader() { + $sbuf = "" . PHP_EOL; + $sbuf .= "" . PHP_EOL; + $sbuf .= "" . PHP_EOL; + $sbuf .= "" . $this->title . "" . PHP_EOL; + $sbuf .= "" . PHP_EOL; + $sbuf .= "" . PHP_EOL; + $sbuf .= "" . PHP_EOL; + $sbuf .= "


" . PHP_EOL; + $sbuf .= "Log session start time " . strftime('%c', time()) . "
" . PHP_EOL; + $sbuf .= "
" . PHP_EOL; + $sbuf .= "" . PHP_EOL; + $sbuf .= "" . PHP_EOL; + $sbuf .= "" . PHP_EOL; + $sbuf .= "" . PHP_EOL; + $sbuf .= "" . PHP_EOL; + $sbuf .= "" . PHP_EOL; + if ($this->locationInfo) { + $sbuf .= "" . PHP_EOL; + } + $sbuf .= "" . PHP_EOL; + $sbuf .= "" . PHP_EOL; + + return $sbuf; + } + + /** + * @return string Returns the appropriate HTML footers. + */ + public function getFooter() { + $sbuf = "
TimeThreadLevelCategoryFile:LineMessage
" . PHP_EOL; + $sbuf .= "
" . PHP_EOL; + $sbuf .= ""; + + return $sbuf; + } +}