LoggerLayoutTTCC.php
6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @package log4php
*/
/**
* TTCC layout format consists of <b>t</b>ime, <b>t</b>hread, <b>c</b>ategory and nested
* diagnostic <b>c</b>ontext information, hence the name.
*
* <p>Each of the four fields can be individually enabled or
* disabled. The time format depends on the <b>DateFormat</b> used.</p>
*
* <p>If no dateFormat is specified it defaults to '%c'.
* See php {@link PHP_MANUAL#date} function for details.</p>
*
* Configurable parameters for this layout are:
* - {@link $threadPrinting} (true|false) enable/disable pid reporting.
* - {@link $categoryPrefixing} (true|false) enable/disable logger category reporting.
* - {@link $contextPrinting} (true|false) enable/disable NDC reporting.
* - {@link $microSecondsPrinting} (true|false) enable/disable micro seconds reporting in timestamp.
* - {@link $dateFormat} (string) set date format. See php {@link PHP_MANUAL#date} function for details.
*
* An example how to use this layout:
*
* {@example ../../examples/php/layout_ttcc.php 19}<br>
*
* {@example ../../examples/resources/layout_ttcc.properties 18}<br>
*
* The above would print:<br>
* <samp>02:28 [13714] INFO root - Hello World!</samp>
*
* @version $Revision: 883108 $
* @package log4php
* @subpackage layouts
*/
class LoggerLayoutTTCC extends LoggerLayout {
/**
* String constant designating no time information. Current value of
* this constant is <b>NULL</b>.
*/
// TODO: not used?
const LOG4PHP_LOGGER_LAYOUT_NULL_DATE_FORMAT = 'NULL';
/**
* String constant designating relative time. Current value of
* this constant is <b>RELATIVE</b>.
*/
// TODO: not used?
const LOG4PHP_LOGGER_LAYOUT_RELATIVE_TIME_DATE_FORMAT = 'RELATIVE';
// Internal representation of options
protected $threadPrinting = true;
protected $categoryPrefixing = true;
protected $contextPrinting = true;
protected $microSecondsPrinting = true;
/**
* @var string date format. See {@link PHP_MANUAL#strftime} for details
*/
protected $dateFormat = '%c';
/**
* Constructor
*
* @param string date format
* @see dateFormat
*/
public function __construct($dateFormat = '') {
if (!empty($dateFormat)) {
$this->dateFormat = $dateFormat;
}
return;
}
/**
* The <b>ThreadPrinting</b> option specifies whether the name of the
* current thread is part of log output or not. This is true by default.
*/
public function setThreadPrinting($threadPrinting) {
$this->threadPrinting = is_bool($threadPrinting) ?
$threadPrinting :
(bool)(strtolower($threadPrinting) == 'true');
}
/**
* @return boolean Returns value of the <b>ThreadPrinting</b> option.
*/
public function getThreadPrinting() {
return $this->threadPrinting;
}
/**
* The <b>CategoryPrefixing</b> option specifies whether {@link Category}
* name is part of log output or not. This is true by default.
*/
public function setCategoryPrefixing($categoryPrefixing) {
$this->categoryPrefixing = LoggerOptionConverter::toBoolean($categoryPrefixing);
}
/**
* @return boolean Returns value of the <b>CategoryPrefixing</b> option.
*/
public function getCategoryPrefixing() {
return $this->categoryPrefixing;
}
/**
* The <b>ContextPrinting</b> option specifies log output will include
* the nested context information belonging to the current thread.
* This is true by default.
*/
public function setContextPrinting($contextPrinting) {
$this->contextPrinting = LoggerOptionConverter::toBoolean($contextPrinting);
}
/**
* @return boolean Returns value of the <b>ContextPrinting</b> option.
*/
public function getContextPrinting() {
return $this->contextPrinting;
}
/**
* The <b>MicroSecondsPrinting</b> option specifies if microseconds infos
* should be printed at the end of timestamp.
* This is true by default.
*/
public function setMicroSecondsPrinting($microSecondsPrinting) {
$this->microSecondsPrinting = is_bool($microSecondsPrinting) ?
$microSecondsPrinting :
(bool)(strtolower($microSecondsPrinting) == 'true');
}
/**
* @return boolean Returns value of the <b>MicroSecondsPrinting</b> option.
*/
public function getMicroSecondsPrinting() {
return $this->microSecondsPrinting;
}
public function setDateFormat($dateFormat) {
$this->dateFormat = $dateFormat;
}
/**
* @return string
*/
public function getDateFormat() {
return $this->dateFormat;
}
/**
* In addition to the level of the statement and message, the
* returned string includes time, thread, category.
* <p>Time, thread, category are printed depending on options.
*
* @param LoggerLoggingEvent $event
* @return string
*/
public function format(LoggerLoggingEvent $event) {
$timeStamp = (float)$event->getTimeStamp();
$format = strftime($this->dateFormat, (int)$timeStamp);
if ($this->microSecondsPrinting) {
$usecs = floor(($timeStamp - (int)$timeStamp) * 1000);
$format .= sprintf(',%03d', $usecs);
}
$format .= ' ';
if ($this->threadPrinting) {
$format .= '['.getmypid().'] ';
}
$level = $event->getLevel();
$format .= $level->toString().' ';
if($this->categoryPrefixing) {
$format .= $event->getLoggerName().' ';
}
if($this->contextPrinting) {
$ndc = $event->getNDC();
if($ndc != null) {
$format .= $ndc.' ';
}
}
$format .= '- '.$event->getRenderedMessage();
$format .= PHP_EOL;
return $format;
}
public function ignoresThrowable() {
return true;
}
}