CodeIgniter教程

CodeIgniter 错误处理

很多时候,在使用应用程序时,我们会遇到错误。如果错误处理不当,对用户来说是非常烦人的。 CodeIgniter 提供了一种简单的错误处理机制。
您希望在应用程序处于开发模式而不是生产模式时显示消息,因为错误消息可以在开发阶段轻松解决。
通过更改 index.php 文件中下面给出的行,可以更改应用程序的环境。这可以设置为任何值,但通常有三个值(开发、测试、生产)用于此目的。
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
不同的环境将需要不同级别的错误报告。默认情况下,开发模式将显示错误,测试和实时模式将隐藏它们。 CodeIgniter 提供了如下三个函数来处理错误。
show_error() 函数在屏幕顶部以 HTML 格式显示错误。
语法
show_error($message, $status_code, $heading = '遇到错误')
Parameters
$message (mixed)-错误信息 $status_code (int)-HTTP 响应状态代码 $heading (string)-错误页面标题
Return Type
mixed
show_404() 如果您尝试访问不存在的页面,则函数会显示错误。
语法
show_404($page = '', $log_error = TRUE)
Parameters
$page (string) – URI 字符串 $log_error (bool) – 是否记录错误
Return Type
void
log_message() 函数用于写入日志消息。当您想编写自定义消息时,这很有用。
语法
log_message($level, $message, $php_error = FALSE)
Parameters
$level (string)-日志级别:"错误"、"调试"或"信息" $message (string)-要记录的消息 $php_error (bool)-是否记录原生 PHP 错误消息
Return Type
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/cliapplication/views/errors/html 中找到各种错误消息模板。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4