source: branches/comu-ver2/data/module/log4php/php4/log4php/layouts/LoggerLayoutHtml.php @ 18220

Revision 18220, 8.5 KB checked in by yokkuns, 11 years ago (diff)

#149 ロガークラス作成

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