最近在整理项目中的日志问题,查了一些关于 “CI 框架中的日志处理 以及 404异常处理” 的东西,顺便记录一下:

关于错误日志:

1. 在CI框架中的 system/core/CodeIgniter.php  中注册了处理函数(这个是PHP7之后的异常处理机制:可以使用set_error_handler来注册自己的错误处理方法来代替php的标准错误处理方式)

详见:https://blog.csdn.net/zhang197093/article/details/75094816

set_error_handler('_error_handler'); //注册error处理函数
set_exception_handler('_exception_handler'); //注册异常处理函数
register_shutdown_function('_shutdown_handler');

2. 在CI框架的index.php 中引入了init.php

3. 在init.php 中自定义error处理句柄:_error_handler

if ( ! function_exists('_error_handler'))
{
function _error_handler($level, $msg, $file, $line)
{
switch ($level)
{
case E_NOTICE:
case E_USER_NOTICE:
$error_type = 'Notice';
break; case E_WARNING:
case E_USER_WARNING:
$error_type = 'Warning';
break; case E_ERROR:
$error_type = 'Fatal Error';
break; case E_USER_ERROR:
$error_type = 'User Fatal Error';
break; default:
$error_type = 'Unknown';
break;
} if('Unknown' == $error_type)
{
return true;
} $log = & load_class('Log', 'core');
$log_info = [
'error_type=' . $error_type,
'error_msg=' . $msg,
];
$log_info_str = implode('||', $log_info);
$log->fatal(['%s', $log_info_str]); }
}

P.S.  如果是E_ERROR,E_PARSE之类的错误,是不能被用户自定义句柄自动捕获处理的

但是在PHP7 之后,可以在程序的 try-catch 中捕获然后处理,不捕获的话,仍然会按照系统默认的方式处理

关于404异常处理

1. 在CI框架的 application/core/Exception.php 中改写了system/core/Exception.php中的方法,包括show_404方法

    public function show_404($page = '', $log_error = TRUE)
{
// By default we log this, but allow a dev to skip it
if ($log_error)
{
log_message('error', $page);
} echo json_encode([
'errno' => 1,
'errmsg' => 'show404',
]); exit(4); // EXIT_UNKNOWN_FILE
}

2. show_404方法中用到的 log_message 方法在 system/core/commen.php 中,系统方法

其中调用/core/Log.php 中的 write_log  方法打印日志

if ( ! function_exists('log_message'))
{
/**
* Error Logging Interface
*
* We use this as a simple mechanism to access the logging
* class and send messages to be logged.
*
* @param string the error level: 'error', 'debug' or 'info'
* @param string the error message
* @return void
*/
function log_message($level, $message)
{
static $_log; if ($_log === NULL)
{
// references cannot be directly assigned to static variables, so we use an array
$_log[0] =& load_class('Log', 'core');
} $_log[0]->write_log($level, $message);
}
}

3. 在 application/core/log.php 中 重载了 system/core/Log.php 中的系统方法,所以2中用到的是 application/core/log.php中的 write_log

    public function write_log($level = 'error', $msg = '', $php_error = FALSE)
{
if ($this->_enabled === FALSE) {
return FALSE;
} $level = strtoupper($level); //将原日志系统映射到现有日志系统中,屏蔽原日志系统
if (isset($this->_levels[$level])) {
$this->__log__($this->_levels[$level], array($msg));
$this->flush();
return TRUE;
}
return FALSE;
}

4. 在system/core/CodeIgniter.php中调用了show_404  方法

    if ($e404)
{
if ( ! empty($RTR->routes['404_override']))
{
if (sscanf($RTR->routes['404_override'], '%[^/]/%s', $error_class, $error_method) !== 2)
{
$error_method = 'index';
} $error_class = ucfirst($error_class); if ( ! class_exists($error_class, FALSE))
{
if (file_exists(APPPATH.'controllers/'.$RTR->directory.$error_class.'.php'))
{
require_once(APPPATH.'controllers/'.$RTR->directory.$error_class.'.php');
$e404 = ! class_exists($error_class, FALSE);
}
// Were we in a directory? If so, check for a global override
elseif ( ! empty($RTR->directory) && file_exists(APPPATH.'controllers/'.$error_class.'.php'))
{
require_once(APPPATH.'controllers/'.$error_class.'.php');
if (($e404 = ! class_exists($error_class, FALSE)) === FALSE)
{
$RTR->directory = '';
}
}
}
else
{
$e404 = FALSE;
}
} // Did we reset the $e404 flag? If so, set the rsegments, starting from index 1
if ( ! $e404)
{
$class = $error_class;
$method = $error_method; $URI->rsegments = array(
1 => $class,
2 => $method
);
}
else
{
show_404($RTR->directory.$class.'/'.$method);
}
}

CI 框架中的日志处理 以及 404异常处理的更多相关文章

  1. PHP框架中的日志系统

    现在在一家公司做PHP后台开发程序猿(我们组没有前端,做活动时会做前端的东西),刚开始到公司的时候花2个周赶出了一个前端加后台的活动(记得当时做不出来周末加了两天班...),到现在过去4个多月了,可以 ...

  2. CI 框架中的自定义路由规则

    在 CI 框架中,一个 URL 和它对应的控制器中的类以及类中的方法是一一对应的,如: www.test.com/user/info/zhaoyingnan 其中 user 对应的就是控制器中的 us ...

  3. php CI框架中URL特殊字符处理与SQL注入隐患

    php CI框架中URL特殊字符处理与SQL注入隐患 php CI框架中URL特殊字符有很多是不支持的,导致像c++,括号这些常用的分类,字符都无法正常显示很头痛,而在配置里增加单引号' 反斜杠\ 这 ...

  4. php json_encode在CI框架中的使用细节

    这个错误的造成原因是加载类类库,转换成json格式的时候不熟悉CI框架的规定导致的,CI框架中规定在将数据转换成json格式的时候需要将类库小写,当然了,调用的时候必须保证有这个类库,且可以在对应的文 ...

  5. CI框架中集成CKEditor编辑器的教程

    CKEditor是在很多开发过程中都会用到的一个富文本编辑器,那么如何在CI框架中使用它呢?这里介绍了在CI下使用CKEditor的方法,版本比较低,是在CI 1.7.3下使用fckeditor 2. ...

  6. CI框架中的奇葩

    今天在win下开发,使用ci框架,本来是没有任何问题,然后转向了mac上开发,结果出现了个奇葩的问题,就是在ci框架中,控制器命名以"Admin_"为前缀的,在url中,控制器也必 ...

  7. 对CI框架中几个文件libraries

    对CI框架中几个文件libraries,helpers,hooks夹说明 来源:未知    时间:2014-10-20 11:37   阅读数:117   作者:xbdadmin [导读] 1.lib ...

  8. CodeIgniter(CI)框架中的验证码

    在CodeIgniter框架中,CI本身自带了验证码,但是查看文档的时候,发现: 需要新建一个表,用来存储验证码信息.因为习惯了session存储验证码信息,所以我把我认为比较好看的验证码应用在了CI ...

  9. CI框架中自定义view文件夹位置

    要想自定义view文件夹的位置,首先要了解CI框架时如何加载view文件夹的. CI中默认调用view的方法是: $this->load->view(); //这一行代码的原理是什么呢?请 ...

随机推荐

  1. h5存储的优点

    1.解决4k大小问题2.解决请求头常带存储信息的问题3.解决关系型存储问题4.可以跨浏览器

  2. HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)

    题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程 ...

  3. POJ3020 Antenna Placement(二分图最小路径覆盖)

    The Global Aerial Research Centre has been allotted the task of building the fifth generation of mob ...

  4. javascript高级程序设计读书笔记----函数表达式

    定义函数两种方式: 1.函数声明 function sayHi(){ alert("Hi"); } sayHi();//调用函数 2.函数表达式 var sayHi = funct ...

  5. linux 系统的ssh服务

    ssh服务由服务端软件Openssh和客户端(常见的有ssh,SecureCRT,putty,xshell)组成,ssh服务默认使用22端口提供服务,它有两个不兼容的ssh协议版本,分别是1.x和2. ...

  6. XE改变图标颜色

    放一个image,load 一张png/..图片 再放一个FillRGBEffect, 将此控价拖到image下 改变FillRGBEffect的Color,就改变了image图标上的颜色. 原图为黑 ...

  7. 微服务linux启动停止脚本

    # 停止脚本#!/bin/bash #其他服务停止脚步可以通过修改APP_MAIN参数即可 APP_MAIN=com.idoipo.infras.eureka.center.Application t ...

  8. Oracle动态执行表不可访问

    在scott 用户下,执行查询语句是出现"Oracle动态执行表不可访问" 经查,是因为用户权限不够所致,修改scott用户权限语句如下: grant select on V_$s ...

  9. [.net 多线程]ThreadPool

    CancellationTokenSource tocken = new CancellationTokenSource(); ThreadPool.QueueUserWorkItem(param = ...

  10. .Net Core .Net Core V1.0 创建MVC项目

    .Net Core V1.0 创建MVC项目 创建MVC项目有两种方式: 一.创建Web项目:(有太多没用的东西要去删太麻烦) 2.项目目录结构: 此种方法要注意的是,会创建好多个json文件,下面就 ...