How can I set up error_log by folder? How can I set up error_log by folder? apache apache

How can I set up error_log by folder?


I found it.

You have to set a directive in the php.ini file as follows, string "error_log". On the right side is the file name you want for the log,

error_log = error_log

This will generate a PHP error log in the folder where script executed are,

I'll try to explain.

Script test.php in folder /www/site/lib:

include "./db_conn.php";

If file db_conn.php is not located in the same directory, this will fire a warning and error. Usually this will be lead to the servers/vhost log, but using this directive you will get an error_log file under the /www/site/lib directory.

Why was I looking or this? Well, as I wrote, I'm working on a huge application, with thousands of files many fires warnings, notices, etc. I'm not the only one in the project and the site error_log file was so huge it was hard to keep tracking debug evolution for one or just some files. Now I just have to track the log from directories where I'm working.


You can manage the logs by adding this to your vhost or htaccess file

ErrorLog /path/to/a/writable/directory/error.log

For more information, have a look at this article on advanced PHP error handling via htaccess.


To do this in PHP, edit file php.ini and add the line

error_log = /path/to/where/you/want/your/php-errors.log

Then restart the web server. This should give you PHP errors, and only PHP errors, to that file. Provided, of course, that the scripts aren't written to throw away all errors.

To do it in Apache, you can use SetEnvIf to add an environment variable to any request ending in PHP, and then printing all logs with that particular environment variable. E.g.:

SetEnvIf Request_URI "\.php$" phplogCustomLog /path/to/php.log env=phplog

To use this in multiple folders, make one environment variable per folder and one CustomLog per folder, like this:

SetEnvIf Request_URI "/folder1/.*\.php$" log_folder1Customlog /path/to/folder1/php.log env=log_folder1SetEnvIf Request_URI "/folder2/.*\.php$" log_folder2Customlog /path/to/folder2/php.log env=log_folder2