CodeIgniter - 错误处理

CodeIgniter - 错误处理 首页 / Codeigniter入门教程 / CodeIgniter - 错误处理

很多时候,在使用应用程序时会遇到错误。 CodeIgniter提供了一种简单的错误处理机制。

通过更改 index.php 文件下面给出的行,可以更改应用程序的环境。

define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');

不同的环境将需要不同级别的错误报告。默认情况下开发模式将显示错误,而测试和实时模式将隐藏错误。 CodeIgniter提供以下三个函数来处理错误。

  • show_error()函数在屏幕顶部以HTML格式显示错误。

语法

链接:https://www.learnfk.comhttps://www.learnfk.com/codeigniter/codeigniter-error-handling.html

来源:LearnFk无涯教程网

show_error($message, $status_code, $heading='An Error Was Encountered')

参数

  • $message ( mixed ) - 错误消息

  • $status_code ( int ) - HTTP响应状态代码

  • $heading ( string ) - 错误页面标题

返回类型

无涯教程网

mixed
  • show_404() 函数将显示错误。

语法

链接:https://www.learnfk.comhttps://www.learnfk.com/codeigniter/codeigniter-error-handling.html

来源:LearnFk无涯教程网

show_404($page='', $log_error=TRUE)

参数

  • $page (string)– URI字符串

  • $log_error ( bool )–是否记录错误

返回类型

无涯教程网

void
  • log_message()函数用于记录日志消息。

语法

链接:https://www.learnfk.comhttps://www.learnfk.com/codeigniter/codeigniter-error-handling.html

来源:LearnFk无涯教程网

log_message($level, $message, $php_error=FALSE)

参数

  • $level (string) - 日志级别:"error","debug"或"info"

  • $message ( string ) - 要记录的消息

  • $php_error ( bool ) - 是否记录本机PHP错误消息

返回类型

无涯教程网

void

可以在 application/config/config.php 文件中启用日志记录。下面给出的是config.php文件的代码,您可以在其中设置阈值。

/*
|--------------------------------------------------------------------------------
|   Error Logging Threshold
|--------------------------------------------------------------------------------
| You can enable error logging by setting a threshold over zero. The 
| threshold determines what gets logged. Threshold options are:
|
|   0=Disable logging, Error logging TURNED OFF
|   1=Error Message (including PHP errors)
|   2=Debug Message
|   3=Informational Messages
|   4=All Messages
|
| You can also pass an array with threshold levels to show individual error types
|
|   array(2)=Debug Message, without Error Messages
| For a live site you'll usually only enable Errors (1) to be logged otherwise 
| your log files will fill up very fast.
|
*/
$config['log_threshold']=0;

您可以在 application/log/中找到日志消息。在启用日志文件之前,请确保该目录可写。

可以在 application/views/errors/cli 或 application/views/errors/html 中找到各种错误消息模板。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

技术与商业案例解读 -〔徐飞〕

Linux性能优化实战 -〔倪朋飞〕

透视HTTP协议 -〔罗剑锋(Chrono)〕

Electron开发实战 -〔邓耀龙〕

系统性能调优必知必会 -〔陶辉〕

人人都用得上的写作课 -〔涵柏〕

大厂晋升指南 -〔李运华〕

流程型组织15讲 -〔蒋伟良〕

LangChain 实战课 -〔黄佳〕

好记忆不如烂笔头。留下您的足迹吧 :)