收藏本站 Archiver
    请 后使用快捷导航
    没有账号?入住 CI 中国社区
    查看: 8506|回复: 3

    [Web] CI中使用log4php调试程序

    [复制链接]
    楼主
    发表于 2013年6月6日 14:49:39 | 只看该作者 回帖奖励 |倒序浏览 |
    下载log4php。我下载的版本是:apache-log4php-2.3.0-src.zip。借压缩,将压缩文件中的src/main/php/文件夹拷贝到CI的application/thrid_party/目录中,并将此文件夹(php),改名为log4php。
    在log4php文件夹中建立log4php的配置文件,文件名为:log4php.properties。此配置文件内容如下:

    1. log4php.rootLogger=DEBUG, A1

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

    5. #输出到文件
    6. log4php.appender.A2 = LoggerAppenderDailyFile
    7. log4php.appender.A2.layout = LoggerLayoutPattern
    8. log4php.appender.A2.layout.ConversionPattern = "%d{ISO8601} [%p] %c: %m (at %F line %L)%n"
    9. log4php.appender.A2.datePattern = Ymd
    10. log4php.appender.A2.file = logs/errorLog_%s.log
    复制代码

    log4php的信息会显示在页面上。
    打开根目录下的index.php文件,在文件中添加入下代码:
    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/core/MY_Router.php文件中:
    PHP复制代码

    class MY_Router 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('core/MY_Router Class Initialized');
    }

    /**
    * Validates the supplied segments. Attempts to determine the path to the controller.
    *
    * @see CI_Router::_validate_request()
    * @access private
    * @param array
    * @return array
    */

    function _validate_request($segments) {
    if (count ($segments) == 0) {
    return $segments;
    }

    // 测试:查看路径
    $this->log->debug('segments数组的大小:'.count ($segments));
    $tempDir = array ();
    for ($i = 0; $i < count ($segments); $i++) {
    $tempDir[] = $segments[$i];
    $this->log->debug('segments['.$i.']:'.$segments[$i]);
    $this->log->debug(APPPATH.'controllers/'.implode ('/', $tempDir));
    if(!is_dir (APPPATH.'controllers/'.implode ('/', $tempDir))) {
    unset ($tempDir[count ($tempDir)-1]);
    break;
    }
    }
    $this->log->debug('segments = '.$segments[0]);

    // Does the requested controller exist in the root folder?
    if (file_exists (APPPATH.'controllers/'.$segments[0].'.php')) {
    return $segments;
    }

    // Is the controller in a sub-folder?
    if (is_dir (APPPATH.'controllers/'.$segments[0])) {
    // Set the directory and remove it from the segment array
    //$this->set_directory($segments[0]);
    //$segments = array_slice($segments, 1);

    $controllerPath = array ();
    $is = 0;
    for(; $is < count ($segments); $is++) {
    $controllerPath[] = $segments[$is];
    if(!is_dir (APPPATH.'controllers/'.implode ('/', $controllerPath))) {
    unset ($controllerPath[count ($controllerPath)-1]);
    break;
    }
    }
    $this->log->debug('传递给set_directory的参数:'.implode ('/', $controllerPath));
    $this->set_directory(implode ('/', $controllerPath));
    $segments = array_slice ($segments, $is);
    $this->log->debug($segments);
    if (count ($segments) > 0) {
    $this->log->debug(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT);
    // Does the requested controller exist in the sub-folder?
    if ( ! file_exists (APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT)) {
    $this->log->debug($this->routes['404_override']);
    if ( ! empty ($this->routes['404_override'])) {
    $x = explode ('/', $this->routes['404_override']);
    $this->log->debug('x='.$x);
    $this->set_directory('');
    $this->set_class($x[0]);
    $this->set_method(isset ($x[1]) ? $x[1] : 'index');

    return $x;
    }
    else {
    show_404($this->fetch_directory().$segments[0]);
    }
    }
    }
    else {
    // Is the method being specified in the route?
    if (strpos ($this->default_controller, '/') !== FALSE) {
    $x = explode ('/', $this->default_controller);

    $this->set_class($x[0]);
    $this->set_method($x[1]);
    }
    else {
    $this->set_class($this->default_controller);
    $this->set_method('index');
    }

    // Does the default controller exist in the sub-folder?
    if ( ! file_exists (APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php')) {
    $this->directory = '';
    return array ();
    }

    }

    return $segments;
    }


    // If we've gotten this far it means that the URI does not correlate to a valid
    // controller class. We will now see if there is an override
    if ( ! empty ($this->routes['404_override'])) {
    $x = explode ('/', $this->routes['404_override']);

    $this->set_class($x[0]);
    $this->set_method(isset ($x[1]) ? $x[1] : 'index');

    return $x;
    }


    // Nothing else to do at this point but show a 404
    show_404($segments[0]);
    }

    /**
    * Set the directory name
    *
    * @access public
    * @param string
    * @return void
    */

    function set_directory($dir) {
    // 原程序将$dir中的"/"或"."过滤掉,出于安全考虑吗?
    //$this->directory = str_replace(array('/', '.'), '', $dir).'/';
    $this->directory = $dir.'/';
    }
    }
    /* End of file MY_Routes.php */
    /* Location: ./application/liberaies/MY_Routes.php */
    复制代码


    运行后,显示如下的信息:


    沙发
    发表于 2014年7月16日 16:43:52 | 只看该作者
    LOG4的确不错哈!避免自己又去写日志系统
    藤椅
    发表于 2014年7月17日 10:25:39 | 只看该作者
    看看了
    板凳
    发表于 2014年7月17日 10:26:03 | 只看该作者
    看看啥情况
    返回列表
    您需要登录后才可以回帖 登录 | 入住 CI 中国社区

    本版积分规则

    AltStyle によって変換されたページ (->オリジナル) /