log4php使用

官网:http://logging.apache.org/log4php/ ,下载src包,php不存在bin包

以2.1为例,查看install文件,安装示例:

将src/main/php中文件放在目标位置即可。

使用:

require_once dirname(__FILE__) . '/../../log4php21/Logger.php';

Logger::configure(dirname(__FILE__) . '/log.properties');

global $log;
$log = Logger :: getLogger('LogAll');

这其中使用的是加载配置文件的方法:

log.properties示例:

log4php.rootLogger=debug,R

log4php.appender.stdout=LoggerAppenderConsole
log4php.appender.stdout.layout=LoggerLayoutPattern

# Pattern to output the caller's file name and line number.
log4php.appender.stdout.layout.ConversionPattern="%5p [%t] (%F:%L) - %m%n"

log4php.appender.R=LoggerAppenderRollingFile
log4php.appender.R.File=log.log

log4php.appender.R.MaxFileSize=10240KB
# Keep one backup file
log4php.appender.R.MaxBackupIndex=1

log4php.appender.R.layout=LoggerLayoutPattern
log4php.appender.R.layout.ConversionPattern="%d{ISO8601} [%p]: %m %n"

上例使用了文件日志,目录相对于调用初始化方法的php文件所在地,所以还是建议以php配置的方式来固定log位置。

<?php
Logger :: configure(dirname(__FILE__) . '/log-config.php', '

LoggerConfiguratorPhp');
?>

<?php
return array (
	'threshold' => 'ALL',
	'rootLogger' => array (
		'level' => 'debug',
		'appenders' => array (
			'R'
		)
	),
	'appenders' => array (
		'R' => array (
			'File'=>

dirname(__FILE__) . '/llllog.log',
'MaxFileSize' => '10240KB',
'MaxBackupIndex' => '2',
'class' => 'LoggerAppenderRollingFile',
'layout' =>
array (
'class' => 'LoggerLayoutPattern',
'conversionPattern' => "%d{ISO8601} [%p]: %m %n"
)
)
)
);
?>

这样就ok了.

另外可使用php来配置,参见下载包中apidocs文档或http://logging.apache.org/log4php/apidocs/index.html:

LoggerConfiguratorPhp

输出格式参见LoggerLayoutPattern


Total views.

© 2013 - 2018. All rights reserved.

Powered by Hydejack v6.6.1