log4php和log4j比较类似,主要介绍下配置
1.下载地址
http://logging.apache.org/log4php/index.html
2.
使用说明
使用src/main/php 部分既可
3.简单实用
我在src/main/下面建了二个新路径,一个存放config.xml,一个存放测试php文件
如下:
4.新建一个config_advenced.xml(和log4j配置很类似,不明白的可以下载文件自带的doc)
class="xml">
<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://logging.apache.org/log4php/">
<appender name="myConsoleAppender" class="LoggerAppenderConsole">
<layout class="LoggerLayoutPattern">
<param name="conversionPattern" value="%d{y-M-d H:m:s} [%level] [%logger] %message%newline" />
</layout>
</appender>
<appender name="myFileAppender" class="LoggerAppenderRollingFile">
<param name="file" value="C:/data1/applog/log4php.log" />
<param name="append" value="true" />
<param name="maxFileSize" value="1KB" />
<param name="maxBackupIndex" value="3" />
<layout class="LoggerLayoutPattern">
<param name="conversionPattern" value="%d{y-M-d H:m:s} [%level] [%logger] %message%newline" />
</layout>
<param name="threshold" value="info" />
</appender>
<root>
<level value="DEBUG" />
<appender_ref ref="myConsoleAppender" />
<appender_ref ref="myFileAppender" />
</root>
</configuration>
5.新建测试文件
<?php
include '../php/Logger.php';
Logger::configure('../resources/config_advanced.xml');
class Advanced
{
private $log;
public function __construct()
{
$this->log = Logger::getLogger(__CLASS__);
}
public function go()
{
for ($x=0; $x<=100; $x++)
{
if ( $x%2 == 0) {
$this->log->info("It's info logger :"+ $x);
} else {
$this->log->debug("It's debugger logger :"+ $x);
}
}
}
}
$advenced = new Advanced();
$advenced->go();
?>
6.输出结果
控制台:
....
14-Nov-12 14:11:10 [DEBUG] [Advanced] 89
14-Nov-12 14:11:10 [INFO] [Advanced] 90
14-Nov-12 14:11:10 [DEBUG] [Advanced] 91
....
文件输出:
在你本机的C:/data1/applog/log4php.log 会有LOG文件生成
最后:用起来挺爽的
- 大小: 43.3 KB