Welcome Guest, Not a member yet? Register   Sign In
Logging db errrors to log file but not showing them in the browser
#1

[eluser]Jonny Noog[/eluser]
Hi,

Just trying to work out if it's possible to log database errors to the log file but at the same time not have them appear in the browser. My tests so far would seem to suggest that if you set $db['default']['db_debug'] = FALSE; in database.php, it will stop database errors from appearing in the browser as expected, but this also appears to make calls to $this->db->_error_message() and $this->db->_error_number() produce null values when I try to use these methods in conjunction with a database transaction.

Some test code:

Code:
$this->db->trans_start();
$this->db->query("INSERT INTO `users` (`foo`) VALUES ('bar')"); // Generates an unknown column error
$this->db->trans_complete();

if ($this->db->trans_status() === FALSE)
{
    $err_msg = $this->db->_error_message();
    $err_num = $this->db->_error_number();
    
    log_message('error', 'Database error: ' . $err_msg . 'Error number: ' . $err_num);
    show_error('A database error has occured.');
    exit();
}

The message 'A database error has occured.' appears in the browser but in the log file, I get:

Quote:ERROR - 2009-06-23 09:19:28 --> Database error: Error number: 0

So it appears that $err_msg and $err_num are not being set to anything meaningful.

If I set $db['default']['db_debug'] = TRUE; in database.php, I get the unknown column error logged in the log file like so:

Quote:ERROR - 2009-06-23 09:31:00 --> Query error: Unknown column 'foo' in 'field list'

But I also get the error being output to the browser.

Does anyone know of a way that I can have database errors logged to the log file but not display them in the browser?
#2

[eluser]TheFuzzy0ne[/eluser]
Disable database debugging in the config.php. I'm sure that does what you're asking.
#3

[eluser]Jonny Noog[/eluser]
Hmm, well as I mentioned in my original post, I've tried setting $db['default']['db_debug'] = FALSE; in database.php. When I do so (and then using the above test code) the output to the browser is:

Quote:An Error Was Encountered

A database error has occured.

Great, that's what I wanted. However, the output to the log file is:

Quote:ERROR - 2009-06-23 10:43:09 --> Database error: Error number: 0

Not helpful at all. The errors are not coming through.

Now if the only change I make is setting $db['default']['db_debug'] = TRUE; in database.php, output to the browser is:

Quote:A Database Error Occurred

Error Number: 1054

Unknown column 'foo' in 'field list'

INSERT INTO `users` (`foo`) VALUES ('bar')

OK that's expected.

Output to the log file is:

Quote:ERROR - 2009-06-23 10:47:21 --> Query error: Unknown column 'foo' in 'field list'

So that's not my code, that's just the standard CI error logging happening. What am I doing wrong?
#4

[eluser]Jonny Noog[/eluser]
[quote author="TheFuzzy0ne" date="1245734078"]Disable database debugging in the config.php. I'm sure that does what you're asking.[/quote]

Incidentally, I see no reference to database debugging in config.php. Only in database.php. Should there be one in config.php?
#5

[eluser]TheFuzzy0ne[/eluser]
No, I just got my file names wrong. Smile
#6

[eluser]Jonny Noog[/eluser]
Further to this issue, I have found that if I have $db['default']['db_debug'] = FALSE; in my database.php file and then run similar test code, but do not use a transaction, like:

Code:
$temp = $this->db->query("INSERT INTO `users` (`foo`) VALUES ('bar')");
if (! $temp)
{
  $err_msg = $this->db->_error_message();
  $err_num = $this->db->_error_number();
            
  log_message('error', 'Database error: ' . $err_msg . 'Error number: ' . $err_num);
  show_error('A database error has occurred.');
  exit();
}

Then everything works as I would have expected. The error gets logged to the log file and the non-descriptive message gets output to the browser. So I can only assume that the problem is to do with the code used for transactions somehow interfering with the return values of _error_message() and _error_number().




Theme © iAndrew 2016 - Forum software by © MyBB