Quick Start
- Install
- download Apache log4php
- copy
將src/main/php/搬到目的地library/log4php
- logging
This produces the following output:include('library/log4php/Logger.php'); $logger = Logger::getLogger("main"); $logger->info("foo"); $logger->warn("bar");
Sun Jul 26 01:40:23 2009,021 [10093] INFO main - foo Sun Jul 26 01:40:23 2009,030 [10093] WARN main - bar
- 設定配置
config.xml//在getLogger前讀 Logger::configure('config.xml'); $logger = Logger::getLogger("main");
- appender
log的記錄可寫到不同的地方,如console, file, db,這就靠appender裡的設定來區分,而appender可以設定不同的class,以寫到不同的類型,以下列出常用的appender classes
- LoggerAppenderEcho 即php的echo
- LoggerAppenderFile 寫入檔案
- LoggerAppenderDailyFile依日期寫入新的檔案
- LoggerAppenderMail 透過mail將log寄出
- 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">
- Configuring loggers
範例中每次要換appender,就得改xml設定
可透過取得不同logger,即可得不同的appender
- Logger threshold
- Filters
提供比logger設定裡更精確的過濾條件 - Renderers
要記錄object時,log4php會透過default renderer預設將所有屬性印出,如果要客製化的話
- 先寫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})"; } }
- 設定檔
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>
- 先寫render
- 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');
- 利用properties(ini)設定default的appender
- Pattern Layout Parameters
%d 時間
%p log的level( info, error , ...)
$c appender的名稱
%m message
%F filename
%L 行數
%n 斷行
沒有留言:
張貼留言