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.
Related
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
CodeIgniter 2.x still uses the classic mysql. We all know it's bad practice to still use it, but my job still requires me to use CodeIgniter.
I always have my Console.app (OSX) open watching my Apache/MySQL/PHP/CodeIgniter-native error logs.
Now I mostly have all notices/errors/etc. fixed always instantly when I see them and constantly monitor my development with Webgrind on my MAMP.
Back to the start; I constantly have one big annoying thing each page-load PHP always gives the error about mysql_pconnect is going to get deprecated in the future.
In the CodeIgniter driver the command is suppressed by # to not print the warnings to the screen, but it still ends up in my logs.
Is there any viable way to except one such error specifically in either PHP code or the PHP settings?
Locally I could recompile the whole PHP core and just remove the warning, but I would like to have the logs on our production installations rid of those warnings too :P.
Thanks in advance!
Traditionally, you can use set error verbosity using error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED) (i.e., report everything—except notices and deprecation warnings) as mentioned in "disabling deprecated errors".
Your issue may be related to CodeIgniter taking ownership of all errors.
system/core/CodeIgniter.php calls the function set_error_handler. You can find and modify the function _exception_handler it invokes on error in system/core/Common.php. It doesn't appear to be a configurable, so you may simply want to edit the line that begins with $is_error.
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.
I would like to log PHP errors on a CakePHP site that has debug = 0. However, even if I turn on the error log, like this:
error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED
log_errors = On
it doesn't log errors.
The problem is that even for a parse error that should cause the CakePHP environment to not load completely (I think), it still blocks the error from being logged. If I set debug to 3, it logs to the file without issue.
I am using CakePHP 1.2. I know this is apparently made easier in 1.3, but I'm not ready to upgrade.
Another way to keep track of and log errors would be to use the Referee plugin as it provides a way to arbitrarily log and catch all (including fatal) errors that occur during exection.
define('LOG_ERROR', 2); in core.php
PHP should log errors to its own logfile, regardless of what CakePhp is doing.
Look in /etc/php.ini file (or wherever yours lives) and search for error_log. This will show you where the PHP log resides on your system.
There is a bug in CakePHP 1.2-1.3 where PHP errors/warnings are suppressed in view code when debugging is disabled.
In the file cake/libs/view/view.php on line #664 it reads
#include ($___viewFn);
But the # directive suppresses errors for the entire view handler. Instead it should be:
include ($___viewFn);
Which allows PHP errors/warnings to be generated in view code and subsequently get logged. Once I changed this and had the right logging settings in core.php I was finally able to get complete logs in production.
Sometime the reason could be very different. For example the framework you are using may have its own internal caching module which keeps the value in buffer while you keep on trying. Check whether duplicate copies are getting generated or not. Typically those files would be named as filename.ext.r123 and so on.
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.