library/log4php/pattern/LoggerPatternConverter.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  * LoggerPatternConverter is an abstract class that provides the formatting
       
    23  * functionality that derived classes need.
       
    24  *
       
    25  * <p>Conversion specifiers in a conversion patterns are parsed to
       
    26  * individual PatternConverters. Each of which is responsible for
       
    27  * converting a logging event in a converter specific manner.</p>
       
    28  *
       
    29  * @version $Revision: 1326626 $
       
    30  * @package log4php
       
    31  * @subpackage helpers
       
    32  * @since 0.3
       
    33  */
       
    34 abstract class LoggerPatternConverter {
       
    35 
       
    36     /**
       
    37      * Next converter in the converter chain.
       
    38      * @var LoggerPatternConverter
       
    39      */
       
    40     public $next = null;
       
    41 
       
    42     /**
       
    43      * Formatting information, parsed from pattern modifiers.
       
    44      * @var LoggerFormattingInfo
       
    45      */
       
    46     protected $formattingInfo;
       
    47 
       
    48     /**
       
    49      * Converter-specific formatting options.
       
    50      * @var array
       
    51      */
       
    52     protected $option;
       
    53 
       
    54     /**
       
    55      * Constructor
       
    56      * @param LoggerFormattingInfo $formattingInfo
       
    57      * @param array $option
       
    58      */
       
    59     public function __construct(LoggerFormattingInfo $formattingInfo = null, $option = null) {
       
    60         $this->formattingInfo = $formattingInfo;
       
    61         $this->option = $option;
       
    62         $this->activateOptions();
       
    63     }
       
    64 
       
    65     /**
       
    66      * Called in constructor. Converters which need to process the options
       
    67      * can override this method.
       
    68      */
       
    69     public function activateOptions() {
       
    70     }
       
    71 
       
    72     /**
       
    73      * Converts the logging event to the desired format. Derived pattern
       
    74      * converters must implement this method.
       
    75      *
       
    76      * @param LoggerLoggingEvent $event
       
    77      */
       
    78     abstract public function convert(LoggerLoggingEvent $event);
       
    79 
       
    80     /**
       
    81      * Converts the event and formats it according to setting in the
       
    82      * Formatting information object.
       
    83      *
       
    84      * @param string &$sbuf string buffer to write to
       
    85      * @param LoggerLoggingEvent $event Event to be formatted.
       
    86      */
       
    87     public function format(&$sbuf, $event) {
       
    88         $string = $this->convert($event);
       
    89 
       
    90         if (!isset($this->formattingInfo)) {
       
    91             $sbuf .= $string;
       
    92             return;
       
    93         }
       
    94 
       
    95         $fi = $this->formattingInfo;
       
    96 
       
    97         // Empty string
       
    98         if ($string === '' || is_null($string)) {
       
    99             if ($fi->min > 0) {
       
   100                 $sbuf .= str_repeat(' ', $fi->min);
       
   101             }
       
   102             return;
       
   103         }
       
   104 
       
   105         $len = strlen($string);
       
   106 
       
   107         // Trim the string if needed
       
   108         if ($len > $fi->max) {
       
   109             if ($fi->trimLeft) {
       
   110                 $sbuf .= substr($string, $len - $fi->max, $fi->max);
       
   111             } else {
       
   112                 $sbuf .= substr($string, 0, $fi->max);
       
   113             }
       
   114         } // Add padding if needed
       
   115         else if ($len < $fi->min) {
       
   116             if ($fi->padLeft) {
       
   117                 $sbuf .= str_repeat(' ', $fi->min - $len);
       
   118                 $sbuf .= $string;
       
   119             } else {
       
   120                 $sbuf .= $string;
       
   121                 $sbuf .= str_repeat(' ', $fi->min - $len);
       
   122             }
       
   123         } // No action needed
       
   124         else {
       
   125             $sbuf .= $string;
       
   126         }
       
   127     }
       
   128 }