Codeigniter error log not including file name

2019-07-17 06:49发布

问题:

I'm trying to log errors from my codeigniter web app and I think the messages that get written to file are getting truncated. I say this because whenever the error is displayed on screen I get the file name where the error occurred whereas when I check the error logs it only says,

Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 24

Is there a way to have the error log return the file location of the error?

回答1:

No, currently there isn't a built-in way to do this in CodeIgniter.

What you can do is to extend the core CI_Log class, override its write_log() method and use debug_backtrace() to get the appropriate file name and prepend it to the message.

// application/core/MY_Log.php
class MY_Log extends CI_Log {

    public function write_log($level, $msg)
    {
        foreach (debug_backtrace() as $call)
        {
            // Somehow find the appropriate call here ...
            if ( ! (/* condition to ignore error-handling calls */))
            {
                break;
            }

            $msg = '['.$call['file'].'] '.$msg;
            break;
        }

        return parent::write_log($level, $msg);
    }

}