星期四, 8月 25, 2011

log4php

Quick Start
  • Install
    1. download Apache log4php
    2. copy
      將src/main/php/搬到目的地library/log4php
  • logging
    include('library/log4php/Logger.php');
    $logger = Logger::getLogger("main");
    $logger->info("foo");
    $logger->warn("bar");
    
    This produces the following output:
    Sun Jul 26 01:40:23 2009,021 [10093] INFO main - foo
    Sun Jul 26 01:40:23 2009,030 [10093] WARN main - bar
    
  • 設定配置
    //在getLogger前讀
    Logger::configure('config.xml');
    $logger = Logger::getLogger("main");
    
    config.xml
    
            
            
            
    
            
            
                            
            
                    
                     
            
    
    
進階設定
  • appender
    log的記錄可寫到不同的地方,如console, file, db,這就靠appender裡的設定來區分,而appender可以設定不同的class,以寫到不同的類型,以下列出常用的appender classes
  • logger
    logger即為記錄器,透過logger記錄資訊,logger設定提供過濾等級(debug, warn或error),及配對appender以便寫到不同的媒體(console, file或db)裡
    • Logger threshold
      設定info以上全記錄
      <configuration xmlns="http://logging.apache.org/log4php/">
          <appender class="LoggerAppenderConsole" name="default">
          
              
              
          
        </appender>
      </configuration>
      
    • Configuring loggers
      範例中每次要換appender,就得改xml設定
      可透過取得不同logger,即可得不同的appender
       
               
      
      
      $logger = Logger::getLogger("fileAppender"); //即可取得 myFileAppender
      
  • Filters
    提供比logger設定裡更精確的過濾條件
  • Renderers
    要記錄object時,log4php會透過default renderer預設將所有屬性印出,如果要客製化的話
    1. 先寫render
      例如$person
      /** All object renderers must implement the LoggerRendererObject interface. */
      class PersonRenderer implements LoggerRendererObject {
          public function render($person) {
              return "{$person->firstName} {$person->lastName} ({$person->age})";
          }
      }
    2. 設定檔
      Person對應PersonRenderer
      <configuration xmlns="http://logging.apache.org/log4php/">
          <renderer renderedClass="Person" renderingClass="PersonRenderer" />
          <appender name="defualt" class="LoggerAppenderEcho" />
          <root>
              <appender_ref ref="defualt" />
          </root>
      </configuration>

  • Configuration
    設定檔可用XML, properties(ini) or PHP files來設定,擇一即可
    • 利用properties(ini)設定default的appender
      log4php.appender.default = LoggerAppenderDailyFile
      log4php.appender.default.layout = LoggerLayoutPattern
      log4php.appender.default.layout.ConversionPattern = "%d{ISO8601} [%p] %c: %m (at %F line %L)%n"
      log4php.appender.default.datePattern = Ymd
      log4php.appender.default.file = logs/behavior/%s.log
    • 利用php設定default2的appender
      //config.php
      return array(
          'rootLogger' => array(
              'appenders' => array('default2'),
          ),
          'appenders' => array(
              'default2' => array(
              'class' => 'LoggerAppenderEcho',
              'layout' => array(
                'class' => 'LoggerLayoutSimple'
              )
           )
          )
      ));
      
      //include cofigure file
      Logger::configure('config.php');
      
  • Pattern Layout Parameters
    %d 時間
    %p log的level( info, error , ...)
    $c appender的名稱
    %m message
    %F filename
    %L 行數
    %n 斷行
references

沒有留言: