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)
- 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包中。
我们下载的包解压,将src\main下的php目录拷贝到web文档目录,并重命名为log4php。
写一个简单的测试例子:
- ?php
- include('log4php/Logger.php');
- $logger = Logger::getLogger("main");
- $logger->info("foo");
- $logger->warn("bar");
- ?>
在控制台(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如下:
- ?xml version="1.0" encoding="UTF-8"?>
- log4php:configuration xmlns:log4php="http://logging.apache.org/log4php/">
- appender name="myAppender" class="LoggerAppenderFile"> !-- 1 -->
- param name="file" value="myLog.log"/> !-- 2 -->
- /appender>
- root>
- level value="WARN" /> !-- 3 -->
- appender_ref ref="myAppender" /> !-- 4 -->
- /root>
- /log4php:configuration>
配置文件说明:
1、创建一个LoggerAppenderFile,用于日志文件输出。
2、文件参数,用于日志输出的文件配置。
3、设置root的级别为WARN,低于WARN的日志不会输出。
4、root的日志输出连接到myAppender上。
测试程序如下:
- ?php
- // Insert the path where you unpacked log4php
- include('log4php/Logger.php');
- // Tell log4php to use our configuration file.
- Logger::configure('log4php.xml');
- // Fetch a logger, it will inherit settings from the root logger
- $log = Logger::getLogger('myLogger');
- // Start logging
- $log->trace("My first message."); // Not logged because TRACE
- $log->debug("My second message."); // Not logged because DEBUG
- $log->info("My third message."); // Not logged because INFO
- $log->warn("My fourth message."); // Logged because WARN >= WARN
- $log->error("My fifth message."); // Logged because ERROR >= WARN
- $log->fatal("My sixth message."); // Logged because FATAL >= WARN
- ?>
我们看到myLog.log文件中输出如下信息:
WARN - My fourth message.
ERROR - My fifth message.
FATAL - My sixth message.
比WARN级别低的日志没有输出。
更高级一点的配置:
- ?xml version="1.0" encoding="UTF-8"?>
- log4php:configuration xmlns:log4php="http://logging.apache.org/log4php/">
-
- appender name="myConsoleAppender" class="LoggerAppenderConsole" />
-
- appender name="myFileAppender" class="LoggerAppenderFile">
- layout class="LoggerLayoutTTCC" />
- param name="file" value="myLog.log" />
- /appender>
- logger name="Foo">
- appender_ref ref="myFileAppender" />
- /logger>
-
- root>
- level value="DEBUG" />
- appender_ref ref="myConsoleAppender" />
- /root>
- /log4php:configuration>
在这个配置文件中,
named loggers using layouts best practices in object-oriented programming通过测试文件如下:
- ?php
- include('log4php/Logger.php');
- Logger::configure('D:\log4php.xml');
- /**
- * This is a classic pattern: using one logger object per class.
- */
- class Foo
- {
- /** Holds the Logger. */
- private $log;
- /** Logger is instantiated in the constructor. */
- public function __construct()
- {
- // The __CLASS__ constant holds the class name, in our case "Foo".
- // Therefore this creates a logger named "Foo" (which we configured in the config file)
- $this->log = Logger::getLogger(__CLASS__);
- }
- /** Logger can be used from any member method. */
- public function go()
- {
- $this->log->info("We have liftoff.");
- }
- }
- $foo = new Foo();
- $foo->go();
- ?>
下面的信息将会输出在控制台:
INFO - We have liftoff.
下面的信息将输出在myLog.log文件中:
注意文件输出的不同,因为在文件appender中使用了LoggerLayoutTTCC这个Layout。
PS:在Linux下要注意输出日志的目录,apache用户有权限写入。
还可以采用properties的形式配置,或者php文件。更多的资料可以参照apache官方网站。http://logging.apache.org/log4php/docs/introduction.html