CI 框架中的日志处理 以及 404异常处理

时间:2023-02-21 00:23:31

最近在整理项目中的日志问题,查了一些关于 “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. 读取Excel文件

    绝对路径String filepath= "E:\\a.xls"; public static String getExcelData(File file,String lang, ...

  2. EnumMap

    以下内容基于jdk1.7.0_79源码: 什么是EnumMap Map接口的实现,其key-value映射中的key是Enum类型: 补充说明 其原理就是一个对象数组,数组的下标索引就是根据Map中的 ...

  3. 在linux下运行apt-get update 时,报错/var/lib/apt/lists/lock

    在运行apt-get update 时,报下面的错误: E: 无法获得锁 /var/lib/apt/lists/lock - open (11: Resource temporarily unavai ...

  4. javascript优化--03高质量编码

    使用Object的直接量实例构造轻量级的字典: 使用for/in循环,使用对象字面量来构建,并确保不增加Object.prototype属性来导致for/in循环:(考虑到兼容性,如Array.pro ...

  5. Java利用Math.random()方法随机生成A-Z的字符

    package reverse; import java.text.DecimalFormat; public class Reverse { public static void main(Stri ...

  6. C# 使用AutoResetEvent进行线程同步

    AutoResetEvent 允许线程通过发信号互相通信. 通常,当线程需要独占访问资源时使用该类. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号. 如果 AutoRe ...

  7. 201521123004 《Java程序设计》第 14 周学习总结

    0. 本周课程设计发布 Java课程设计 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 1.思维导图如下: 2.补充: 数据库 为了实现一定目的按某种规则组织 ...

  8. 基于 Redis 的分布式锁

    前言 分布式锁在分布式应用中应用广泛,想要搞懂一个新事物首先得了解它的由来,这样才能更加的理解甚至可以举一反三. 首先谈到分布式锁自然也就联想到分布式应用. 在我们将应用拆分为分布式应用之前的单机系统 ...

  9. 远程连接mysql8.0,Error No.2058 Plugin caching_sha2_password could not be loaded

    通过本地去连接远程的mysql时报错,原因时mysql8.0的加密方法变了. mysql8.0默认采用caching_sha2_password的加密方式 第三方客户端基本都不支持这种加密方式,只有自 ...

  10. pygame将文字保存为图片形式

    近期自学了点小基础,分享一下用pygame制作字体图片的方法:   # 将文字保存为图片形式 import pygame import sys pygame.init()   导入字体包,也可以调用系 ...