library/log4php/layouts/LoggerLayoutPattern.php
changeset 46 f11c31f7fa3e
parent 45 a56e7f9a0463
child 47 03388ec805b4
equal deleted inserted replaced
45:a56e7f9a0463 46:f11c31f7fa3e
     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  * A flexible layout configurable with a pattern string.
       
    23  *
       
    24  * Configurable parameters:
       
    25  *
       
    26  * * converionPattern - A string which controls the formatting of logging
       
    27  *   events. See docs for full specification.
       
    28  *
       
    29  * @package log4php
       
    30  * @subpackage layouts
       
    31  * @version $Revision: 1395470 $
       
    32  */
       
    33 class LoggerLayoutPattern extends LoggerLayout {
       
    34 
       
    35     /** Default conversion pattern */
       
    36     const DEFAULT_CONVERSION_PATTERN = '%date %-5level %logger %message%newline';
       
    37 
       
    38     /** Default conversion TTCC Pattern */
       
    39     const TTCC_CONVERSION_PATTERN = '%d [%t] %p %c %x - %m%n';
       
    40 
       
    41     /** The conversion pattern. */
       
    42     protected $pattern = self::DEFAULT_CONVERSION_PATTERN;
       
    43 
       
    44     /** Maps conversion keywords to the relevant converter (default implementation). */
       
    45     protected static $defaultConverterMap = array(
       
    46         'c' => 'LoggerPatternConverterLogger',
       
    47         'lo' => 'LoggerPatternConverterLogger',
       
    48         'logger' => 'LoggerPatternConverterLogger',
       
    49 
       
    50         'C' => 'LoggerPatternConverterClass',
       
    51         'class' => 'LoggerPatternConverterClass',
       
    52 
       
    53         'cookie' => 'LoggerPatternConverterCookie',
       
    54 
       
    55         'd' => 'LoggerPatternConverterDate',
       
    56         'date' => 'LoggerPatternConverterDate',
       
    57 
       
    58         'e' => 'LoggerPatternConverterEnvironment',
       
    59         'env' => 'LoggerPatternConverterEnvironment',
       
    60 
       
    61         'ex' => 'LoggerPatternConverterThrowable',
       
    62         'exception' => 'LoggerPatternConverterThrowable',
       
    63         'throwable' => 'LoggerPatternConverterThrowable',
       
    64 
       
    65         'F' => 'LoggerPatternConverterFile',
       
    66         'file' => 'LoggerPatternConverterFile',
       
    67 
       
    68         'l' => 'LoggerPatternConverterLocation',
       
    69         'location' => 'LoggerPatternConverterLocation',
       
    70 
       
    71         'L' => 'LoggerPatternConverterLine',
       
    72         'line' => 'LoggerPatternConverterLine',
       
    73 
       
    74         'm' => 'LoggerPatternConverterMessage',
       
    75         'msg' => 'LoggerPatternConverterMessage',
       
    76         'message' => 'LoggerPatternConverterMessage',
       
    77 
       
    78         'M' => 'LoggerPatternConverterMethod',
       
    79         'method' => 'LoggerPatternConverterMethod',
       
    80 
       
    81         'n' => 'LoggerPatternConverterNewLine',
       
    82         'newline' => 'LoggerPatternConverterNewLine',
       
    83 
       
    84         'p' => 'LoggerPatternConverterLevel',
       
    85         'le' => 'LoggerPatternConverterLevel',
       
    86         'level' => 'LoggerPatternConverterLevel',
       
    87 
       
    88         'r' => 'LoggerPatternConverterRelative',
       
    89         'relative' => 'LoggerPatternConverterRelative',
       
    90 
       
    91         'req' => 'LoggerPatternConverterRequest',
       
    92         'request' => 'LoggerPatternConverterRequest',
       
    93 
       
    94         's' => 'LoggerPatternConverterServer',
       
    95         'server' => 'LoggerPatternConverterServer',
       
    96 
       
    97         'ses' => 'LoggerPatternConverterSession',
       
    98         'session' => 'LoggerPatternConverterSession',
       
    99 
       
   100         'sid' => 'LoggerPatternConverterSessionID',
       
   101         'sessionid' => 'LoggerPatternConverterSessionID',
       
   102 
       
   103         't' => 'LoggerPatternConverterProcess',
       
   104         'pid' => 'LoggerPatternConverterProcess',
       
   105         'process' => 'LoggerPatternConverterProcess',
       
   106 
       
   107         'x' => 'LoggerPatternConverterNDC',
       
   108         'ndc' => 'LoggerPatternConverterNDC',
       
   109 
       
   110         'X' => 'LoggerPatternConverterMDC',
       
   111         'mdc' => 'LoggerPatternConverterMDC',
       
   112     );
       
   113 
       
   114     /** Maps conversion keywords to the relevant converter. */
       
   115     protected $converterMap = array();
       
   116 
       
   117     /**
       
   118      * Head of a chain of Converters.
       
   119      * @var LoggerPatternConverter
       
   120      */
       
   121     private $head;
       
   122 
       
   123     /** Returns the default converter map. */
       
   124     public static function getDefaultConverterMap() {
       
   125         return self::$defaultConverterMap;
       
   126     }
       
   127 
       
   128     /** Constructor. Initializes the converter map. */
       
   129     public function __construct() {
       
   130         $this->converterMap = self::$defaultConverterMap;
       
   131     }
       
   132 
       
   133     /**
       
   134      * Sets the conversionPattern option. This is the string which
       
   135      * controls formatting and consists of a mix of literal content and
       
   136      * conversion specifiers.
       
   137      * @param array $conversionPattern
       
   138      */
       
   139     public function setConversionPattern($conversionPattern) {
       
   140         $this->pattern = $conversionPattern;
       
   141     }
       
   142 
       
   143     /**
       
   144      * Processes the conversion pattern and creates a corresponding chain of
       
   145      * pattern converters which will be used to format logging events.
       
   146      */
       
   147     public function activateOptions() {
       
   148         if (!isset($this->pattern)) {
       
   149             throw new LoggerException("Mandatory parameter 'conversionPattern' is not set.");
       
   150         }
       
   151 
       
   152         $parser = new LoggerPatternParser($this->pattern, $this->converterMap);
       
   153         $this->head = $parser->parse();
       
   154     }
       
   155 
       
   156     /**
       
   157      * Produces a formatted string as specified by the conversion pattern.
       
   158      *
       
   159      * @param LoggerLoggingEvent $event
       
   160      * @return string
       
   161      */
       
   162     public function format(LoggerLoggingEvent $event) {
       
   163         $sbuf = '';
       
   164         $converter = $this->head;
       
   165         while ($converter !== null) {
       
   166             $converter->format($sbuf, $event);
       
   167             $converter = $converter->next;
       
   168         }
       
   169         return $sbuf;
       
   170     }
       
   171 }