您的位置:首页 > 编程语言 > PHP开发

log4php安装及使用【易懂】

2016-04-12 02:32 453 查看
一。介绍

Apache log4php™是一个通用的日志框架为PHP,可以通过xml或php文件来进行配置。可以应用在cms、crm等php系统中。
是Log4xx系列日志组件之一,是Log4j迁移到php的版本,主要用来记录日志信息,支持多种输入目的地,
包括:日志文件、日志回滚文件、数据库、日志服务器等等;同时,还支持多种输入格式。

本文只作一个入门引用,其实很简单的,详细使用请自学成才:http://logging.apache.org/log4php/quickstart.html

二。组成介绍
Loggers(记录器):记录器可以分配一个入口等级(配置文件)。所有日志记录请求与等级低于这个入口值将被忽略。
TRACE,DEBUG,INFO,WARN,ERROR,FATAL (默认为DEBUG)

LevelSeverityDescription
FATAL 致命HighestVery severe error events that will presumably lead the application to abort.(非常严重的错误可能会导致应用程序中止的事件。
ERROR错误...Error events that might still allow the application to continue running.(错误的事件仍可能使应用程序继续运行。
WARN警告...Potentially harmful situations which still allow the application to continue running.(潜在的有害的情况下,仍然允许应用程序继续运行。
INFO信息...Informational messages that highlight the progress of the application at coarse-grained level.(信息消息强调在粗粒度级别应用程序的进展。
DEBUG调试...Fine-grained informational events that are most useful to debug an application.(细粒度的信息事件最有用的调试应用程序。
TRACE运行轨迹LowestFinest-grained informational events.(最细致的获得时间信息
Appenders(输出源):日志输出是的去向,填写见$two
'LoggerAppenderFile' :A file.(一个文件)
'LoggerAppenderDailyFile', :A file (new file each day).(每天一个文件)
'LoggerAppenderEcho' :Console, using the PHP echo command.(使用PHP echo命令)
'LoggerAppenderPDO' :Database.

Layouts(布局):布局组件负责将日志事件转换为一个字符串,填写见$three
'LoggerLayoutPattern' :A flexible layout configurable via a pattern string. 灵活的布局配置通过模式字符串
'LoggerLayoutSimple' :A simple, non configurable layout. 一个简单的、非可配置的布局【不能设置显示格式】
'LoggerLayoutSerialized' :Outputs serialized objects. 输出序列化的对象。
'LoggerLayoutXml' :Outputs events as an XML document. 作为XML文档输出事件。

三。安装

1.版本:目前最新版本为 2.3.0.
2.下载:http://logging.apache.org/log4php/download.html




3.安装:将下载下来的压缩包内/src/main文件夹,放到你的项目指定位置,进行简单配置就可以进行应用。



四。使用(在index.php中操作)

1.面向过程简易使用

include ('./main/php/Logger.php');
$logger = Logger::getLogger("main");
$logger->info("This is an informational message.");
echo "<br>";
$logger->warn("I'm not feeling so good...");


2.面向对象使用

//配置方法一:PHP数组配置格式
$config = array(
'appenders' => array(
'default' => array(
//'LoggerAppenderFile','LoggerAppenderDailyFile','LoggerAppenderEcho','LoggerAppenderPDO'
'class' => 'LoggerAppenderDailyFile',//$two:Appenders(输出源)
'layout' => array(
//'LoggerLayoutPattern','LoggerLayoutSimple','LoggerLayoutSerialized','LoggerLayoutXml'
'class' => 'LoggerLayoutSimple',//$three:Layouts(布局),
),
'params' => array(
/*以下Layouts(布局)Pattern时才能用*/
// 'conversionPattern' => '%date %logger %-5level %msg%n',  //,用来自定义日志内容的格式

/*以下Layouts(布局)LoggerLayoutSimple*/
/*case:1 Appenders(输出源)Echo时 */
// 'htmlLineBreaks' => 'true',
/*case:1Appenders(输出源)Echo时 */

/*case:2 Appenders(输出源)DailyFile时 */
'datePattern' => 'Y-m-d',    //去掉该参数,则文件名称时间为:201600412
'file' => 'file-%s.log',     //文件名称
'append'=>true  ,             //没填默认true输出内容将追加,,若为false,文件内容将被覆盖。
/*case:2Appenders(输出源)file时*/

/*case:3 Appenders(输出源)File时 */
// 'file' => 'file.log',//文件名称
// 'append' => false//不追加
/*case:3Appenders(输出源)File时*/

/*case:4 Appenders(输出源)PDO时 */
// 'dsn' => 'mysql:host=localhost;dbname=logdb',
// 'user' => 'root',
// 'password' => 'secret',
// 'table' => 'log',
/*case:4 Appenders(输出源)PDO时*/

),

)
),
'rootLogger' => array(
'appenders' => array('default')
)
);


include ('./main/php/Logger.php');
Logger::configure($config);//PHP数组配置是开启
// Logger::configure('config.xml');//XML配置

/**
* This is a classic usage pattern: 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__);
var_dump($this->log);
}

/** Logger can be used from any member method. */
public function go()
{
/*同业执行脚本中,不能出现两次相同函数*/
$this->log->trace("<------------------>\r\n My first message.111<br>\r\n"); // Not logged because TRACE < WARN
$this->log->debug("My second message.111<br>\r\n"); // Not logged because DEBUG < WARN
$this->log->info("My third message.1111<br>\r\n"); // Not logged because INFO < WARN
$this->log->warn("My fourth message.111<br>\r\n"); // Logged because WARN >= WARN
$this->log->error("My fifth message.111<br>\r\n"); // Logged because ERROR >= WARN
$this->log->fatal("My sixth message.1111<br>\r\n<------------------>"); // Logged because FATAL >= WARN
}
}

$foo = new Foo();
$foo->go();
/*END面向对象*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: