I am sending in error log information using PHP's error_log() function and it's working just fine except that every line -- when run from the web-browser under MAMP -- is prepending a timestamp such as this:
[26-Jun-2013 00:29:23 Europe/London] My message goes here
Obviously a timestamp isn't the worst idea but I'd like to have control over the full text and how and where the timestamp goes on the line. How does this initial text get set? I thought it must be in the php.ini or httpd.ini files but on quick perusal I couldn't find it.
You can't, at least not in Apache 2.
It is not possible to customize the error log by adding or removing
information. However, error log entries dealing with particular
requests have corresponding entries in the access log. For example,
the above example entry corresponds to an access log entry with status
code 403. Since it is possible to customize the access log, you can
obtain more information about error conditions using that log file.
http://httpd.apache.org/docs/2.2/logs.html#errorlog
So if you want a custom formatted log, don't use apache's error log.
Create your own error handler: http://php.net/manual/en/function.set-error-handler.php It works with trigger_error()
If you're running Apache it appears this may be something controlled by mod_log_config. There is a LogFormat directive in your httpd.conf that controls the format for error messages
Similar thread: How to change default timestamp that PHP uses for logging to file?
Related
I am having an issue when I have a php application that is returning an internal server error (500) however nothing is showing up in the error log.
Now I know there are error with what I am trying to run, I know I have missing some files and what not but something should show in the apache error log (otherwise how are I supposed to know exactly what I am missing).
I created a test script is errors it in under the same vhost configuration and those error show up fine so everything seems configured right as far as php/apache. Are there certain php errors that does show up in the error log (php is configure to display any type of notice, warning, , error, fatal error, etc...)?
This is running on ubunut 10.04 with the standard apache and php from the ubuntu repo with apt-get.
Scan your source files to find #.
From php documentation site
Currently the "#" error-control operator prefix will even disable
error reporting for critical errors that will terminate script
execution. Among other things, this means that if you use "#" to
suppress errors from a certain function and either it isn't available
or has been mistyped, the script will die right there with no
indication as to why.
Copy and paste the following into a new .htaccess file and place it on your website's root folder :
php_flag display_errors on
php_flag display_startup_errors on
Errors will be shown directly in your page.
That's the best way to debug quickly but don't use it for long time because it could be a security breach.
If you still have 500 error and no logs you can try to execute from command line:
php -f file.php
it will not work exactly like in a browser (from server) but if there is syntax error in your code, you will see error message in console.
Maybe something turns off error output. (I understand that you are trying to say that other scripts properly output their errors to the errorlog?)
You could start debugging the script by determining where it exits the script (start by adding a echo 1; exit; to the first line of the script and checking whether the browser outputs 1 and then move that line down).
In the past, I had no error logs in two cases:
The user under which Apache was running had no permissions to modify php_error_log file.
Error 500 occurred because of bad configuration of .htaccess, for example wrong rewrite module settings. In this situation errors are logged to Apache error_log file.
For Symfony projects, be sure to check files in the project'es app/logs
More details available on this post :
How to debug 500 Error in Symfony 2
Btw, other frameworks or CMS share this kind of behaviour.
Here is another reason why errors might not be visible:
I had the same issue. In my case, I had copied the source from a production environment. Hence the ENVIRONMENT variable defined in index.php was set to 'production'. This caused error_reporting to be set to 0 (no logging). Just set it to 'development' and you should start seeing error messages in apache log.
Turned out the 500 was due to a semi colon missing in database config :-)
Another case which happened to me, is I did a CURL to some of my pages, and got internal server error and nothing was in the apache logs, even when I enabled all error reporting.
My problem was that in the CURL I set
curl_setopt($CR, CURLOPT_FAILONERROR, true);
Which then didn't show me my error, though there was one, this happened because the error was on a framework level and not a PHP one, so it didn't appear in the logs.
You need to enable the PHP error log.
This is due to some random glitch in the web server when you have a php error, it throws a 500 internal error (i have the same issue).
If you look in the PHP error log, you should find your solution.
see here in the doc of how to enable it in the php.ini
Be sure your file permissions are correct. If apache doesn't have permission to read the file then it can't write to the log.
What happened for me when this was an issue, was that the site had used too much memory, so I'm guessing that it couldn't write to an error log or displayed the error. For clarity, it was a Wordpress site that did this. Upping the memory limit on the server showed the site again.
SOLVED
I struggled with this and later on, I realized that I was working on PHP 5.6, so I upgraded to PHP 7.0, then I released there were comments placed by git for conflicting codes. I found something like this in my code <<<<<<<< But solved it.
I have an IIS server which is serving PHP via fastcgi.
When the error log file is written to by a user other than one in IIS_IUSRS group (The group the IIS User is running under) the file becomes un-writable by IIS and the PHP calls to error_log() causes a 500 error. (At least that's my guess seeing as if I delete the log file, the error dissapears and the log file is re-created).
Is there anyway I can stop the 500 error from happening?
EDIT: To be clear I know I can stop this by stopping logging, logging to event log or different location etc, but that's not what I mean. I mean I just want to prevent the 500 error, I don't care enough that my system can't log that it should break the site when it tries to. That's exactly the worst behaviour it could have. I just want the 500 error to not happen and the site to continue working.
Since your scheduled task is actually changing the permissions on the error log file, the only viable options I can see are:
1) Make the scheduled task not write to the error_log. Add the following to the top of the cron job:
error_reporting(E_NONE);
2) Make the scheduled task write to the system log (event viewer in windows) by issuing the following command at the start of your scheduled task (PHP file):
ini_set('error_log', 'syslog');
3) If all of the above do not suit you, you can try scheduling the task as the IIS User/Group. This would insure that the permissions are met and error 500 is no longer caused.
There is no magic fix to this, you can either change the scheduled task so it has the same UID/GID as the PHP process, or you can stop logging in the scheduled_task.
Edit the php.ini, and find this line and edit:
error_log = /your/website/path/to/log
And sure to change the display_errors to off:
display_errors = off
Remember to put chmod 777 to the file :). If u wanna to see the file in the browser, can put something like this in the .htaccess file:
<Files /your/site/path/to/log/file.log>
allow from 10.0.1.1/16
deny from all
</Files>
PS: Sry, i dnt see the notation are a ISS server... hum, maybe can view more information about the error in the error_log of the ISS (i dont know where is)
I experienced this. Your error_log might be too big for a file. For example, when your error_log reaches 16mb the server will throw an error 500. You may delete your error_log and try if it still throws that error. You may wanna check the error_logs permissions and ownership too.
You want to avoid the error_log function from throwing a 500 Internal Server Error!
Well, if that may be the case, have you tried prepending your call to error_log with an '#'. That could possibly suppress the error. Please try and revert! :)
You could try using error_log() to write your application log in an alternative path:
error_log("Some fancy error to log", 3, "c:\tmp\your-custom-errors.log");
Or you could configure php to log to windows event log (in your php.ini):
error_log = syslog
Is it possible to configure php.ini to store errors in MySQL database rather than plain error-log?
The only option that I see is using php.ini to append file containing custom error handling function to every PHP script. Though, this doesn't sound efficient.
This is more of a server level question, if you don't like the answer that Frankie provided in his comment. Without using set_error_handler there is no way (in PHP) to output all errors to a log file instead of the log.
If you are using Apache, you can do the following:
CustomLog "|/path/to/custom_log_script.php [OPTIONS]"
(note the pipe)
That will allow you to use a custom error log handler to control what does and doesn't wind up in the log files.
You can set a cron job to execute a php script read the error log file for new errors and store them in the database. The error log table won't be real time though but I think it shouldn't be too big a problem.
Is there a PHP function or some other way of obtaining the PHP error log as a string?
I need this because I cannot access the error log of a site I am running on someone else's server. - He offered to email me the error log, but that isn't exactly convenient.
Is there some way I could output the error log to a PHP page?
I realize that viewing the entire server's error log is not really going to happen for me. However, I know you can do something like this to email a manual error_log call to yourself:
error_log('A really bad error', 3, 'me#myemail.com');
Is it possible to configure a page to email errors to you instead of displaying them?
On a badly secured server, yes. But on most servers there are two users: apache and [ you ]. You don't have access to the server logs, since they are owned by the apache user (or whichever server you're using).
However, you could probably try it:
echo file_get_contents('/var/log/httpd/error_log');
Note: that's the default location on a RedHat-based apache server. It may be different
Update To reflect the updated question
No, you cannot view the error log with error_log - it is a one-way process that gets handled by the webserver. It only writes the log, but you cannot read it.
You can probably display the errors with this:
ini_set('display_errors', 'On');
error_reporting(E_ALL);
You could even use set_error_handler to handle all warnings and notices (for example, to mail them). But that's pretty much all you can do.
I am having an issue when I have a php application that is returning an internal server error (500) however nothing is showing up in the error log.
Now I know there are error with what I am trying to run, I know I have missing some files and what not but something should show in the apache error log (otherwise how are I supposed to know exactly what I am missing).
I created a test script is errors it in under the same vhost configuration and those error show up fine so everything seems configured right as far as php/apache. Are there certain php errors that does show up in the error log (php is configure to display any type of notice, warning, , error, fatal error, etc...)?
This is running on ubunut 10.04 with the standard apache and php from the ubuntu repo with apt-get.
Scan your source files to find #.
From php documentation site
Currently the "#" error-control operator prefix will even disable
error reporting for critical errors that will terminate script
execution. Among other things, this means that if you use "#" to
suppress errors from a certain function and either it isn't available
or has been mistyped, the script will die right there with no
indication as to why.
Copy and paste the following into a new .htaccess file and place it on your website's root folder :
php_flag display_errors on
php_flag display_startup_errors on
Errors will be shown directly in your page.
That's the best way to debug quickly but don't use it for long time because it could be a security breach.
If you still have 500 error and no logs you can try to execute from command line:
php -f file.php
it will not work exactly like in a browser (from server) but if there is syntax error in your code, you will see error message in console.
Maybe something turns off error output. (I understand that you are trying to say that other scripts properly output their errors to the errorlog?)
You could start debugging the script by determining where it exits the script (start by adding a echo 1; exit; to the first line of the script and checking whether the browser outputs 1 and then move that line down).
In the past, I had no error logs in two cases:
The user under which Apache was running had no permissions to modify php_error_log file.
Error 500 occurred because of bad configuration of .htaccess, for example wrong rewrite module settings. In this situation errors are logged to Apache error_log file.
For Symfony projects, be sure to check files in the project'es app/logs
More details available on this post :
How to debug 500 Error in Symfony 2
Btw, other frameworks or CMS share this kind of behaviour.
Here is another reason why errors might not be visible:
I had the same issue. In my case, I had copied the source from a production environment. Hence the ENVIRONMENT variable defined in index.php was set to 'production'. This caused error_reporting to be set to 0 (no logging). Just set it to 'development' and you should start seeing error messages in apache log.
Turned out the 500 was due to a semi colon missing in database config :-)
Another case which happened to me, is I did a CURL to some of my pages, and got internal server error and nothing was in the apache logs, even when I enabled all error reporting.
My problem was that in the CURL I set
curl_setopt($CR, CURLOPT_FAILONERROR, true);
Which then didn't show me my error, though there was one, this happened because the error was on a framework level and not a PHP one, so it didn't appear in the logs.
You need to enable the PHP error log.
This is due to some random glitch in the web server when you have a php error, it throws a 500 internal error (i have the same issue).
If you look in the PHP error log, you should find your solution.
see here in the doc of how to enable it in the php.ini
Be sure your file permissions are correct. If apache doesn't have permission to read the file then it can't write to the log.
What happened for me when this was an issue, was that the site had used too much memory, so I'm guessing that it couldn't write to an error log or displayed the error. For clarity, it was a Wordpress site that did this. Upping the memory limit on the server showed the site again.
SOLVED
I struggled with this and later on, I realized that I was working on PHP 5.6, so I upgraded to PHP 7.0, then I released there were comments placed by git for conflicting codes. I found something like this in my code <<<<<<<< But solved it.