log4php使用手記

wangccsy發表於2011-07-31
   log4php是apache的logging下的一個子專案,是log4j在php下的實現。log4j是apache專案下有名的日誌記錄元件,有針對比較流行的語言的實現,log4cxx,log4j,log4net,log4php等等。
   官方對log4php的介紹如下。

Apache log4php™ is a versatile logging framework for PHP.

Feature highlights:

  • Configuration through XML, properties or PHP files
  • Various logging destinations, including:
    • Console (stdout, stderr)
    • Files (including daily and rolling files)
    • Email
    • Databases
    • Sockets
    • Syslog
  • Several built-in log message formats, including:
    • HTML
    • XML
    • User defined pattern
  • Nested (NDC) and Mapped (MDC) Diagnostic Contexts.

  2009年log4php版推出2.0.0版本後,很長一段時間沒有更新,最近更新到了2.1.0版本。下載地址在http://logging.apache.com。2.1.0版本支援了對php的pear包的支援,可以將log4php功能整合到pear包中。

   我們下載的包解壓,將srcmain下的php目錄拷貝到web文件目錄,並重新命名為log4php。

   寫一個簡單的測試例子: 

  1. ?php
  2. include(`log4php/Logger.php`);
  3. $logger = Logger::getLogger(“main”);
  4. $logger>info(“foo”);
  5. $logger>warn(“bar”);
  6. ?>

    在控制檯(windows下用cmd命令執行)會看到如下的輸出:

[quote]07/31/11 08:26:12,043 [496] INFO main – foo
07/31/11 08:26:12,047 [496] WARN main – bar[/quote]

   注意:你用瀏覽器開啟,是看不到輸出的,因為log4php的預設配置輸出是到控制檯。

   通過配置檔案配置日誌的輸出,log4php.xml如下:

  1. ?xml version=“1.0” encoding=“UTF-8”?>
  2. log4php:configuration xmlns:log4php=“http://logging.apache.org/log4php/”>
  3.     appender name=“myAppender” class=“LoggerAppenderFile”> !– 1 —>
  4.         param name=“file” value=“myLog.log”/> !– 2 —>
  5.     /appender>
  6.     root>
  7.         level value=“WARN” /> !– 3 —>
  8.         appender_ref ref=“myAppender” /> !– 4 —>
  9.     /root>
  10. /log4php:configuration>

配置檔案說明:

1、建立一個LoggerAppenderFile,用於日誌檔案輸出。

2、檔案引數,用於日誌輸出的檔案配置。

3、設定root的級別為WARN,低於WARN的日誌不會輸出。

4、root的日誌輸出連線到myAppender上。

測試程式如下:

  1. ?php
  2.     // Insert the path where you unpacked log4php
  3.     include(`log4php/Logger.php`);
  4.     // Tell log4php to use our configuration file.
  5.     Logger::configure(`log4php.xml`);
  6.     // Fetch a logger, it will inherit settings from the root logger
  7.     $log = Logger::getLogger(`myLogger`);
  8.     // Start logging
  9.     $log>trace(“My first message.”); // Not logged because TRACE
  10.     $log>debug(“My second message.”); // Not logged because DEBUG
  11.     $log>info(“My third message.”); // Not logged because INFO
  12.     $log>warn(“My fourth message.”); // Logged because WARN >= WARN
  13.     $log>error(“My fifth message.”); // Logged because ERROR >= WARN
  14.     $log>fatal(“My sixth message.”); // Logged because FATAL >= WARN
  15. ?>

我們看到myLog.log檔案中輸出如下資訊:

WARN – My fourth message.
ERROR – My fifth message.
FATAL – My sixth message.

比WARN級別低的日誌沒有輸出。

更高階一點的配置:

  1. ?xml version=“1.0” encoding=“UTF-8”?>
  2. log4php:configuration xmlns:log4php=“http://logging.apache.org/log4php/”>
  3.     
  4.     appender name=“myConsoleAppender” class=“LoggerAppenderConsole” />
  5.     
  6.     appender name=“myFileAppender” class=“LoggerAppenderFile”>
  7.                 layout class=“LoggerLayoutTTCC” />
  8.         param name=“file” value=“myLog.log” />
  9.     /appender>
  10.     logger name=“Foo”>
  11.         appender_ref ref=“myFileAppender” />
  12.     /logger>
  13.     
  14.     root>
  15.         level value=“DEBUG” />
  16.         appender_ref ref=“myConsoleAppender” />
  17.     /root>
  18. /log4php:configuration>

在這個配置檔案中,

named loggers

using layouts

best practices in object-oriented programming

通過測試檔案如下:

  1. ?php
  2.     include(`log4php/Logger.php`);
  3. Logger::configure(`D:log4php.xml`);
  4. /**
  5.  * This is a classic pattern: using one logger object per class.
  6.  */
  7. class Foo
  8. {
  9.     /** Holds the Logger. */
  10.     private $log;
  11.     /** Logger is instantiated in the constructor. */
  12.     public function __construct()
  13.     {
  14.         // The __CLASS__ constant holds the class name, in our case “Foo”.
  15.         // Therefore this creates a logger named “Foo” (which we configured in the config file)
  16.         $this>log = Logger::getLogger(__CLASS__);
  17.     }
  18.     /** Logger can be used from any member method. */
  19.     public function go()
  20.     {
  21.         $this>log>info(“We have liftoff.”);
  22.     }
  23. }
  24. $foo = new Foo();
  25. $foo>go();
  26. ?>

下面的資訊將會輸出在控制檯:

INFO – We have liftoff.

下面的資訊將輸出在myLog.log檔案中:

01/06/11 18:43:39,545 [5428] INFO Foo – We have liftoff.

注意檔案輸出的不同,因為在檔案appender中使用了LoggerLayoutTTCC這個Layout。

PS:在Linux下要注意輸出日誌的目錄,apache使用者有許可權寫入。

還可以採用properties的形式配置,或者php檔案。更多的資料可以參照apache官方網站。http://logging.apache.org/log4php/docs/introduction.html


相關文章