Force error log file in current directory - php

i have a developement dir on my server but i dont have root access so i cant change which php ini file loads. I ask my host if they would set things up so that i could have a custom php ini file but they dont allow that.
I am always having to go all the way back to the public dir to check the error log file during development. I would like to force the dev folder to create its own error log file. Is there a way to do this without root access. I just want to be able to check errors in the dev dir because its faster to check them.
I have already added a php.ini file to my dev dir. And inside that php ini file i have the error log location code
;just for testing
error_reporting = E_ALL
error_log = '/home/xxxxxxxx/public_html/xxxxxxxxx/xxxxxxx_dev/error_log'
but its probably not doing anything as they dont allow those. Can i do this with htaccess? or is there another way to do this for a local error log?
I am on a linux apache machine.

You can use set_error_handler() to change system default behavior.
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
// do something
}
set_error_handler('myErrorHandler');

This worked in the .htaccess file...
enable PHP error logging
php_flag log_errors on
php_value error_log /home/xxxxxxxxx/xxxxxxx/xxxxxxxxx/xx_dev/error_log

You can set error_log file in .htaccess, for example
# enable PHP error logging
php_flag log_errors on
php_value error_log /home/path/public_html/domain/PHP_errors.log
This file should be writable for apache process user (www-data, apache - see apache config).

Related

Enable php-logging on server of godaddy hosted wordpress

for about a day I'm trying to find a way to see php logs on a server by godaddy where wordpress is hosted.
I can ssh to the server, but I'm not able to edit the php.ini because it's readonly.
I've read that it would be possible to add a custom php.ini to change some settings. I've put one in the public folder where the wordpress site is located and added the line:
error_log = "/php_error.log"
In the same folder I've created the file php_error.log and set the following permissions:
-rw-rw----
As this seemed not to work, I've tried an approach editing the .htaccess file.
# log php errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_flag log_errors on
php_value error_log /php_error.log
Still I don't see any errors in the file.
Is it a problem of the permissions of the error file or should the path be different e.g. html/php_error.log ?
I've finally found a solution.
I have to create a .user.ini file to set custom settings of php.ini.
I've put the file in the root of my public website directory and inserted the following line:
error_log = "/var/www/php_error.log"
It took me quite a while to figure out what the expected path to my log file is. Hopefully this will save some other people a lot of time and headache.

I can't get php errors to log to a custom log in CentOS 7.3 and PHP7

I have in a php.ini in public_html that has
display_errors = on
error_reporting = E_ALL | E_STRICT
log_errors on
error_log /home/account/public_html/error.log
phpinfo shows that the php.ini file in public_html is being loaded. The error_log file has 755 permissions and belongs to the same group:owner as all other files in public_html.
But, when I force php errors, no errors are displayed or logged. Errors are logged in /usr/local/apache/logs/error_log. And I've restarted apache.
Any ideas?
Try changing:
log_errors on
error_log /home/account/public_html/error.log
To
log_errors = on
error_log = /home/account/public_html/error.log
Next, near the top of your script, do:
echo php_ini_loaded_file(); // to make sure that you're editing the right php.ini
echo ini_get('log_errors'); // should be '1'
echo ini_get('error_log'); // should be path from php.ini
If the settings don't match what you set (I assumed you restarted Apache), check the rest of the php.ini as well as Apache VirtualHost configuration for another setting that may be overriding yours.
When you runphpInfo(), it will sometimes show a list of ini files that get loaded (not just your basic php.ini. One of them could also contain an override.
Finally if none of that works, you could set error-logging in-script with ini_set(). Just be aware that if that script itself has an error, this setting may not have the chance to take effect so it's best to do it in a script separate from your main, and require it
main.php
require_once('./init.php);
// ... your regular script
init.php
ini_set('log_errors', '1');
ini_set('error_log', '/path/to/errors.log');

Where are PHP errors logged to if the error_log directive simply says "error_log"?

I ran phpinfo() and the error_log directive simply says error_log. What file is that referring to? i.e. what would the full path to the error_log be?
Quote from the documentation:
error_log string
Name of the file where script errors should be
logged. The file should be writable by the web server's user. If the
special value syslog is used, the errors are sent to the system logger
instead. On Unix, this means syslog(3) and on Windows NT it means the
event log. The system logger is not supported on Windows 95. See also:
syslog(). If this directive is not set, errors are sent to the SAPI
error logger. For example, it is an error log in Apache or stderr in
CLI.
So, when the value is not set (which is the default) it will be sent to the parent error logger, which is apache (if run via it) or stderr if you run the script on the command line.
If you use the script via apache you will have to look at the apache error log, usually in /var/log/apache or /var/log/httpd, depending on your distribution. You can check the apache configuration file for the exact location.
Edit:
I just noticed I misread your question, I guess you mean error_log has the actual value error_log?
I just did some testing. When I set error_log to a value like php_errors.log PHP still writes the error messages to the apache error log. It behaves as if the value was empty. When I set the value to a full path (e.g. /tmp/php_errors.log) then it writes the errors to the specified file.
So I guess in your case it writes the errors to the apache error log file.
Of course you can set your own log file be adding
ini_set("error_log", "/tmp/php_errors.log");
to your PHP files where you need it (if it hasn't been disabled by an administrator).
When the value simply says error_log (or error_log = error_log in your php.ini file), that means the errors will be written to a file called error_log in the same directory that the error occurs.
So, if you have a file called index.php in /home/user/public_html/ that throws an error, the error will be written to: /home/user/public_html/error_log.
If you have file called resize.php in /home/user/public_html/admin/images/ that throws an error, then the errors will be written to: /home/user/public_html/admin/images/error_log
Source: Display and log errors for PHP
Open your php.ini file.
Check the path against error_log.
On Windows 10, IIS 10.0.18362.1, I set this in file php\php.ini:
error_log = "C:\tmp\php_errors.log"
or
error_log = C:\tmp\php_errors.log
or
error_log = \tmp\php_errors.log
phpinfo() shows:
error_log C:\tmp\php_errors.log or \tmp\php_errors.log
Sometimes directly, but always after iisreset (recycling the application pool).
That is where the file is saved.
Without pad: error_log = php_errors.log it will be written where the error occurs. It has already been mentioned.

IIS php.ini display_errors = stderr to a file?

I am running a php app on microsoft IIS . I am debugging a problem and wants to use the php.ini directive display_errors = stderr . I want to see the errors messages in a file .
Is it possible in php.ini to redirect the stderr (or stdout) to a file ?
That's what error_log is for.
You can get the errors logged into the file of your choice by appropriately setting the value of error_log.

php.ini misconfiguration

I've narrowed my problem down somewhat.
When I run "error_log('hey');" from the command line it dumps to STDOUT. But if I run the same code from my web interface (Apache) it puts the error in the error log.
I've checked both ini files, the one Apache is using, and the one in /private/etc (I'm on a Mac running MAMP). Both error_log variables point to the exact same place.
And when I run
echo ini_get('error_log');
The value is the same on the command line as it is in the browser.
What ini setting is misconfigured here? This is quite annoying, as more than just error logging is broken. It's affecting my include paths as well :/
What are you trying to accomplish? Within apache, stderr goes into the error_log... the error_log() function documentation states that by default it logs to the server's error log. If you want to log to a different destination, use the message_type and destination parameters.
You probably need to edit the following config file:
/Applications/MAMP/conf/php5/php.ini
MAMP uses it's own Apache server, which by default runs on port 8080. You probably want to turn off the Apache server in the System Preferences -> Sharing.
Also, try running a PHP file containing:
<?php phpinfo(); ?>
This will tell you which php.ini is actually being read by Apache.
Will
A reason for error_log displaying in the console, and not in the log file might be because of a problem with permissions -- I don't really know MacOS, but as it's UNIX-based, I'm guessing that :
The log file used by Apache belongs to a specific user
When running the script from the console, you are not that user, and you don't have write-access to the log file
If it can't log to the log file, I suppose that error_log is writting to the standard output for error (stderr), which is generally the console.
This comment on the manual's page seems to indicate this might be the cause of your problems (quoting) :
it seems that PHP logs to stderr if it
can't write to the log file. Command
line PHP falls back to stderr because
the log file is (usually) only
writable by the webserver.
Also, make sure the log_errors and display_errors are properly configured in the php.ini file used in CLI :
log_errors boolean
Tells whether script error messages
should be logged to the server's error
log or error_log. This option is thus
server-specific.
And :
display_errors string
This determines whether errors should
be printed to the screen as part of
the output or if they should be hidden
from the user.
Value "stderr" sends the errors to
stderr instead of stdout.
The relevant ini setting here is display_errors.
From the command line a value of On will dump the errors to STDOUT; stderr will divert them to STDERR and Off will suppress them. For Apache only On and Off make any sense.
The odds are that the ini file for Apache has display_errors = Off whilst the one in /private/etc has display_errors = On.
The error_log directive tells PHP where to log errors to, but it also requires log_errors to be set to On, otherwise it has no effect. (Again, chances are the ini file in /private/etc has log_errors = Off.)

Categories