CodeIgniter 错误处理
很多时候,我们会在使用应用程序时遇到错误,如果错误处理不当,对用户来说是非常烦人的。 CodeIgniter 提供了一种简单的错误处理机制。
你希望在应用程序处于开发模式而不是生产模式时显示消息,因为在开发阶段可以轻松解决错误消息。
你的应用程序的环境可以被改变,通过改变index.php文件中的以下一行。这可以被设置为任何东西,但通常有三个值(开发 development、测试 test、生产 production)用于此目的。
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
不同的环境将需要不同级别的错误报告。默认情况下,开发模式将显示错误,测试和实时模式将隐藏它们。 CodeIgniter 提供了如下三个函数来处理错误。
show_error()函数在屏幕顶部以 HTML 格式显示错误。
语法 | show_error( $message, $status_code, $heading = 'An Error Was Encountered' ) |
参数 |
|
返回类型 | mixed |
show_404()如果你尝试访问不存在的页面,则函数会显示错误。
语法 | show_404( $page = '', $log_error = TRUE ) |
参数 |
|
返回类型 | void |
log_message()函数用于写入日志消息,当你想编写自定义消息时,这很有用。
语法 | 日志消息( $level, $message, $php_error = FALSE ) |
参数 |
|
返回类型 | 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。