source: branches/comu-ver2/data/module/log4php/php5/log4php/layouts/LoggerLayoutTTCC.php @ 18220

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

#149 ロガークラス作成

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');
45 
46/**
47 * String constant designating no time information. Current value of
48 * this constant is <b>NULL</b>.
49 */
50define ('LOG4PHP_LOGGER_LAYOUT_NULL_DATE_FORMAT',   'NULL');
51
52/**
53 * String constant designating relative time. Current value of
54 * this constant is <b>RELATIVE</b>.
55 */
56define ('LOG4PHP_LOGGER_LAYOUT_RELATIVE_TIME_DATE_FORMAT', 'RELATIVE');
57
58/**
59 * TTCC layout format consists of time, thread, category and nested
60 * diagnostic context information, hence the name.
61 *
62 * <p>Each of the four fields can be individually enabled or
63 * disabled. The time format depends on the <b>DateFormat</b> used.</p>
64 *
65 * <p>If no dateFormat is specified it defaults to '%c'.
66 * See php {@link PHP_MANUAL#date} function for details.</p>
67 *
68 * Params:
69 * - {@link $threadPrinting} (true|false) enable/disable pid reporting.
70 * - {@link $categoryPrefixing} (true|false) enable/disable logger category reporting.
71 * - {@link $contextPrinting} (true|false) enable/disable NDC reporting.
72 * - {@link $microSecondsPrinting} (true|false) enable/disable micro seconds reporting in timestamp.
73 * - {@link $dateFormat} (string) set date format. See php {@link PHP_MANUAL#date} function for details.
74 *
75 * @author  Marco Vassura
76 * @version $Revision: 635069 $
77 * @package log4php
78 * @subpackage layouts
79 */
80class LoggerLayoutTTCC extends LoggerLayout {
81
82    // Internal representation of options
83    protected $threadPrinting    = true;
84    protected $categoryPrefixing = true;
85    protected $contextPrinting   = true;
86    protected $microSecondsPrinting = true;
87   
88    /**
89     * @var string date format. See {@link PHP_MANUAL#strftime} for details
90     */
91    protected $dateFormat = '%c';
92
93    /**
94     * Constructor
95     *
96     * @param string date format
97     * @see dateFormat
98     */
99    public function __construct($dateFormat = '')
100    {
101        if (!empty($dateFormat))
102            $this->dateFormat = $dateFormat;
103        return;
104    }
105
106    public function activateOptions() {
107        return;
108    }
109
110    /**
111     * The <b>ThreadPrinting</b> option specifies whether the name of the
112     * current thread is part of log output or not. This is true by default.
113     */
114    public function setThreadPrinting($threadPrinting)
115    {
116       
117        $this->threadPrinting = is_bool($threadPrinting) ?
118            $threadPrinting :
119            (bool)(strtolower($threadPrinting) == 'true');
120    }
121
122    /**
123     * @return boolean Returns value of the <b>ThreadPrinting</b> option.
124     */
125    public function getThreadPrinting() {
126        return $this->threadPrinting;
127    }
128
129    /**
130     * The <b>CategoryPrefixing</b> option specifies whether {@link Category}
131     * name is part of log output or not. This is true by default.
132     */
133    public function setCategoryPrefixing($categoryPrefixing)
134    {
135        $this->categoryPrefixing = is_bool($categoryPrefixing) ?
136            $categoryPrefixing :
137            (bool)(strtolower($categoryPrefixing) == 'true');
138    }
139
140    /**
141     * @return boolean Returns value of the <b>CategoryPrefixing</b> option.
142     */
143    public function getCategoryPrefixing() {
144        return $this->categoryPrefixing;
145    }
146
147    /**
148     * The <b>ContextPrinting</b> option specifies log output will include
149     * the nested context information belonging to the current thread.
150     * This is true by default.
151     */
152    public function setContextPrinting($contextPrinting) {
153        $this->contextPrinting = is_bool($contextPrinting) ?
154            $contextPrinting :
155            (bool)(strtolower($contextPrinting) == 'true');
156    }
157
158    /**
159     * @return boolean Returns value of the <b>ContextPrinting</b> option.
160     */
161    public function getContextPrinting()
162    {
163        return $this->contextPrinting;
164    }
165   
166    /**
167     * The <b>MicroSecondsPrinting</b> option specifies if microseconds infos
168     * should be printed at the end of timestamp.
169     * This is true by default.
170     */
171    public function setMicroSecondsPrinting($microSecondsPrinting) {
172        $this->microSecondsPrinting = is_bool($microSecondsPrinting) ?
173            $microSecondsPrinting :
174            (bool)(strtolower($microSecondsPrinting) == 'true');
175    }
176
177    /**
178     * @return boolean Returns value of the <b>MicroSecondsPrinting</b> option.
179     */
180    public function getMicroSecondsPrinting()
181    {
182        return $this->microSecondsPrinting;
183    }
184   
185   
186    public function setDateFormat($dateFormat)
187    {
188        $this->dateFormat = $dateFormat;
189    }
190   
191    /**
192     * @return string
193     */
194    public function getDateFormat()
195    {
196        return $this->dateFormat;
197    }
198
199    /**
200     * In addition to the level of the statement and message, the
201     * returned string includes time, thread, category.
202     * <p>Time, thread, category are printed depending on options.
203     *
204     * @param LoggerLoggingEvent $event
205     * @return string
206     */
207    public function format($event)
208    {
209        $timeStamp = (float)$event->getTimeStamp();
210        $format = strftime($this->dateFormat, (int)$timeStamp);
211       
212        if ($this->microSecondsPrinting) {
213            $usecs = floor(($timeStamp - (int)$timeStamp) * 1000);
214            $format .= sprintf(',%03d', $usecs);
215        }
216           
217        $format .= ' ';
218       
219        if ($this->threadPrinting)
220            $format .= '['.getmypid().'] ';
221       
222        $level = $event->getLevel();
223        $format .= $level->toString().' ';
224       
225        if($this->categoryPrefixing) {
226            $format .= $event->getLoggerName().' ';
227        }
228       
229        if($this->contextPrinting) {
230            $ndc = $event->getNDC();
231            if($ndc != null) {
232                $format .= $ndc.' ';
233            }
234        }
235       
236        $format .= '- '.$event->getRenderedMessage();
237        $format .= LOG4PHP_LINE_SEP;
238       
239        return $format;
240    }
241
242    public function ignoresThrowable()
243    {
244        return true;
245    }
246}
Note: See TracBrowser for help on using the repository browser.