library/log4php/layouts/LoggerLayoutHtml.php
changeset 46 f11c31f7fa3e
parent 45 a56e7f9a0463
child 47 03388ec805b4
equal deleted inserted replaced
45:a56e7f9a0463 46:f11c31f7fa3e
     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: 1379731 $
       
    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         $this->setBoolean('locationInfo', $flag);
       
    85     }
       
    86 
       
    87     /**
       
    88      * Returns the current value of the <b>LocationInfo</b> option.
       
    89      */
       
    90     public function getLocationInfo() {
       
    91         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         $this->setString('title', $title);
       
   101     }
       
   102 
       
   103     /**
       
   104      * @return string Returns the current value of the <b>Title</b> option.
       
   105      */
       
   106     public function getTitle() {
       
   107         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         return "text/html";
       
   115     }
       
   116 
       
   117     /**
       
   118      * @param LoggerLoggingEvent $event
       
   119      * @return string
       
   120      */
       
   121     public function format(LoggerLoggingEvent $event) {
       
   122         $sbuf = PHP_EOL . "<tr>" . PHP_EOL;
       
   123 
       
   124         $sbuf .= "<td>";
       
   125         $sbuf .= round(1000 * $event->getRelativeTime());
       
   126         $sbuf .= "</td>" . PHP_EOL;
       
   127 
       
   128         $sbuf .= "<td title=\"" . $event->getThreadName() . " thread\">";
       
   129         $sbuf .= $event->getThreadName();
       
   130         $sbuf .= "</td>" . PHP_EOL;
       
   131 
       
   132         $sbuf .= "<td title=\"Level\">";
       
   133 
       
   134         $level = $event->getLevel();
       
   135 
       
   136         if ($level->equals(LoggerLevel::getLevelDebug())) {
       
   137             $sbuf .= "<font color=\"#339933\">$level</font>";
       
   138         } else if ($level->equals(LoggerLevel::getLevelWarn())) {
       
   139             $sbuf .= "<font color=\"#993300\"><strong>$level</strong></font>";
       
   140         } else {
       
   141             $sbuf .= $level;
       
   142         }
       
   143         $sbuf .= "</td>" . PHP_EOL;
       
   144 
       
   145         $sbuf .= "<td title=\"" . htmlentities($event->getLoggerName(), ENT_QUOTES) . " category\">";
       
   146         $sbuf .= htmlentities($event->getLoggerName(), ENT_QUOTES);
       
   147         $sbuf .= "</td>" . PHP_EOL;
       
   148 
       
   149         if ($this->locationInfo) {
       
   150             $locInfo = $event->getLocationInformation();
       
   151             $sbuf .= "<td>";
       
   152             $sbuf .= htmlentities($locInfo->getFileName(), ENT_QUOTES) . ':' . $locInfo->getLineNumber();
       
   153             $sbuf .= "</td>" . PHP_EOL;
       
   154         }
       
   155 
       
   156         $sbuf .= "<td title=\"Message\">";
       
   157         $sbuf .= htmlentities($event->getRenderedMessage(), ENT_QUOTES);
       
   158         $sbuf .= "</td>" . PHP_EOL;
       
   159 
       
   160         $sbuf .= "</tr>" . PHP_EOL;
       
   161 
       
   162         if ($event->getNDC() != null) {
       
   163             $sbuf .= "<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : xx-small;\" colspan=\"6\" title=\"Nested Diagnostic Context\">";
       
   164             $sbuf .= "NDC: " . htmlentities($event->getNDC(), ENT_QUOTES);
       
   165             $sbuf .= "</td></tr>" . PHP_EOL;
       
   166         }
       
   167         return $sbuf;
       
   168     }
       
   169 
       
   170     /**
       
   171      * @return string Returns appropriate HTML headers.
       
   172      */
       
   173     public function getHeader() {
       
   174         $sbuf = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">" . PHP_EOL;
       
   175         $sbuf .= "<html>" . PHP_EOL;
       
   176         $sbuf .= "<head>" . PHP_EOL;
       
   177         $sbuf .= "<title>" . $this->title . "</title>" . PHP_EOL;
       
   178         $sbuf .= "<style type=\"text/css\">" . PHP_EOL;
       
   179         $sbuf .= "<!--" . PHP_EOL;
       
   180         $sbuf .= "body, table {font-family: arial,sans-serif; font-size: x-small;}" . PHP_EOL;
       
   181         $sbuf .= "th {background: #336699; color: #FFFFFF; text-align: left;}" . PHP_EOL;
       
   182         $sbuf .= "-->" . PHP_EOL;
       
   183         $sbuf .= "</style>" . PHP_EOL;
       
   184         $sbuf .= "</head>" . PHP_EOL;
       
   185         $sbuf .= "<body bgcolor=\"#FFFFFF\" topmargin=\"6\" leftmargin=\"6\">" . PHP_EOL;
       
   186         $sbuf .= "<hr size=\"1\" noshade>" . PHP_EOL;
       
   187         $sbuf .= "Log session start time " . strftime('%c', time()) . "<br>" . PHP_EOL;
       
   188         $sbuf .= "<br>" . PHP_EOL;
       
   189         $sbuf .= "<table cellspacing=\"0\" cellpadding=\"4\" border=\"1\" bordercolor=\"#224466\" width=\"100%\">" . PHP_EOL;
       
   190         $sbuf .= "<tr>" . PHP_EOL;
       
   191         $sbuf .= "<th>Time</th>" . PHP_EOL;
       
   192         $sbuf .= "<th>Thread</th>" . PHP_EOL;
       
   193         $sbuf .= "<th>Level</th>" . PHP_EOL;
       
   194         $sbuf .= "<th>Category</th>" . PHP_EOL;
       
   195         if ($this->locationInfo) {
       
   196             $sbuf .= "<th>File:Line</th>" . PHP_EOL;
       
   197         }
       
   198         $sbuf .= "<th>Message</th>" . PHP_EOL;
       
   199         $sbuf .= "</tr>" . PHP_EOL;
       
   200 
       
   201         return $sbuf;
       
   202     }
       
   203 
       
   204     /**
       
   205      * @return string Returns the appropriate HTML footers.
       
   206      */
       
   207     public function getFooter() {
       
   208         $sbuf = "</table>" . PHP_EOL;
       
   209         $sbuf .= "<br>" . PHP_EOL;
       
   210         $sbuf .= "</body></html>";
       
   211 
       
   212         return $sbuf;
       
   213     }
       
   214 }