library/log4php/layouts/LoggerLayoutTTCC.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     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: 1302503 $
       
    48  * @package log4php
       
    49  * @subpackage layouts
       
    50  *
       
    51  * @deprecated LoggerLayout TTCC is deprecated and will be removed in a future release. Please use
       
    52  *   LoggerLayoutPattern instead.
       
    53  */
       
    54 class LoggerLayoutTTCC extends LoggerLayout {
       
    55 
       
    56     // Internal representation of options
       
    57     protected $threadPrinting = true;
       
    58     protected $categoryPrefixing = true;
       
    59     protected $contextPrinting = true;
       
    60     protected $microSecondsPrinting = true;
       
    61 
       
    62     /**
       
    63      * @var string date format. See {@link PHP_MANUAL#strftime} for details
       
    64      */
       
    65     protected $dateFormat = '%c';
       
    66 
       
    67     /**
       
    68      * Constructor
       
    69      *
       
    70      * @param string date format
       
    71      * @see dateFormat
       
    72      */
       
    73     public function __construct($dateFormat = '') {
       
    74         $this->warn("LoggerLayout TTCC is deprecated and will be removed in a future release. Please use LoggerLayoutPattern instead.");
       
    75         if (!empty($dateFormat)) {
       
    76             $this->dateFormat = $dateFormat;
       
    77         }
       
    78         return;
       
    79     }
       
    80 
       
    81     /**
       
    82      * The <b>ThreadPrinting</b> option specifies whether the name of the
       
    83      * current thread is part of log output or not. This is true by default.
       
    84      */
       
    85     public function setThreadPrinting($threadPrinting) {
       
    86         $this->setBoolean('threadPrinting', $threadPrinting);
       
    87     }
       
    88 
       
    89     /**
       
    90      * @return boolean Returns value of the <b>ThreadPrinting</b> option.
       
    91      */
       
    92     public function getThreadPrinting() {
       
    93         return $this->threadPrinting;
       
    94     }
       
    95 
       
    96     /**
       
    97      * The <b>CategoryPrefixing</b> option specifies whether {@link Category}
       
    98      * name is part of log output or not. This is true by default.
       
    99      */
       
   100     public function setCategoryPrefixing($categoryPrefixing) {
       
   101         $this->setBoolean('categoryPrefixing', $categoryPrefixing);
       
   102     }
       
   103 
       
   104     /**
       
   105      * @return boolean Returns value of the <b>CategoryPrefixing</b> option.
       
   106      */
       
   107     public function getCategoryPrefixing() {
       
   108         return $this->categoryPrefixing;
       
   109     }
       
   110 
       
   111     /**
       
   112      * The <b>ContextPrinting</b> option specifies log output will include
       
   113      * the nested context information belonging to the current thread.
       
   114      * This is true by default.
       
   115      */
       
   116     public function setContextPrinting($contextPrinting) {
       
   117         $this->setBoolean('contextPrinting', $contextPrinting);
       
   118     }
       
   119 
       
   120     /**
       
   121      * @return boolean Returns value of the <b>ContextPrinting</b> option.
       
   122      */
       
   123     public function getContextPrinting() {
       
   124         return $this->contextPrinting;
       
   125     }
       
   126 
       
   127     /**
       
   128      * The <b>MicroSecondsPrinting</b> option specifies if microseconds infos
       
   129      * should be printed at the end of timestamp.
       
   130      * This is true by default.
       
   131      */
       
   132     public function setMicroSecondsPrinting($microSecondsPrinting) {
       
   133         $this->setBoolean('microSecondsPrinting', $microSecondsPrinting);
       
   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->setString('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 }