LoggerLayoutPattern.php
Current file: /home/ihabunek/apache/log4php/src/main/php/layouts/LoggerLayoutPattern.php
Legend: executed not executed dead code

  Coverage
  Classes Functions / Methods Lines
Total
100.00% 1 / 1
100.00% 3 / 3 CRAP
100.00% 22 / 22
LoggerLayoutPattern
100.00% 1 / 1
100.00% 3 / 3 6
100.00% 22 / 22
 setConversionPattern($conversionPattern)
100.00% 1 / 1 1
100.00% 4 / 4
 format(LoggerLoggingEvent $event)
100.00% 1 / 1 2
100.00% 7 / 7
 formatToArray(LoggerLoggingEvent $event)
100.00% 1 / 1 3
100.00% 11 / 11


       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 pattern string.                                                                                                                                                                                                                                                                                                                                        
      23                 :  *                                                                                                                                                                                                                                                                                                                                                                                            
      24                 :  * <p>Example:</p>                                                                                                                                                                                                                                                                                                                                                                            
      25                 :  *                                                                                                                                                                                                                                                                                                                                                                                            
      26                 :  * {@example ../../examples/php/layout_pattern.php 19}<br>                                                                                                                                                                                                                                                                                                                                    
      27                 :  *                                                                                                                                                                                                                                                                                                                                                                                            
      28                 :  * <p>with the following properties file:</p>                                                                                                                                                                                                                                                                                                                                                 
      29                 :  *                                                                                                                                                                                                                                                                                                                                                                                            
      30                 :  * {@example ../../examples/resources/layout_pattern.properties 18}<br>                                                                                                                                                                                                                                                                                                                       
      31                 :  *                                                                                                                                                                                                                                                                                                                                                                                            
      32                 :  * <p>would print the following:</p>                                                                                                                                                                                                                                                                                                                                                          
      33                 :  *                                                                                                                                                                                                                                                                                                                                                                                            
      34                 :  * <pre>                                                                                                                                                                                                                                                                                                                                                                                      
      35                 :  * 2009-09-09 00:27:35,787 [INFO] root: Hello World! (at src/examples/php/layout_pattern.php line 6)                                                                                                                                                                                                                                                                                          
      36                 :  * 2009-09-09 00:27:35,787 [DEBUG] root: Second line (at src/examples/php/layout_pattern.php line 7)                                                                                                                                                                                                                                                                                          
      37                 :  * </pre>                                                                                                                                                                                                                                                                                                                                                                                     
      38                 :  *                                                                                                                                                                                                                                                                                                                                                                                            
      39                 :  * <p>The conversion pattern is closely related to the conversion pattern of the printf function in C.                                                                                                                                                                                                                                                                                        
      40                 :  * A conversion pattern is composed of literal text and format control expressions called conversion specifiers.                                                                                                                                                                                                                                                                              
      41                 :  * You are free to insert any literal text within the conversion pattern.</p>                                                                                                                                                                                                                                                                                                                 
      42                 :  *                                                                                                                                                                                                                                                                                                                                                                                            
      43                 :  * <p>Each conversion specifier starts with a percent sign (%) and is followed by optional                                                                                                                                                                                                                                                                                                    
      44                 :  * format modifiers and a conversion character.</p>                                                                                                                                                                                                                                                                                                                                           
      45                 :  *                                                                                                                                                                                                                                                                                                                                                                                            
      46                 :  * <p>The conversion character specifies the type of data, e.g. category, priority, date, thread name.                                                                                                                                                                                                                                                                                        
      47                 :  * The format modifiers control such things as field width, padding, left and right justification.</p>                                                                                                                                                                                                                                                                                        
      48                 :  *                                                                                                                                                                                                                                                                                                                                                                                            
      49                 :  * <p>Note that there is no explicit separator between text and conversion specifiers.</p>                                                                                                                                                                                                                                                                                                    
      50                 :  *                                                                                                                                                                                                                                                                                                                                                                                            
      51                 :  * <p>The pattern parser knows when it has reached the end of a conversion specifier when it reads a conversion character.                                                                                                                                                                                                                                                                    
      52                 :  * In the example above the conversion specifier %-5p means the priority of the logging event should be                                                                                                                                                                                                                                                                                       
      53                 :  * left justified to a width of five characters.</p>                                                                                                                                                                                                                                                                                                                                          
      54                 :  *                                                                                                                                                                                                                                                                                                                                                                                            
      55                 :  * Not all log4j conversion characters are implemented. The recognized conversion characters are:                                                                                                                                                                                                                                                                                             
      56                 :  * - <b>c</b> Used to output the category of the logging event. The category conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets.                                                                                                                                                                                                  
      57                 :  *         If a precision specifier is given, then only the corresponding number of right most components of the category name will be printed.                                                                                                                                                                                                                                               
      58                 :  *         By default the category name is printed in full.                                                                                                                                                                                                                                                                                                                                   
      59                 :  *         For example, for the category name "a.b.c" the pattern %c{2} will output "b.c".                                                                                                                                                                                                                                                                                                    
      60                 :  * - <b>C</b> Used to output the fully qualified class name of the caller issuing the logging request.                                                                                                                                                                                                                                                                                        
      61                 :  *         This conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets.                                                                                                                                                                                                                                                               
      62                 :  *         If a precision specifier is given, then only the corresponding number of right most components of the class name will be printed.                                                                                                                                                                                                                                                  
      63                 :  *         By default the class name is output in fully qualified form.                                                                                                                                                                                                                                                                                                                       
      64                 :  *         For example, for the class name "org.apache.xyz.SomeClass", the pattern %C{1} will output "SomeClass".                                                                                                                                                                                                                                                                             
      65                 :  * - <b>d</b> Used to output the date of the logging event.                                                                                                                                                                                                                                                                                                                                   
      66                 :  *         The date conversion specifier may be followed by a date format specifier enclosed between braces.                                                                                                                                                                                                                                                                                  
      67                 :  *         The format specifier follows the {@link PHP_MANUAL#date} function.                                                                                                                                                                                                                                                                                                                 
      68                 :  *         Note that the special character <b>u</b> is used to as microseconds replacement (to avoid replacement,                                                                                                                                                                                                                                                                             
      69                 :  *         use <b>\u</b>).                                                                                                                                                                                                                                                                                                                                                                    
      70                 :  *         For example, %d{H:i:s,u} or %d{d M Y H:i:s,u}. If no date format specifier is given then ISO8601 format is assumed.                                                                                                                                                                                                                                                                
      71                 :  *         The date format specifier admits the same syntax as the time pattern string of the SimpleDateFormat.                                                                                                                                                                                                                                                                               
      72                 :  *         It is recommended to use the predefined log4php date formatters.                                                                                                                                                                                                                                                                                                                   
      73                 :  *         These can be specified using one of the strings "ABSOLUTE", "DATE" and "ISO8601" for specifying                                                                                                                                                                                                                                                                                    
      74                 :  *         AbsoluteTimeDateFormat, DateTimeDateFormat and respectively ISO8601DateFormat.                                                                                                                                                                                                                                                                                                     
      75                 :  *         For example, %d{ISO8601} or %d{ABSOLUTE}.                                                                                                                                                                                                                                                                                                                                          
      76                 :  * - <b>F</b> Used to output the file name where the logging request was issued.                                                                                                                                                                                                                                                                                                              
      77                 :  * - <b>l</b> Used to output location information of the caller which generated the logging event.                                                                                                                                                                                                                                                                                            
      78                 :  * - <b>L</b> Used to output the line number from where the logging request was issued.                                                                                                                                                                                                                                                                                                       
      79                 :  * - <b>m</b> Used to output the application supplied message associated with the logging event.                                                                                                                                                                                                                                                                                              
      80                 :  * - <b>M</b> Used to output the method name where the logging request was issued.                                                                                                                                                                                                                                                                                                            
      81                 :  * - <b>p</b> Used to output the priority of the logging event.                                                                                                                                                                                                                                                                                                                               
      82                 :  * - <b>r</b> Used to output the number of milliseconds elapsed since the start of                                                                                                                                                                                                                                                                                                            
      83                 :  *            the application until the creation of the logging event.                                                                                                                                                                                                                                                                                                                        
      84                 :  * - <b>t</b> Used to output the name of the thread that generated the logging event.                                                                                                                                                                                                                                                                                                         
      85                 :  * - <b>x</b> Used to output the NDC (nested diagnostic context) associated with                                                                                                                                                                                                                                                                                                              
      86                 :  *            the thread that generated the logging event.                                                                                                                                                                                                                                                                                                                                    
      87                 :  * - <b>X</b> Used to output the MDC (mapped diagnostic context) associated with                                                                                                                                                                                                                                                                                                              
      88                 :  *            the thread that generated the logging event.                                                                                                                                                                                                                                                                                                                                    
      89                 :  *            The X conversion character must be followed by the key for the map placed between braces,                                                                                                                                                                                                                                                                                       
      90                 :  *            as in <i>%X{clientNumber}</i> where clientNumber is the key.                                                                                                                                                                                                                                                                                                                    
      91                 :  *            The value in the MDC corresponding to the key will be output.                                                                                                                                                                                                                                                                                                                   
      92                 :  *            See {@link LoggerMDC} class for more details.                                                                                                                                                                                                                                                                                                                                   
      93                 :  * - <b>%</b> The sequence %% outputs a single percent sign.                                                                                                                                                                                                                                                                                                                                  
      94                 :  *                                                                                                                                                                                                                                                                                                                                                                                            
      95                 :  * <p>By default the relevant information is output as is.                                                                                                                                                                                                                                                                                                                                    
      96                 :  *  However, with the aid of format modifiers it is possible to change the minimum field width,                                                                                                                                                                                                                                                                                               
      97                 :  *  the maximum field width and justification.</p>                                                                                                                                                                                                                                                                                                                                            
      98                 :  *                                                                                                                                                                                                                                                                                                                                                                                            
      99                 :  * <p>The optional format modifier is placed between the percent sign and the conversion character.</p>                                                                                                                                                                                                                                                                                       
     100                 :  * <p>The first optional format modifier is the left justification flag which is just the minus (-) character.                                                                                                                                                                                                                                                                                
     101                 :  *  Then comes the optional minimum field width modifier.                                                                                                                                                                                                                                                                                                                                     
     102                 :  *  This is a decimal constant that represents the minimum number of characters to output.                                                                                                                                                                                                                                                                                                    
     103                 :  *  If the data item requires fewer characters, it is padded on either the left or the right until the minimum width is reached. The default is to pad on the left (right justify) but you can specify right padding with the left justification flag. The padding character is space. If the data item is larger than the minimum field width, the field is expanded to accommodate the data.
     104                 :  *  The value is never truncated.</p>                                                                                                                                                                                                                                                                                                                                                         
     105                 :  *                                                                                                                                                                                                                                                                                                                                                                                            
     106                 :  * <p>This behavior can be changed using the maximum field width modifier which is designated by a period                                                                                                                                                                                                                                                                                     
     107                 :  *  followed by a decimal constant.                                                                                                                                                                                                                                                                                                                                                           
     108                 :  *  If the data item is longer than the maximum field,                                                                                                                                                                                                                                                                                                                                        
     109                 :  *  then the extra characters are removed from the beginning of the data item and not from the end.                                                                                                                                                                                                                                                                                           
     110                 :  *  For example, it the maximum field width is eight and the data item is ten characters long,                                                                                                                                                                                                                                                                                                
     111                 :  *  then the first two characters of the data item are dropped.                                                                                                                                                                                                                                                                                                                               
     112                 :  *  This behavior deviates from the printf function in C where truncation is done from the end.</p>                                                                                                                                                                                                                                                                                           
     113                 :  *                                                                                                                                                                                                                                                                                                                                                                                            
     114                 :  * <p>Below are various format modifier examples for the category conversion specifier.</p>                                                                                                                                                                                                                                                                                                   
     115                 :  * <pre>                                                                                                                                                                                                                                                                                                                                                                                      
     116                 :  *   Format modifier  left justify  minimum width  maximum width  comment                                                                                                                                                                                                                                                                                                                     
     117                 :  *   %20c             false         20             none           Left pad with spaces if the category name                                                                                                                                                                                                                                                                                   
     118                 :  *                                                                is less than 20 characters long.                                                                                                                                                                                                                                                                                            
     119                 :  *   %-20c            true          20             none           Right pad with spaces if the category name                                                                                                                                                                                                                                                                                  
     120                 :  *                                                                is less than 20 characters long.                                                                                                                                                                                                                                                                                            
     121                 :  *   %.30c            NA            none           30             Truncate from the beginning if the category name                                                                                                                                                                                                                                                                            
     122                 :  *                                                                is longer than 30 characters.                                                                                                                                                                                                                                                                                               
     123                 :  *   %20.30c          false         20             30             Left pad with spaces if the category name                                                                                                                                                                                                                                                                                   
     124                 :  *                                                                is shorter than 20 characters.                                                                                                                                                                                                                                                                                              
     125                 :  *                                                                However, if category name is longer than 30 chars,                                                                                                                                                                                                                                                                          
     126                 :  *                                                                then truncate from the beginning.                                                                                                                                                                                                                                                                                           
     127                 :  *   %-20.30c         true          20             30             Right pad with spaces if the category name is                                                                                                                                                                                                                                                                               
     128                 :  *                                                                shorter than 20 chars.                                                                                                                                                                                                                                                                                                      
     129                 :  *                                                                However, if category name is longer than 30 chars,                                                                                                                                                                                                                                                                          
     130                 :  *                                                                then truncate from the beginning.                                                                                                                                                                                                                                                                                           
     131                 :  * </pre>                                                                                                                                                                                                                                                                                                                                                                                     
     132                 :  *                                                                                                                                                                                                                                                                                                                                                                                            
     133                 :  * @version $Revision: 1213283 $                                                                                                                                                                                                                                                                                                                                                              
     134                 :  * @package log4php                                                                                                                                                                                                                                                                                                                                                                           
     135                 :  * @subpackage layouts                                                                                                                                                                                                                                                                                                                                                                        
     136                 :  * @since 0.3                                                                                                                                                                                                                                                                                                                                                                                 
     137                 :  */                                                                                                                                                                                                                                                                                                                                                                                           
     138                 : class LoggerLayoutPattern extends LoggerLayout {                                                                                                                                                                                                                                                                                                                                              
     139                 :     /** Default conversion Pattern */                                                                                                                                                                                                                                                                                                                                                         
     140                 :     const DEFAULT_CONVERSION_PATTERN = '%m%n';                                                                                                                                                                                                                                                                                                                                                
     141                 :                                                                                                                                                                                                                                                                                                                                                                                               
     142                 :     /** Default conversion TTCC Pattern */                                                                                                                                                                                                                                                                                                                                                    
     143                 :     const TTCC_CONVERSION_PATTERN = '%d [%t] %p %c %x - %m%n';                                                                                                                                                                                                                                                                                                                                
     144                 :                                                                                                                                                                                                                                                                                                                                                                                               
     145                 :     /** The conversion pattern. */                                                                                                                                                                                                                                                                                                                                                            
     146                 :     protected $pattern = self::DEFAULT_CONVERSION_PATTERN;                                                                                                                                                                                                                                                                                                                                    
     147                 :                                                                                                                                                                                                                                                                                                                                                                                               
     148                 :     /**                                                                                                                                                                                                                                                                                                                                                                                       
     149                 :      * Head of a chain of Converters.                                                                                                                                                                                                                                                                                                                                                         
     150                 :      * @var LoggerPatternConverter                                                                                                                                                                                                                                                                                                                                                            
     151                 :      */                                                                                                                                                                                                                                                                                                                                                                                       
     152                 :     private $head;                                                                                                                                                                                                                                                                                                                                                                            
     153                 :                                                                                                                                                                                                                                                                                                                                                                                               
     154                 :     /**                                                                                                                                                                                                                                                                                                                                                                                       
     155                 :      * Set the <b>ConversionPattern</b> option. This is the string which                                                                                                                                                                                                                                                                                                                      
     156                 :      * controls formatting and consists of a mix of literal content and                                                                                                                                                                                                                                                                                                                       
     157                 :      * conversion specifiers.                                                                                                                                                                                                                                                                                                                                                                 
     158                 :      */                                                                                                                                                                                                                                                                                                                                                                                       
     159                 :     public function setConversionPattern($conversionPattern) {                                                                                                                                                                                                                                                                                                                                
     160              17 :         $this->pattern = $conversionPattern;                                                                                                                                                                                                                                                                                                                                                  
     161              17 :         $patternParser = new LoggerPatternParser($this->pattern);                                                                                                                                                                                                                                                                                                                             
     162              17 :         $this->head = $patternParser->parse();                                                                                                                                                                                                                                                                                                                                                
     163              17 :     }                                                                                                                                                                                                                                                                                                                                                                                         
     164                 :                                                                                                                                                                                                                                                                                                                                                                                               
     165                 :     /**                                                                                                                                                                                                                                                                                                                                                                                       
     166                 :      * Produces a formatted string as specified by the conversion pattern.                                                                                                                                                                                                                                                                                                                    
     167                 :      *                                                                                                                                                                                                                                                                                                                                                                                        
     168                 :      * @param LoggerLoggingEvent $event                                                                                                                                                                                                                                                                                                                                                       
     169                 :      * @return string                                                                                                                                                                                                                                                                                                                                                                         
     170                 :      */                                                                                                                                                                                                                                                                                                                                                                                       
     171                 :     public function format(LoggerLoggingEvent $event) {                                                                                                                                                                                                                                                                                                                                       
     172              14 :         $sbuf = '';                                                                                                                                                                                                                                                                                                                                                                           
     173              14 :         $c = $this->head;                                                                                                                                                                                                                                                                                                                                                                     
     174              14 :         while ($c !== null) {                                                                                                                                                                                                                                                                                                                                                                 
     175              11 :             $c->format($sbuf, $event);                                                                                                                                                                                                                                                                                                                                                        
     176              11 :             $c = $c->next;                                                                                                                                                                                                                                                                                                                                                                    
     177              11 :         }                                                                                                                                                                                                                                                                                                                                                                                     
     178              14 :         return $sbuf;                                                                                                                                                                                                                                                                                                                                                                         
     179                 :     }                                                                                                                                                                                                                                                                                                                                                                                         
     180                 :                                                                                                                                                                                                                                                                                                                                                                                               
     181                 :     /**                                                                                                                                                                                                                                                                                                                                                                                       
     182                 :      * Returns an array with the formatted elements.                                                                                                                                                                                                                                                                                                                                          
     183                 :      *                                                                                                                                                                                                                                                                                                                                                                                        
     184                 :      * This method is mainly used for the prepared statements of {@see LoggerAppenderPDO}.                                                                                                                                                                                                                                                                                                    
     185                 :      *                                                                                                                                                                                                                                                                                                                                                                                        
     186                 :      * It requires {@link $this->pattern} to be a comma separated string of patterns like                                                                                                                                                                                                                                                                                                     
     187                 :      * e.g. <code>%d,%c,%p,%m,%t,%F,%L</code>.                                                                                                                                                                                                                                                                                                                                                
     188                 :      *                                                                                                                                                                                                                                                                                                                                                                                        
     189                 :      * @return array(string)   An array of the converted elements i.e. timestamp, message, filename etc.                                                                                                                                                                                                                                                                                      
     190                 :      */                                                                                                                                                                                                                                                                                                                                                                                       
     191                 :     public function formatToArray(LoggerLoggingEvent $event) {                                                                                                                                                                                                                                                                                                                                
     192               3 :         $results = array();                                                                                                                                                                                                                                                                                                                                                                   
     193               3 :         $c = $this->head;                                                                                                                                                                                                                                                                                                                                                                     
     194               3 :         while ($c !== null) {                                                                                                                                                                                                                                                                                                                                                                 
     195               3 :             if ( ! $c instanceOf LoggerLiteralPatternConverter) {                                                                                                                                                                                                                                                                                                                             
     196               3 :                 $sbuf = null;                                                                                                                                                                                                                                                                                                                                                                 
     197               3 :                 $c->format($sbuf, $event);                                                                                                                                                                                                                                                                                                                                                    
     198               3 :                 $results[] = $sbuf;                                                                                                                                                                                                                                                                                                                                                           
     199               3 :             }                                                                                                                                                                                                                                                                                                                                                                                 
     200               3 :             $c = $c->next;                                                                                                                                                                                                                                                                                                                                                                    
     201               3 :         }                                                                                                                                                                                                                                                                                                                                                                                     
     202               3 :         return $results;                                                                                                                                                                                                                                                                                                                                                                      
     203                 :     }                                                                                                                                                                                                                                                                                                                                                                                         
     204                 :                                                                                                                                                                                                                                                                                                                                                                                               

Generated by PHP_CodeCoverage 1.1.1 using PHP 5.3.3-7+squeeze3 and PHPUnit 3.6.3 at Sat Feb 18 22:32:39 GMT 2012.