|
下载log4php。我下载的版本是:apache-log4php-2.3.0-src.zip。借压缩,将压缩文件中的src/main/php/文件夹拷贝到CI的application/thrid_party/目录中,并将此文件夹(php),改名为log4php。
在log4php文件夹中建立log4php的配置文件,文件名为:log4php.properties。此配置文件内容如下:
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 */ 运行后,显示如下的信息: | |
|
LOG4的确不错哈!避免自己又去写日志系统
| |