CI中使用log4php調試程序

下載log4php。我下載的版本是:apache-log4php-2.3.0-src.zip。借壓縮,將壓縮文件中的src/main/php/文件夾拷貝到CI的application/thrid_party/目錄中,並將此文件夾(php),更名爲log4php。

在log4php文件夾中創建log4php的配置文件,文件名爲:log4php.properties。此配置文件內容以下:php

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文件,在文件中添加入下代碼:html

// 載入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');
	}
}
相關文章
相關標籤/搜索