最近在整理项目中的日志问题,查了一些关于 “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. 黑盒测试实践--Day1 11.25

    黑盒测试实践--Day1 今天完成任务情况: 晚上得到老师布置的本周小组作业--黑盒测试的基本要求,然后小组在上周作业建立的微信群里开了个在线的短会,主要内容如下: 组长小靳带领大家学习了这个要求 计 ...

  2. Linux下的strerror是否线程安全?

    下列是glibc-2.14中的源代码: 点击(此处)折叠或打开 char * strerror (errnum) int errnum; { char *ret = __strerror_r (err ...

  3. Deep Visual-Semantic Alignments for Generating Image Descriptions(深度视觉-语义对应对于生成图像描述)

    https://cs.stanford.edu/people/karpathy/deepimagesent/ Abstract We present a model that generates na ...

  4. Android getDimension,getDimensionPixelOffset,getDimensionPixelSize

    1.例如在onMeasure(int , int)方法中可能要获取自定义属性的值.如: TypedArray a = context.obtainStyledAttributes(attrs, R.s ...

  5. Snapshot--使用脚本创建快照

    USE master; SET NOCOUNT ON; GO ); --数据库名 );--快照名 );--保存路径 SET @dbname='DB1'; SET @snapname='DB1_SNAP ...

  6. 打开Word 2010 老提示安装 Office single image 2010

    解决办法: WScript.Echo "Try to repair registry key..."  'verify Office version  Set objshell = ...

  7. 分别实现数组所有元素相加、相乘、相与——FP 风格

    var ops = { "plus": (x,y)=>x+y, "mul" : (x,y)=>x*y, "and" : (x,y ...

  8. C# 在Winform设计一个耗时较久的任务在后台执行时的状态提示窗口

    很多时候,我们需要在窗体中执行一些耗时比较久的任务.比如:循环处理某些文件,发送某些消息等... 单纯的依靠状态栏,用户体验不佳,按下功能按钮后得不到有效的提醒,小白用户绝对会电话给你说“我点了以后就 ...

  9. luoguP2387 [NOI2014]魔法森林

    https://www.luogu.org/problemnew/show/P2387 考虑先将所有边按 a 值排序,依次加入每一条边,如果这条边的两个端点 ( l, r ) 之间的简单路径中 b 的 ...

  10. SHELL编程之条件判断

    一.if 语句结构 (1)单分支语句结构 if  条件测试操作 then  命令序列 fi #!/bin/bash MOUNT_DIR="/media/cdrom/" #-d $M ...