LoggerLayoutHtml.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * log4php is a PHP port of the log4j java logging package.
  4. *
  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>
  9. *
  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>
  12. *
  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>
  15. *
  16. * @package log4php
  17. * @subpackage layouts
  18. */
  19. /**
  20. * @ignore
  21. */
  22. if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
  23. if (!defined('LOG4PHP_LINE_SEP')) {
  24. if (substr(php_uname(), 0, 7) == "Windows") {
  25. /**
  26. * @ignore
  27. */
  28. define('LOG4PHP_LINE_SEP', "\r\n");
  29. } else {
  30. /**
  31. * @ignore
  32. */
  33. define('LOG4PHP_LINE_SEP', "\n");
  34. }
  35. }
  36. /**
  37. */
  38. require_once(LOG4PHP_DIR . '/LoggerLayout.php');
  39. require_once(LOG4PHP_DIR . '/spi/LoggerLoggingEvent.php');
  40. /**
  41. * This layout outputs events in a HTML table.
  42. *
  43. * Parameters are: {@link $title}, {@link $locationInfo}.
  44. *
  45. * @author VxR <vxr@vxr.it>
  46. * @version $Revision: 1.1 $
  47. * @package log4php
  48. * @subpackage layouts
  49. */
  50. class LoggerLayoutHtml extends LoggerLayout {
  51. /**
  52. * The <b>LocationInfo</b> option takes a boolean value. By
  53. * default, it is set to false which means there will be no location
  54. * information output by this layout. If the the option is set to
  55. * true, then the file name and line number of the statement
  56. * at the origin of the log statement will be output.
  57. *
  58. * <p>If you are embedding this layout within a {@link LoggerAppenderMail}
  59. * or a {@link LoggerAppenderMailEvent} then make sure to set the
  60. * <b>LocationInfo</b> option of that appender as well.
  61. * @var boolean
  62. */
  63. var $locationInfo = false;
  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. var $title = "Log4php Log Messages";
  71. /**
  72. * Constructor
  73. */
  74. function LoggerLayoutHtml()
  75. {
  76. return;
  77. }
  78. /**
  79. * The <b>LocationInfo</b> option takes a boolean value. By
  80. * default, it is set to false which means there will be no location
  81. * information output by this layout. If the the option is set to
  82. * true, then the file name and line number of the statement
  83. * at the origin of the log statement will be output.
  84. *
  85. * <p>If you are embedding this layout within a {@link LoggerAppenderMail}
  86. * or a {@link LoggerAppenderMailEvent} then make sure to set the
  87. * <b>LocationInfo</b> option of that appender as well.
  88. */
  89. function setLocationInfo($flag)
  90. {
  91. if (is_bool($flag)) {
  92. $this->locationInfo = $flag;
  93. } else {
  94. $this->locationInfo = (bool)(strtolower($flag) == 'true');
  95. }
  96. }
  97. /**
  98. * Returns the current value of the <b>LocationInfo</b> option.
  99. */
  100. function getLocationInfo()
  101. {
  102. return $this->locationInfo;
  103. }
  104. /**
  105. * The <b>Title</b> option takes a String value. This option sets the
  106. * document title of the generated HTML document.
  107. * Defaults to 'Log4php Log Messages'.
  108. */
  109. function setTitle($title)
  110. {
  111. $this->title = $title;
  112. }
  113. /**
  114. * @return string Returns the current value of the <b>Title</b> option.
  115. */
  116. function getTitle()
  117. {
  118. return $this->title;
  119. }
  120. /**
  121. * @return string Returns the content type output by this layout, i.e "text/html".
  122. */
  123. function getContentType()
  124. {
  125. return "text/html";
  126. }
  127. /**
  128. * No options to activate.
  129. */
  130. function activateOptions()
  131. {
  132. return true;
  133. }
  134. /**
  135. * @param LoggerLoggingEvent $event
  136. * @return string
  137. */
  138. function format($event)
  139. {
  140. $sbuf = LOG4PHP_LINE_SEP . "<tr>" . LOG4PHP_LINE_SEP;
  141. $sbuf .= "<td>";
  142. $eventTime = (float)$event->getTimeStamp();
  143. $eventStartTime = (float)LoggerLoggingEvent::getStartTime();
  144. $sbuf .= number_format(($eventTime - $eventStartTime) * 1000, 0, '', '');
  145. $sbuf .= "</td>" . LOG4PHP_LINE_SEP;
  146. $sbuf .= "<td title=\"" . $event->getThreadName() . " thread\">";
  147. $sbuf .= $event->getThreadName();
  148. $sbuf .= "</td>" . LOG4PHP_LINE_SEP;
  149. $sbuf .= "<td title=\"Level\">";
  150. $level = $event->getLevel();
  151. if ($level->equals(LoggerLevel::getLevelDebug())) {
  152. $sbuf .= "<font color=\"#339933\">";
  153. $sbuf .= $level->toString();
  154. $sbuf .= "</font>";
  155. }elseif($level->equals(LoggerLevel::getLevelWarn())) {
  156. $sbuf .= "<font color=\"#993300\"><strong>";
  157. $sbuf .= $level->toString();
  158. $sbuf .= "</strong></font>";
  159. } else {
  160. $sbuf .= $level->toString();
  161. }
  162. $sbuf .= "</td>" . LOG4PHP_LINE_SEP;
  163. $sbuf .= "<td title=\"" . htmlentities($event->getLoggerName(), ENT_QUOTES) . " category\">";
  164. $sbuf .= htmlentities($event->getLoggerName(), ENT_QUOTES);
  165. $sbuf .= "</td>" . LOG4PHP_LINE_SEP;
  166. if ($this->locationInfo) {
  167. $locInfo = $event->getLocationInformation();
  168. $sbuf .= "<td>";
  169. $sbuf .= htmlentities($locInfo->getFileName(), ENT_QUOTES). ':' . $locInfo->getLineNumber();
  170. $sbuf .= "</td>" . LOG4PHP_LINE_SEP;
  171. }
  172. $sbuf .= "<td title=\"Message\">";
  173. $sbuf .= htmlentities($event->getRenderedMessage(), ENT_QUOTES);
  174. $sbuf .= "</td>" . LOG4PHP_LINE_SEP;
  175. $sbuf .= "</tr>" . LOG4PHP_LINE_SEP;
  176. if ($event->getNDC() != null) {
  177. $sbuf .= "<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : xx-small;\" colspan=\"6\" title=\"Nested Diagnostic Context\">";
  178. $sbuf .= "NDC: " . htmlentities($event->getNDC(), ENT_QUOTES);
  179. $sbuf .= "</td></tr>" . LOG4PHP_LINE_SEP;
  180. }
  181. return $sbuf;
  182. }
  183. /**
  184. * @return string Returns appropriate HTML headers.
  185. */
  186. function getHeader()
  187. {
  188. $sbuf = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">" . LOG4PHP_LINE_SEP;
  189. $sbuf .= "<html>" . LOG4PHP_LINE_SEP;
  190. $sbuf .= "<head>" . LOG4PHP_LINE_SEP;
  191. $sbuf .= "<title>" . $this->title . "</title>" . LOG4PHP_LINE_SEP;
  192. $sbuf .= "<style type=\"text/css\">" . LOG4PHP_LINE_SEP;
  193. $sbuf .= "<!--" . LOG4PHP_LINE_SEP;
  194. $sbuf .= "body, table {font-family: arial,sans-serif; font-size: x-small;}" . LOG4PHP_LINE_SEP;
  195. $sbuf .= "th {background: #336699; color: #FFFFFF; text-align: left;}" . LOG4PHP_LINE_SEP;
  196. $sbuf .= "-->" . LOG4PHP_LINE_SEP;
  197. $sbuf .= "</style>" . LOG4PHP_LINE_SEP;
  198. $sbuf .= "</head>" . LOG4PHP_LINE_SEP;
  199. $sbuf .= "<body bgcolor=\"#FFFFFF\" topmargin=\"6\" leftmargin=\"6\">" . LOG4PHP_LINE_SEP;
  200. $sbuf .= "<hr size=\"1\" noshade>" . LOG4PHP_LINE_SEP;
  201. $sbuf .= "Log session start time " . strftime('%c', time()) . "<br>" . LOG4PHP_LINE_SEP;
  202. $sbuf .= "<br>" . LOG4PHP_LINE_SEP;
  203. $sbuf .= "<table cellspacing=\"0\" cellpadding=\"4\" border=\"1\" bordercolor=\"#224466\" width=\"100%\">" . LOG4PHP_LINE_SEP;
  204. $sbuf .= "<tr>" . LOG4PHP_LINE_SEP;
  205. $sbuf .= "<th>Time</th>" . LOG4PHP_LINE_SEP;
  206. $sbuf .= "<th>Thread</th>" . LOG4PHP_LINE_SEP;
  207. $sbuf .= "<th>Level</th>" . LOG4PHP_LINE_SEP;
  208. $sbuf .= "<th>Category</th>" . LOG4PHP_LINE_SEP;
  209. if ($this->locationInfo)
  210. $sbuf .= "<th>File:Line</th>" . LOG4PHP_LINE_SEP;
  211. $sbuf .= "<th>Message</th>" . LOG4PHP_LINE_SEP;
  212. $sbuf .= "</tr>" . LOG4PHP_LINE_SEP;
  213. return $sbuf;
  214. }
  215. /**
  216. * @return string Returns the appropriate HTML footers.
  217. */
  218. function getFooter()
  219. {
  220. $sbuf = "</table>" . LOG4PHP_LINE_SEP;
  221. $sbuf .= "<br>" . LOG4PHP_LINE_SEP;
  222. $sbuf .= "</body></html>";
  223. return $sbuf;
  224. }
  225. }
  226. ?>