PageRenderTime 26ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/scalr-2/tags/scalr-2.0.0/app/src/externals/apache-log4php-2.0.0-incubating/src/main/php/layouts/LoggerLayoutTTCC.php

http://scalr.googlecode.com/
PHP | 215 lines | 79 code | 27 blank | 109 comment | 9 complexity | 31e5b4a3d9dcd6ab324db43ee11ad7d1 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: 883108 $
 48 * @package log4php
 49 * @subpackage layouts
 50 */
 51class LoggerLayoutTTCC extends LoggerLayout {
 52
 53	/**
 54	 * String constant designating no time information. Current value of
 55	 * this constant is <b>NULL</b>.
 56	 */
 57	 // TODO: not used?
 58	const LOG4PHP_LOGGER_LAYOUT_NULL_DATE_FORMAT = 'NULL';
 59	
 60	/**
 61	 * String constant designating relative time. Current value of
 62	 * this constant is <b>RELATIVE</b>.
 63	 */
 64	 // TODO: not used?
 65	const LOG4PHP_LOGGER_LAYOUT_RELATIVE_TIME_DATE_FORMAT = 'RELATIVE';
 66	
 67    // Internal representation of options
 68    protected $threadPrinting    = true;
 69    protected $categoryPrefixing = true;
 70    protected $contextPrinting   = true;
 71    protected $microSecondsPrinting = true;
 72    
 73    /**
 74     * @var string date format. See {@link PHP_MANUAL#strftime} for details
 75     */
 76    protected $dateFormat = '%c';
 77
 78    /**
 79     * Constructor
 80     *
 81     * @param string date format
 82     * @see dateFormat
 83     */
 84    public function __construct($dateFormat = '') {
 85        if (!empty($dateFormat)) {
 86            $this->dateFormat = $dateFormat;
 87        }
 88        return;
 89    }
 90
 91    /**
 92     * The <b>ThreadPrinting</b> option specifies whether the name of the
 93     * current thread is part of log output or not. This is true by default.
 94     */
 95    public function setThreadPrinting($threadPrinting) {
 96        $this->threadPrinting = is_bool($threadPrinting) ? 
 97            $threadPrinting : 
 98            (bool)(strtolower($threadPrinting) == 'true'); 
 99    }
100
101    /**
102     * @return boolean Returns value of the <b>ThreadPrinting</b> option.
103     */
104    public function getThreadPrinting() {
105        return $this->threadPrinting;
106    }
107
108    /**
109     * The <b>CategoryPrefixing</b> option specifies whether {@link Category}
110     * name is part of log output or not. This is true by default.
111     */
112    public function setCategoryPrefixing($categoryPrefixing) {
113        $this->categoryPrefixing = LoggerOptionConverter::toBoolean($categoryPrefixing);
114    }
115
116    /**
117     * @return boolean Returns value of the <b>CategoryPrefixing</b> option.
118     */
119    public function getCategoryPrefixing() {
120        return $this->categoryPrefixing;
121    }
122
123    /**
124     * The <b>ContextPrinting</b> option specifies log output will include
125     * the nested context information belonging to the current thread.
126     * This is true by default.
127     */
128    public function setContextPrinting($contextPrinting) {
129        $this->contextPrinting = LoggerOptionConverter::toBoolean($contextPrinting); 
130    }
131
132    /**
133     * @return boolean Returns value of the <b>ContextPrinting</b> option.
134     */
135    public function getContextPrinting() {
136        return $this->contextPrinting;
137    }
138    
139    /**
140     * The <b>MicroSecondsPrinting</b> option specifies if microseconds infos
141     * should be printed at the end of timestamp.
142     * This is true by default.
143     */
144    public function setMicroSecondsPrinting($microSecondsPrinting) {
145        $this->microSecondsPrinting = is_bool($microSecondsPrinting) ? 
146            $microSecondsPrinting : 
147            (bool)(strtolower($microSecondsPrinting) == 'true'); 
148    }
149
150    /**
151     * @return boolean Returns value of the <b>MicroSecondsPrinting</b> option.
152     */
153    public function getMicroSecondsPrinting() {
154        return $this->microSecondsPrinting;
155    }
156    
157    
158    public function setDateFormat($dateFormat) {
159        $this->dateFormat = $dateFormat;
160    }
161    
162    /**
163     * @return string
164     */
165    public function getDateFormat() {
166        return $this->dateFormat;
167    }
168
169    /**
170     * In addition to the level of the statement and message, the
171     * returned string includes time, thread, category.
172     * <p>Time, thread, category are printed depending on options.
173     *
174     * @param LoggerLoggingEvent $event
175     * @return string
176     */
177    public function format(LoggerLoggingEvent $event) {
178        $timeStamp = (float)$event->getTimeStamp();
179        $format = strftime($this->dateFormat, (int)$timeStamp);
180        
181        if ($this->microSecondsPrinting) {
182            $usecs = floor(($timeStamp - (int)$timeStamp) * 1000);
183            $format .= sprintf(',%03d', $usecs);
184        }
185            
186        $format .= ' ';
187        
188        if ($this->threadPrinting) {
189            $format .= '['.getmypid().'] ';
190        }
191        
192        $level = $event->getLevel();
193        $format .= $level->toString().' ';
194        
195        if($this->categoryPrefixing) {
196            $format .= $event->getLoggerName().' ';
197        }
198       
199        if($this->contextPrinting) {
200            $ndc = $event->getNDC();
201            if($ndc != null) {
202                $format .= $ndc.' ';
203            }
204        }
205        
206        $format .= '- '.$event->getRenderedMessage();
207        $format .= PHP_EOL;
208        
209        return $format;
210    }
211
212    public function ignoresThrowable() {
213        return true;
214    }
215}