source: tmp/version-2_5-test/data/module/log4php/php5/log4php/layouts/LoggerLayoutHtml.php @ 18609

Revision 18609, 8.4 KB checked in by kajiwara, 14 years ago (diff)

正式版にナイトリービルド版をマージしてみるテスト

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