How to Log MySQL Errors in WordPress

shutdown
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//-------------------------------------------------
// Database logging - query errors
//
// email database query errors to the contact
// specified
//
// @param n/a
// @return n/a
 
function db_debug_log(){
 
    //WP already stores query errors in this obscure
    //global variable, so we can see what we've ended
    //up with just before shutdown
    global $EZSQL_ERROR;
 
    try {
        //proceed if there were MySQL errors during runtime
        if(is_array($EZSQL_ERROR) && count($EZSQL_ERROR)) {
            //build a log entry
            $xout = array();
 
            //let's start with some environmental information
            $xout[] = "DATE: " . current_time('r');
            $xout[] = "SITE: " . site_url();
            $xout[] = "IP: " . $_SERVER['REMOTE_ADDR'];
            $xout[] = "UA: " . $_SERVER['HTTP_USER_AGENT'];
            $xout[] = "SCRIPT: " . $_SERVER['SCRIPT_NAME'];
            $xout[] = "REQUEST: " . $_SERVER['REQUEST_URI'];
            $xout[] = "\n\n\n\n";
 
            //and lastly, add the error messages with some line separations for readability
            foreach($EZSQL_ERROR AS $e) {
                $xout[] = str_repeat('-', 50) . "\n" . implode("\n", $e) . "\n" . str_repeat('-', 50);
                $xout[] = "\n\n\n\n";
            }
 
            //email it!
            //if a plugin overrides the content-type header for outbound emails, change the message body
            //below to nl2br(esc_html(implode("\n", $xout)))
            wp_mail(get_bloginfo('admin_email'), '[' . get_bloginfo('name') . '] DB Error', implode(“\n”, $xout));
        }
    } catch(Exception $e){ }
 
    return;
}
add_action('shutdown', 'db_debug_log');