在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.loglog4php的信息会显示在页面上。
打开根目录下的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'); } }