[英]Codeigniter how to get DB errors using hooks and save in logs table?
I need to save all PHP and Database errors in log table in codeigniter.我需要在 codeigniter 的日志表中保存所有 PHP 和数据库错误。 For this i am trying to do this using hooks in codeigniter.为此,我尝试使用 codeigniter 中的钩子来执行此操作。 This is my code.这是我的代码。
hooks.php钩子.php
$hook['post_controller'] = array(
'class' => 'Db_log',
'function' => 'logQueries',
'filename' => 'db_log.php',
'filepath' => 'hooks'
);
Codeigniter how to get DB errors using hooks and save in logs table Codeigniter 如何使用钩子获取数据库错误并保存在日志表中
I have created file db_log.php in hooks directory.我在 hooks 目录中创建了文件 db_log.php。 and this is my code.这是我的代码。
<?php
class Db_log {
function __construct() {
}
function logQueries() {
$CI = & get_instance();
$times = $CI->db->query_times;
$error = $CI->db->error();
print_r($error);
// foreach ($CI->db->queries as $key => $query)
// {
// $sql = $query . " \n Execution Time:" . $times[$key];
// }
}
}
It's working to show all queries but not working when any error comes related to Database.它正在显示所有查询,但在与数据库相关的任何错误时不起作用。 I had tried $CI->db->error() also to get the error but it's not working.我也尝试过 $CI->db->error() 来获取错误,但它不起作用。 I need to show custom message if any error come related to Database and need to save a entry in custom log table.如果有任何与数据库相关的错误并且需要在自定义日志表中保存一个条目,我需要显示自定义消息。
Try with shutdown_function尝试使用 shutdown_function
public function setHandler() {
register_shutdown_function('handleShutdown');
}
}
function handleShutdown() {
print_r(error_get_last());
if (($error = error_get_last())) {
die("nnnnnnnnnnnnn");
ob_start();
echo "<pre>";
var_dump($error);
echo "</pre>";
$message = ob_get_clean();
//sendEmail($message);
ob_start();
echo '{"status":"error","message":"Internal application error!"}';
ob_flush();
exit();
}
}
hooks.php
$hook['pre_system'][] = array(
'class' => 'PHPFatalError',
'function' => 'setHandler',
'filename' => 'PHPFatalError.php',
'filepath' => 'hooks'
);
I think it's impossible - its one of the flaws in CI < 4我认为这是不可能的 - 这是 CI < 4 的缺陷之一
The problem is in their db driver class - if you take a look at their display_error() function you'll see everytime a DB Error occurs the entire application will be stopped immediately.问题出在他们的数据库驱动程序 class - 如果您查看他们的display_error() function,您会看到每次发生数据库错误时,整个应用程序都会立即停止。
However you can try to register a shutdown function and try to handle some stuff there.但是,您可以尝试注册关闭 function并尝试在那里处理一些事情。
Another method would be to extend the DB Driver - for that purpose you've to extend the Loader.另一种方法是扩展 DB 驱动程序 - 为此您必须扩展加载程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.