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 : * @package log4php
19 : */
20 :
21 : /**
22 : * TTCC layout format consists of <b>t</b>ime, <b>t</b>hread, <b>c</b>ategory and nested
23 : * diagnostic <b>c</b>ontext information, hence the name.
24 : *
25 : * <p>Each of the four fields can be individually enabled or
26 : * disabled. The time format depends on the <b>DateFormat</b> used.</p>
27 : *
28 : * <p>If no dateFormat is specified it defaults to '%c'.
29 : * See php {@link PHP_MANUAL#date} function for details.</p>
30 : *
31 : * Configurable parameters for this layout are:
32 : * - {@link $threadPrinting} (true|false) enable/disable pid reporting.
33 : * - {@link $categoryPrefixing} (true|false) enable/disable logger category reporting.
34 : * - {@link $contextPrinting} (true|false) enable/disable NDC reporting.
35 : * - {@link $microSecondsPrinting} (true|false) enable/disable micro seconds reporting in timestamp.
36 : * - {@link $dateFormat} (string) set date format. See php {@link PHP_MANUAL#date} function for details.
37 : *
38 : * An example how to use this layout:
39 : *
40 : * {@example ../../examples/php/layout_ttcc.php 19}<br>
41 : *
42 : * {@example ../../examples/resources/layout_ttcc.properties 18}<br>
43 : *
44 : * The above would print:<br>
45 : * <samp>02:28 [13714] INFO root - Hello World!</samp>
46 : *
47 : * @version $Revision: 1213283 $
48 : * @package log4php
49 : * @subpackage layouts
50 : */
51 : class LoggerLayoutTTCC extends LoggerLayout {
52 :
53 : // Internal representation of options
54 : protected $threadPrinting = true;
55 : protected $categoryPrefixing = true;
56 : protected $contextPrinting = true;
57 : protected $microSecondsPrinting = true;
58 :
59 : /**
60 : * @var string date format. See {@link PHP_MANUAL#strftime} for details
61 : */
62 : protected $dateFormat = '%c';
63 :
64 : /**
65 : * Constructor
66 : *
67 : * @param string date format
68 : * @see dateFormat
69 : */
70 : public function __construct($dateFormat = '') {
71 14 : if (!empty($dateFormat)) {
72 0 : $this->dateFormat = $dateFormat;
73 0 : }
74 14 : return;
75 : }
76 :
77 : /**
78 : * The <b>ThreadPrinting</b> option specifies whether the name of the
79 : * current thread is part of log output or not. This is true by default.
80 : */
81 : public function setThreadPrinting($threadPrinting) {
82 0 : $this->setBoolean('threadPrinting', $threadPrinting);
83 0 : }
84 :
85 : /**
86 : * @return boolean Returns value of the <b>ThreadPrinting</b> option.
87 : */
88 : public function getThreadPrinting() {
89 0 : return $this->threadPrinting;
90 : }
91 :
92 : /**
93 : * The <b>CategoryPrefixing</b> option specifies whether {@link Category}
94 : * name is part of log output or not. This is true by default.
95 : */
96 : public function setCategoryPrefixing($categoryPrefixing) {
97 0 : $this->setBoolean('categoryPrefixing', $categoryPrefixing);
98 0 : }
99 :
100 : /**
101 : * @return boolean Returns value of the <b>CategoryPrefixing</b> option.
102 : */
103 : public function getCategoryPrefixing() {
104 0 : return $this->categoryPrefixing;
105 : }
106 :
107 : /**
108 : * The <b>ContextPrinting</b> option specifies log output will include
109 : * the nested context information belonging to the current thread.
110 : * This is true by default.
111 : */
112 : public function setContextPrinting($contextPrinting) {
113 0 : $this->setBoolean('contextPrinting', $contextPrinting);
114 0 : }
115 :
116 : /**
117 : * @return boolean Returns value of the <b>ContextPrinting</b> option.
118 : */
119 : public function getContextPrinting() {
120 0 : return $this->contextPrinting;
121 : }
122 :
123 : /**
124 : * The <b>MicroSecondsPrinting</b> option specifies if microseconds infos
125 : * should be printed at the end of timestamp.
126 : * This is true by default.
127 : */
128 : public function setMicroSecondsPrinting($microSecondsPrinting) {
129 0 : $this->setBoolean('microSecondsPrinting', $microSecondsPrinting);
130 0 : }
131 :
132 : /**
133 : * @return boolean Returns value of the <b>MicroSecondsPrinting</b> option.
134 : */
135 : public function getMicroSecondsPrinting() {
136 0 : return $this->microSecondsPrinting;
137 : }
138 :
139 :
140 : public function setDateFormat($dateFormat) {
141 0 : $this->setString('dateFormat', $dateFormat);
142 0 : }
143 :
144 : /**
145 : * @return string
146 : */
147 : public function getDateFormat() {
148 0 : return $this->dateFormat;
149 : }
150 :
151 : /**
152 : * In addition to the level of the statement and message, the
153 : * returned string includes time, thread, category.
154 : * <p>Time, thread, category are printed depending on options.
155 : *
156 : * @param LoggerLoggingEvent $event
157 : * @return string
158 : */
159 : public function format(LoggerLoggingEvent $event) {
160 2 : $timeStamp = (float)$event->getTimeStamp();
161 2 : $format = strftime($this->dateFormat, (int)$timeStamp);
162 :
163 2 : if ($this->microSecondsPrinting) {
164 2 : $usecs = floor(($timeStamp - (int)$timeStamp) * 1000);
165 2 : $format .= sprintf(',%03d', $usecs);
166 2 : }
167 :
168 2 : $format .= ' ';
169 :
170 2 : if ($this->threadPrinting) {
171 2 : $format .= '['.getmypid().'] ';
172 2 : }
173 :
174 2 : $level = $event->getLevel();
175 2 : $format .= $level.' ';
176 :
177 2 : if($this->categoryPrefixing) {
178 2 : $format .= $event->getLoggerName().' ';
179 2 : }
180 :
181 2 : if($this->contextPrinting) {
182 2 : $ndc = $event->getNDC();
183 2 : if($ndc != null) {
184 0 : $format .= $ndc.' ';
185 0 : }
186 2 : }
187 :
188 2 : $format .= '- '.$event->getRenderedMessage();
189 2 : $format .= PHP_EOL;
190 :
191 2 : return $format;
192 : }
193 :
194 : public function ignoresThrowable() {
195 0 : return true;
196 : }
197 : }
|