Granular error reporting in PHP - php

I am not sure how to pose this question. Sorry if this is already asked somewhere else: I want to log PHP errors on screen and warnings and above in a log file. Is there a way in PHP itself to set this or do I need to program a class as error handler that decides where to log?

You may want to use set_error_handler(). You can catch the level of an error, therefore decide whether to show up or log to file.
FYI, php error handling

Related

Why doesn't PHP show all of the syntax errors?

This is my error setting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL|E_PARSE);
Now I write this test code:
helloooooo;
And It shows me:
Use of undefined constant helloooooo ...
So far so good.
Now I remove the semicolon from end of my so-called code:
helloooooo
Shows nothing. It shows a white page.
In my experience whenever I see a white page there is a syntax error so I look for a typo.
The question is why doesn't PHP help in this case?
Someone said :
Syntax checking is done before executing any code. So it can't execute
the ini_set() functions if there's a syntax error.
How about PHP interpreter applies settings first then executes the rest of code?
JavaScript interpreter can detect the same error in runtime.
Just try this:
<script>
helloooooo
</script>
Now go to Firefox => Tools=> WebDeveloper => WebConsole
ReferenceError: helloooooo is not defined
PHP's default error reporting mechanism (might) suppress error output. If you're trying to turn it on at runtime using the error_reporting function, then PHP will first have to successfully parse the file, then execute it to set that new desired error reporting level. That fails when there are syntax errors, because then PHP can't successfully parse and execute the file to change the error reporting level. The syntax error is getting suppressed because… well… it couldn't change the error reporting level yet.
Javascript doesn't have that, it outputs any and all errors it encounters.
The reason PHP likes to suppress errors is because its running on the server, and internal server debug details shouldn't be shown on public web pages. In Javascript that is moot, since it's running in the browser anyway, and the errors are reported to the console, which regular users don't see. Moreover, PHP does log all errors to a log file, so the same way you go looking for errors in Javascript, you could explicitly go looking for them in PHP's log file as well.

php exception handling without error_reporting on

I'm working on a legacy app where towards the beginning of most files, php error reporting is disabled using error_reporting(0). The app is mostly functioning, but if I enable the error reporting there are a lot of errors and execution stops at the first one. When error reporting is disabled, errors are not logged anywhere. If I enable error reporting, error logging works great. Is there any way to enable error reporting without stopping execution of the script, as though error reporting was disabled? I need to log the errors but I also need the app to continue functioning as is.
No.
The very definition of an error is that the program has encountered a state which it cannot recover from. If it was recoverable/avoidable/non-destructive, then it would probably be a warning.
Exceptions are different, they are catchable and you can decide what to do with them with try/catch. I see a lot of code here where people simply ignore any caught exceptions and continue on as if nothing happened, but that is usually a big mistake and the reason they had a problem that warranted an SO post. Most exceptions should be handled by cleaning up your program [complete file writes, close handles/connections/etc] before displaying an error and halting execution.
Some of this may be accomplished with set_error_handler() but execution should still be halted at the end of your custom error handler.
Make sure this is in your php.ini file:
display_errors = Off
error_log = /var/log/php/error.log
log_errors = On

Suppress stacktrace with trigger_error when logging at E_USER_NOTICE

I want to log some messages from my php application in the apache error logs. But I would like this to happen only in the non-production environments.
I found that php has error_log to log messages to the error log file but how do I control it to only log if my application is running in the non-prod environments.
Also, I found that there is trigger_error which takes a error type parameter. And the error_reporting setting in php.ini can then decide which error types are actually logged. So, while in dev, I have error_reporting set to E_ALL , it is more restricted in production. So, now I can use trigger_log and always log at E_USER_NOTICE. This would mean that my message is only shown in the dev log files but not in the production log files.
However, trigger_error also produces a stack trace with every log which is a bit unnecessary for me. How can I disable this stack trace.
Also, am I doing things the right way. How do people generally handle this problem.
Edit:
I am not trying to suppress error messages. I am trying to provide more debug messages in dev. Things like "Request came with following parameters", "step 1 done", etc. This is really not required in production.
If you want just custom messages sent into error log manually, log_error() is the function you are looking for.
Firstly, suppressing error handling is not a good idea - errors have a huge impact on your application performance - and suppressing error reporting does not eliminate the slowdown.
There is no 'trigger_log' in PHP.
The build in error handling in PHP does not generate a stack trace - you must have a custom error handler installed. You need to find it and amend the code if you want to change it's behaviour.
Using 'trigger_error' to record debug events is a very bad idea.

how to change/hide server directory name?

I have question. I have some app on facebook and getting this error
Fatal error: Uncaught OAuthException: An active access token must be used to query
information about the current user. thrown in
/home/xxx/public_html/domain/lib/base_facebook.php on line 1024
but no matter at this time.. the matter is that, is it possible to change/hide this "xxx" name? you understand? for example, instead this I would have /public_html/domain/...
OR completely hide the path ??
thanks in advance =)
The recommended action would be to disable the public display of all PHP errors when you are in production mode.
To do that, edit your php.ini file and set display_errors to 0. You can still set the error_reporting level to something suitable and all errors will be logged to the error_log file, but the actual errors themselves are not visible to the end user.
Otherwise, there is no way to modify PHP's built in error messages to hide the path. Doing so would render the error message much less helpful.
See display_errors and error_reporting PHP directives.
EDIT: In the case of the exact error message in your question, you could handle the error (try/catch) and then display a more friendly error that helps you but also doesn't expose your path. The reason it is displaying like that is because an exception that was thrown was uncaught.
No. If you don't want the complete debug backtrace in case of an uncaught exception, you'll need to catch it every time. There are no shortcuts here.

PHP error reporting: some errors to screen, all to log

I want to know how I can display E_ERROR error messages to the screen but write E_ALL error messages to the error log, we currently use the error_reporting() in our app index page so we can change error reporting without the need to constantly restart the web server, but it seems that this (or perhaps the way it's meant to work) means that we only log errors that we see on the screen.
Is there a way to log and display different levels of errors?
Cheers!
You could make a custom error handler, and in your error handler check if the error is an E_ERROR; if so, print it out. Then log the error, regardless of whether it is an E_ERROR or not.
If you're not familiar with custom error handling, the PHP manual has a good example on how to use an error handler to do different things depending on the nature of a PHP error.

Categories