PageRenderTime 33ms CodeModel.GetById 15ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/log4php/layouts/LoggerLayoutTTCC.php

http://sabre-zarafa.googlecode.com/
PHP | 197 lines | 73 code | 25 blank | 99 comment | 7 complexity | 3f2175a87f268e4617c52e4e392b8225 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: 31 $
 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->setBoolean('threadPrinting', $threadPrinting);
 83	}
 84
 85	/**
 86	 * @return boolean Returns value of the <b>ThreadPrinting</b> option.
 87	 */
 88	public function getThreadPrinting() {
 89		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		$this->setBoolean('categoryPrefixing', $categoryPrefixing);
 98	}
 99
100	/**
101	 * @return boolean Returns value of the <b>CategoryPrefixing</b> option.
102	 */
103	public function getCategoryPrefixing() {
104		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		$this->setBoolean('contextPrinting', $contextPrinting);
114	}
115
116	/**
117	 * @return boolean Returns value of the <b>ContextPrinting</b> option.
118	 */
119	public function getContextPrinting() {
120		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		$this->setBoolean('microSecondsPrinting', $microSecondsPrinting);
130	}
131
132	/**
133	 * @return boolean Returns value of the <b>MicroSecondsPrinting</b> option.
134	 */
135	public function getMicroSecondsPrinting() {
136		return $this->microSecondsPrinting;
137	}
138	
139	
140	public function setDateFormat($dateFormat) {
141		$this->setString('dateFormat', $dateFormat);
142	}
143	
144	/**
145	 * @return string
146	 */
147	public function getDateFormat() {
148		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		$timeStamp = (float)$event->getTimeStamp();
161		$format = strftime($this->dateFormat, (int)$timeStamp);
162		
163		if ($this->microSecondsPrinting) {
164			$usecs = floor(($timeStamp - (int)$timeStamp) * 1000);
165			$format .= sprintf(',%03d', $usecs);
166		}
167			
168		$format .= ' ';
169		
170		if ($this->threadPrinting) {
171			$format .= '['.getmypid().'] ';
172		}
173		
174		$level = $event->getLevel();
175		$format .= $level.' ';
176		
177		if($this->categoryPrefixing) {
178			$format .= $event->getLoggerName().' ';
179		}
180	   
181		if($this->contextPrinting) {
182			$ndc = $event->getNDC();
183			if($ndc != null) {
184				$format .= $ndc.' ';
185			}
186		}
187		
188		$format .= '- '.$event->getRenderedMessage();
189		$format .= PHP_EOL;
190		
191		return $format;
192	}
193
194	public function ignoresThrowable() {
195		return true;
196	}
197}