Currently, when a PHP error occurs, it will log it into a file called error_log in the same directory the error occurred in.How can I keep the error_log files outside of the web root so that users cannot view them, for security?
Edit your php.ini file. Find the error_log setting and change it to the full path of where you want the error_log file to be written.
Related
When I log messages via error_log() in my PHP web app, the messages get logged in Apache's /var/log/httpd/error_log.
When I log messages the same way in a PHP command-line app, the messages go to PHP's own php-error.log
Is there a way to log messages to PHP's error log from a PHP web app?
In your php.ini file look for this parameter
error_log =
And set it to whatever filename and location you want.
Of course if you want to seperate your web PHP error logs from your PHP CLI error logs then you need to remember that there are normally 2 php.ini files
One that is used by Apache/PHP and the other that is used by PHP CLI possibly /etc/php5/cli/php.ini
If you change the php.ini file used by the PHP CLI you can have a different file name completely used for the error logging from PHP CLI scripts
Look for
error_log =
and change to
error_log = /var/log/php_cli_errors.log
for example.
I recently got already written php project, and I am trying to work on it. I can see that all logs are written to the specific location using file_put_contents() function.
But I can see that there's error_log() function in one place, without a destination file given. It looks like this:
error_log($str);
Where can I find logs file in this case?
The error log file is set with the error_log directoive in php.ini.
If the directive is not set, the file location is managed by the SAPI error logger, which in case of Apache it is in "Apache" logs directory.
You can find if this directive have a value with phpinfo()
i have error in my page but it is not logs in to the log file
i have set
display_errors:Off
display_startup_errors:Off
error_log:error_log
log_errors:On
i have restarted apache after configuration
but it is not working
just checked the apache file /etc/httpd/logs/error_log file
it contain the error but why the error_log does not creating in the folder in which error occurs
try this in your php file
ini_set("log_errors", 1);
ini_set("error_log",$_SERVER['DOCUMENT_ROOT']."/logs/php-error.log");
Give write permission to the folder logs and its file, then check the file in logs folder.
error_log:error_log
error_log not has correct path, set a correct path like below
log_errors = On
error_log = "C:\xampp\php\logs\php_error_log"
You said:
just checked the apache file /etc/httpd/logs/error_log file it contain the error ...
which means:
loggine php errors works
all errors are written to /etc/httpd/logs/error_log
You also said:
... but why the error_log does not creating in the folder in which error occurs
Your php.ini directove error_log specifies where your want your error log to be. Now it is default. Specify different log file location and you'll have it in different place. There is no why - it lands where it is said to be.
I have tried two different things on my server:
created a directory php in /var/log/
changed owner:group to www-data:www-data (apache owner)
changed permissions of the directory to 775
php.ini has error_log = "/var/log/php/php.log"
php.ini already had error logging set to on, and error reporting set to all
restarted apache
in the code, changed error_log('message') to error_log('message',3,path_to_logfile)
Neither results in messages in the applicable log file.
You say you created /var/log, but you're trying to write to /var/log/php. Try creating a directory called php in /var/log.
We have a linux hosting from a company and if any php script on that server has any PHP FATAL errors, it is automatically written into a file separately for each directory. So, although PHP errors are off in my scripts, I can still get the error logs as log files in each directory of those PHP scripts.
What I know that I can enable PHP error from configurations for my local server and set a specific file path which will contain all PHP errors from all scripts AND from all directories. But, I would love something automatic and separate like them.
How can I achieve the function in my local servers that we have on our hosting? Have they written a special script or is that a PHP ini settings that I should know?
UPDATED
Let's say public_html directory has three A, B and C directory or folder. There are three scripts a.php, b.php and c.php in those directories respectively. So, each of those directories will have error_log file that can contain all errors of those scripts separately. Let's say, folder A has also another script called d.php. But, errors from a.php and d.php will be written under public_html/A/error_log file.
Other directories will also have following files:
public_html/B/error_log file
public_html/C/error_log file
In php.ini, I had to enable the following setting and restart the server. It worked like I needed.
error_log = "error"