1

I was wondering if anybody knows of a way to alter the default way an error message gets logged in PHP. Hopefully this can be applied to ALL messages, fatal errors, warnings, etc. as well as any time my script calls upon the error_log.

Currently the default error messages show up in the log as

[19-Feb-2017 15:38:42 America/Vancouver] Could not post employee data - no rows submitted
[20-Feb-2017 11:12:34 America/Toronto] PHP Warning:  array_splice() expects parameter ...

But what would be greatly beneficial to me is if I could have it also include some variables that i have stored in $_SESSION for each user, such as the user's login and company. So for example the error could be output like the following

[19-Feb-2017 15:38:42 America/Vancouver] [CompanyXYZ/bob_smith] Could not post employee data - no rows submitted
[20-Feb-2017 11:12:34 America/Toronto] [OtherCompany/jimbo_redneck] PHP Warning:  array_splice() expects parameter ...

Does anything like this exist in the world of PHP?

2
  • 1
    Where does Could not post employee data - no rows submitted come from? That's not a PHP error. Wherever that is generated, add the other variables there. Commented Feb 23, 2017 at 21:52
  • Sorry I should have clarified that error message, that is my own error message from using error_log. My project is littered with them everywhere, I was hoping php would have a way to modify both the default error messages and my own without having to rewrite every error_log instance. Commented Feb 23, 2017 at 22:19

1 Answer 1

1

You can create your own error handling routine. You should be able to add whatever data you want to the error message. Something like this should get your started:

<?php
function my_handler($errno , $errstr, $errfile, $errline, $errcontext)
{
    $msg = "Hey I got an error of type $errno ($errstr) from $errfile line $errline!";
    $msg .= "Here's some more info: $_SESSION[foo]";
    error_log($msg);
    return true;
}
set_error_handler("my_handler");

Fatal errors are only caused by code problems such as syntax errors. They will not happen to users, so you don't need to worry about catching them.

Note that things change considerably in PHP 7.

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

2 Comments

Hey thanks, I think I completely overlooked that function! If I understand correctly, after implementing a custom error handler, should I be using trigger_error("Oh no an error in ...") instead of error_log("Oh no an error in ...") in my project to have it not bypass the custom handler?
error_log() simply writes directly to the log file, so yes trigger_error() would be what you're looking for here, if you want to log an "error" that PHP doesn't consider an error, and have it go through your custom handler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.