CI中使用log4php调试程序

下载log4php。我下载的版本是:apache-log4php-2.3.0-src.zip。借压缩,将压缩文件中的src/main/php/文件夹拷贝到CI的application/thrid_party/目录中,并将此文件夹(php),改名为log4php。

在log4php文件夹中建立log4php的配置文件,文件名为:log4php.properties。此配置文件内容如下:

log4php.rootLogger=DEBUG, A1

#输出到页面
log4php.appender.A1 = LoggerAppenderEcho
log4php.appender.A1.layout = LoggerLayoutHtml

#输出到文件
log4php.appender.A2 = LoggerAppenderDailyFile
log4php.appender.A2.layout = LoggerLayoutPattern
log4php.appender.A2.layout.ConversionPattern = "%d{ISO8601} [%p] %c: %m (at %F line %L)%n"
log4php.appender.A2.datePattern = Ymd
log4php.appender.A2.file = logs/errorLog_%s.log
log4php的信息会显示在页面上。

打开根目录下的index.php文件,在文件中添加入下代码:

// 载入Log4php
define('LOG4PHP_DIR', APPPATH.'third_party/log4php/');
require_once LOG4PHP_DIR.'Logger.php';
Logger::configure(LOG4PHP_DIR.'log4php.properties');
 
/*
 * --------------------------------------------------------------------
 * LOAD THE BOOTSTRAP FILE
 * --------------------------------------------------------------------
 *
 * And away we go...
 *
 */
require_once BASEPATH.'core/CodeIgniter.php';
在需要调试的文件中,如/application/controllers/index/test.php文件中:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test extends CI_Router {
    // 定义私有日志变量log
    private $log;
    
    /**
     * Constructor
     * Runs the route mapping function.
     */
    public function __construct() {
        parent::__construct();
        // 生成log
        $this->log = Logger::getLogger(__CLASS__);
        // 使用log
        $this->log->debug('Class Initialized');
    }
	function index(){
		$a = 'aaa';
		$this->log->info('index_test:'.$a);
		die('end');
	}
}



posted @ 2014-10-28 07:44  moqiang02  阅读(345)  评论(0编辑  收藏  举报