[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/logger/layout/ -> patternlayout.class.php (source)

   1  <?php
   2  
   3      lt_include( PLOG_CLASS_PATH."class/logger/layout/layout.class.php" );
   4  
   5  
   6      /**
   7       * PatternLayout allows a completely customizable layout that uses a conversion
   8       * pattern for formatting.
   9       *
  10       * \ingroup logger
  11       */
  12      class PatternLayout extends Layout
  13      {
  14          /**
  15           * The message to be formatted.
  16           */
  17          var $message;
  18  
  19          /**
  20           * The conversion pattern to use with this layout.
  21           */
  22          var $pattern;
  23  
  24          /**
  25           * Create a new PatternLayout instance.
  26           */
  27          function PatternLayout ($pattern)
  28          {
  29              parent::Layout( $pattern );
  30              
  31              $this->pattern = $pattern;
  32          }
  33          
  34          /**
  35           * @private
  36           * Returns a number given a priority string
  37           */
  38  		function getPriorityNumber( $prio )
  39          {
  40              $value = 0;
  41          
  42              switch( $prio ) {
  43                  case LOGGER_PRIO_INFO: $value = 1; break;
  44                  case LOGGER_PRIO_WARN: $value = 2; break;
  45                  case LOGGER_PRIO_ERROR: $value = 3; break;
  46                  case LOGGER_PRIO_DEBUG: $value = 4; break;                                                
  47              }
  48              
  49              return( $value );
  50          }
  51          
  52          /**
  53           * @static
  54           * @returns returns an array containing information about the stack, or an empty array
  55           * if such information is not available.
  56           * per line
  57           */    
  58  		function getStackTrace()
  59          {
  60              if( function_exists("debug_backtrace"))
  61                  return( debug_backtrace());
  62              else
  63                  return( Array());
  64          }
  65          
  66          /**
  67           * @static
  68           * @returns returns an string containing a full stack trace, one step of the stack
  69           * per line
  70           */
  71  		function printableStackTrace()
  72          {
  73              $info = PatternLayout::getStackTrace();
  74              $result = "";
  75              foreach( $info as $trace ) {
  76                  if( !isset($trace["file"])) $trace["file"] = "not available";
  77                  if( ($trace["function"] != "printStackTrace") && ($trace["file"] != __FILE__ )) {
  78                      isset($trace["file"]) ? $result .= $trace["file"] : $result .= "not available";
  79                      isset($trace["line"]) ? $result .= "(".$trace["line"]."): " : $result .= "(not available): ";
  80                      if( isset( $trace["class"] )) {
  81                          if( $trace["class"] != "" )
  82                              $result .= $trace["class"].".";
  83                      }
  84                      $result .= $trace["function"];
  85                      $result .= "\n";
  86                  }
  87              }            
  88              return( $result );
  89          }
  90  
  91          /**
  92           * Format a log message.
  93           *
  94           *
  95           * <b>Conversion characters:</b>
  96           *
  97           * <ul>
  98           *     <li><b>%c</b>               - the class where message was logged</li>
  99           *     <li><b>%d</b>               - current date</li>
 100           *     <li><b>%f</b>               - the file where the message was logged</li>
 101           *     <li><b>%F</b>               - the function where the message was
 102           *                                   logged</li>
 103           *     <li><b>%l</b>               - the line where the message was logged</li>
 104           *     <li><b>%m</b>               - the log message</li>
 105           *     <li><b>%n</b>               - a newline</li>
 106           *     <li><b>%N</b>               - the level name</li>
 107           *     <li><b>%p</b>               - the level of priority</li>
 108           *     <li><b>%r</b>               - a carriage return</li>
 109           *     <li><b>%t</b>               - a horizontal tab</li>
 110           *     <li><b>%T</b>               - a unix timestamp (seconds since January
 111           *                                   1st, 1970)</li>
 112           *     <li><b>%S</b>               - the full stack trace, if available</li>
 113           * </ul>
 114           *
 115           * @param Message A Message instance.
 116           *
 117           * @return string A formatted log message.
 118           */
 119          function format (&$message)
 120          {        
 121              $pattern = str_replace( "%c", $message->class, $this->pattern );
 122              $pattern = str_replace( "%d", strftime("%d-%m-%Y %H:%M:%S", time()), $pattern );
 123              $pattern = str_replace( "%f", $message->file, $pattern );
 124              $pattern = str_replace( "%F", $message->function, $pattern );
 125              $pattern = str_replace( "%l", $message->line, $pattern );
 126              $pattern = str_replace( "%m", $message->getMessage(), $pattern );
 127              $pattern = str_replace( "%n", "\n", $pattern );
 128              $pattern = str_replace( "%N", strtoupper( $message->getParameter("prio")), $pattern );
 129              $pattern = str_replace( "%p", strtoupper( $this->getPriorityNumber($message->getParameter("prio"))), $pattern );
 130              $pattern = str_replace( "%r", "\r", $pattern );
 131              $pattern = str_replace( "%t", "\t", $pattern );
 132              $pattern = str_replace( "%T", time(), $pattern );
 133              $pattern = str_replace( "%S", PatternLayout::printableStackTrace(), $pattern );
 134              
 135              return( $pattern );
 136          }
 137      }
 138  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics