星期二, 7月 31, 2012

log4php結合FirePHP

firephp很好用,那怎麼跟log4php結合咧
download: 下載 利用ci寫的


由於ci使用library都得用new實體化
而log4php透過static method(getLogger)取得logger
用起來總是卡卡... 應該說會卡住...
所以寫成library

  1. FirePHP appender
    簡單說,就是多寫個FirePHP的Appender
    這樣log4php就可以使用FirePHP
    class LoggerAppenderFirePHP extends LoggerAppender {
         ...
        public function append(LoggerLoggingEvent $event) {
           if($this->layout !== null) 
               return;
    
           $ci = & get_instance();
           $level = $event->getLevel();
           if($level->isGreaterOrEqual(LoggerLevel::getLevelError())) {
           $ci->fb->error($this->layout->format($event));
           } else if ($level->isGreaterOrEqual(LoggerLevel::getLevelWarn())) {
           $ci->fb->warn($this->layout->format($event));
           } else {
           $ci->fb->info($this->layout->format($event));
          }
         
       }
    }
    p.s. 因為是套用在codeigniter下... 所以有出現個get_instance() XD
  2. config
    <configuration xmlns="http://logging.apache.org/log4php/">
            <appender name="firephp" class="LoggerAppenderFirePHP">
                    <layout class="LoggerLayoutPattern">
                            <param name="ConversionPattern" value="%m"/>
                    </layout>
            </appender>
            <root>
                    <appender_ref ref="firephp" />
            </root>
    </configuration>
  3. logging
        $this->load->library('Log4php');
    
        $this->log4php->log('info',"[behavior] info");
        $this->log4php->log('error',"error");
        $this->log4php->log('warn',"warn");
    結果如下



沒有留言: