| <?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. |
| */ |
| |
| namespace Apache\Log4php\Layouts; |
| |
| use Apache\Log4php\LoggingEvent; |
| use Apache\Log4php\Level; |
| |
| /** |
| * This layout outputs events in a HTML table. |
| * |
| * Configurable parameters for this layout are: |
| * |
| * - title |
| * - locationInfo |
| * |
| * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 |
| */ |
| class HtmlLayout extends AbstractLayout |
| { |
| /** |
| * 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 MailAppender} |
| * or a {@link MailEventAppender} 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 MailAppender} |
| * or a {@link MailEventAppender} 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 LoggingEvent $event |
| * @return string |
| */ |
| public function format(LoggingEvent $event) |
| { |
| $sbuf = PHP_EOL . "<tr>" . PHP_EOL; |
| |
| $sbuf .= "<td>"; |
| $sbuf .= round(1000 * $event->getRelativeTime()); |
| $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(Level::getLevelDebug())) { |
| $sbuf .= "<font color=\"#339933\">$level</font>"; |
| } elseif ($level->equals(Level::getLevelWarn())) { |
| $sbuf .= "<font color=\"#993300\"><strong>$level</strong></font>"; |
| } else { |
| $sbuf .= $level; |
| } |
| $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;\" "; |
| $sbuf .= "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\" "; |
| $sbuf .= "\"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\" "; |
| $sbuf .= "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; |
| } |
| } |