PageRenderTime 53ms CodeModel.GetById 31ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/php/log4php/layouts/LoggerLayoutTTCC.php

https://code.google.com/p/plexnmt/
PHP | 201 lines | 77 code | 25 blank | 99 comment | 9 complexity | c9b84a435094f3e104ec49c7d9dba57e MD5 | raw file
  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: 1136787 $
 48 * @package log4php
 49 * @subpackage layouts
 50 */
 51class 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		if (!empty($dateFormat)) {
 72			$this->dateFormat = $dateFormat;
 73		}
 74		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		$this->threadPrinting = is_bool($threadPrinting) ? 
 83			$threadPrinting : 
 84			(bool)(strtolower($threadPrinting) == 'true'); 
 85	}
 86
 87	/**
 88	 * @return boolean Returns value of the <b>ThreadPrinting</b> option.
 89	 */
 90	public function getThreadPrinting() {
 91		return $this->threadPrinting;
 92	}
 93
 94	/**
 95	 * The <b>CategoryPrefixing</b> option specifies whether {@link Category}
 96	 * name is part of log output or not. This is true by default.
 97	 */
 98	public function setCategoryPrefixing($categoryPrefixing) {
 99		$this->categoryPrefixing = LoggerOptionConverter::toBoolean($categoryPrefixing);
100	}
101
102	/**
103	 * @return boolean Returns value of the <b>CategoryPrefixing</b> option.
104	 */
105	public function getCategoryPrefixing() {
106		return $this->categoryPrefixing;
107	}
108
109	/**
110	 * The <b>ContextPrinting</b> option specifies log output will include
111	 * the nested context information belonging to the current thread.
112	 * This is true by default.
113	 */
114	public function setContextPrinting($contextPrinting) {
115		$this->contextPrinting = LoggerOptionConverter::toBoolean($contextPrinting); 
116	}
117
118	/**
119	 * @return boolean Returns value of the <b>ContextPrinting</b> option.
120	 */
121	public function getContextPrinting() {
122		return $this->contextPrinting;
123	}
124	
125	/**
126	 * The <b>MicroSecondsPrinting</b> option specifies if microseconds infos
127	 * should be printed at the end of timestamp.
128	 * This is true by default.
129	 */
130	public function setMicroSecondsPrinting($microSecondsPrinting) {
131		$this->microSecondsPrinting = is_bool($microSecondsPrinting) ? 
132			$microSecondsPrinting : 
133			(bool)(strtolower($microSecondsPrinting) == 'true'); 
134	}
135
136	/**
137	 * @return boolean Returns value of the <b>MicroSecondsPrinting</b> option.
138	 */
139	public function getMicroSecondsPrinting() {
140		return $this->microSecondsPrinting;
141	}
142	
143	
144	public function setDateFormat($dateFormat) {
145		$this->dateFormat = $dateFormat;
146	}
147	
148	/**
149	 * @return string
150	 */
151	public function getDateFormat() {
152		return $this->dateFormat;
153	}
154
155	/**
156	 * In addition to the level of the statement and message, the
157	 * returned string includes time, thread, category.
158	 * <p>Time, thread, category are printed depending on options.
159	 *
160	 * @param LoggerLoggingEvent $event
161	 * @return string
162	 */
163	public function format(LoggerLoggingEvent $event) {
164		$timeStamp = (float)$event->getTimeStamp();
165		$format = strftime($this->dateFormat, (int)$timeStamp);
166		
167		if ($this->microSecondsPrinting) {
168			$usecs = floor(($timeStamp - (int)$timeStamp) * 1000);
169			$format .= sprintf(',%03d', $usecs);
170		}
171			
172		$format .= ' ';
173		
174		if ($this->threadPrinting) {
175			$format .= '['.getmypid().'] ';
176		}
177		
178		$level = $event->getLevel();
179		$format .= $level.' ';
180		
181		if($this->categoryPrefixing) {
182			$format .= $event->getLoggerName().' ';
183		}
184	   
185		if($this->contextPrinting) {
186			$ndc = $event->getNDC();
187			if($ndc != null) {
188				$format .= $ndc.' ';
189			}
190		}
191		
192		$format .= '- '.$event->getRenderedMessage();
193		$format .= PHP_EOL;
194		
195		return $format;
196	}
197
198	public function ignoresThrowable() {
199		return true;
200	}
201}