1

I'm wondering if it's possible to change the default behaviour of PHP's error_log() functionality to include the source and originating line number by default.

I am guessing this could be potentially be done either with a PHP ini setting or by configuring apache in some specific way, though not being an apache guru I'm not sure how this would be made possible.

In my default environment, finding the source of the errors in a log generally doesn't pose too much of a problem where the error is legitimately spawned by a PHP warning or notice, as it will report automatically where the line originates... however, calls made manually by programmers to PHP error_log() don't do this, and I can't find a way to make this behavior default.

I am aware that generally speaking you can achieve the line reporting manually with the magic constant like this:

error_log("Failed to login to MySQL ".__LINE__);

However, I am curious and open to suggestions about if there are any ways to perhaps configure the way errors are reported universally in the log or anything else to get around changing every single call in code to include the magic constant.

2
  • Try to use log4php. Commented Oct 31, 2013 at 7:49
  • Never tried this, but if you don't want to change existing code you might try to override the build in error_log function: php.net/manual/de/function.override-function.php (and maybe use error_get_last as parameter to automatically get an error message). Commented Oct 31, 2013 at 8:01

1 Answer 1

2

You can define your own error handler using the set_error_handler function. This allows you to do with the errors whatever you like, as long as it is within the scope of PHP language (including printing the file and line where the error occured). Most common course of action in this case is to convert the error into a ErrorException.

Also you can use tools like xDebug, which, when activated in php.ini, display the errors in more readable form.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.