Overview

Packages

  • log4php
    • appenders
    • configurators
    • filters
    • helpers
    • layouts
    • pattern
    • renderers

Classes

  • LoggerFormattingInfo
  • LoggerOptionConverter
  • LoggerPatternConverter
  • LoggerPatternParser
  • LoggerUtils
  • Overview
  • Package
  • Class
  • Tree
  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:      * Converts the logging event to the desired format. Derived pattern 
 73:      * converters must implement this method.
 74:      *
 75:      * @param LoggerLoggingEvent $event
 76:      */
 77:     abstract public function convert(LoggerLoggingEvent $event);
 78: 
 79:     /**
 80:      * Converts the event and formats it according to setting in the 
 81:      * Formatting information object.
 82:      *
 83:      * @param string &$sbuf string buffer to write to
 84:      * @param LoggerLoggingEvent $event Event to be formatted.
 85:      */
 86:     public function format(&$sbuf, $event) {
 87:         $string = $this->convert($event);
 88:         
 89:         if (!isset($this->formattingInfo)) {
 90:             $sbuf .= $string;
 91:             return; 
 92:         }
 93:         
 94:         $fi = $this->formattingInfo;
 95:         
 96:         // Empty string
 97:         if($string === '' || is_null($string)) {
 98:             if($fi->min > 0) {
 99:                 $sbuf .= str_repeat(' ', $fi->min);
100:             }
101:             return;
102:         }
103:         
104:         $len = strlen($string);
105:     
106:         // Trim the string if needed
107:         if($len > $fi->max) {
108:             if ($fi->trimLeft) {
109:                 $sbuf .= substr($string, $len - $fi->max, $fi->max);
110:             } else {
111:                 $sbuf .= substr($string , 0, $fi->max);
112:             }
113:         }
114:         
115:         // Add padding if needed
116:         else if($len < $fi->min) {
117:             if($fi->padLeft) {
118:                 $sbuf .= str_repeat(' ', $fi->min - $len);
119:                 $sbuf .= $string;
120:             } else {
121:                 $sbuf .= $string;
122:                 $sbuf .= str_repeat(' ', $fi->min - $len);
123:             }
124:         }
125:         
126:         // No action needed
127:         else {
128:             $sbuf .= $string;
129:         }
130:     }
131: }
132: 
Apache log4php API documentation generated by ApiGen 2.8.0