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