How can I set up error_log by folder?
Asked Answered
A

3

6

My company has a large hosting, but it’s not managed by us, we don't see configuration files, but I want to reply this feature on our local test server.

I’m new in my company and want to start some debug of applications to fix some minors and majors issues to clients, but the amount of files is so big that single error_file is huge.. and there are many people working on this so each time I check log (like 30 secs to 1 min) has hundreds of added lines.

I don’t know if this is set up on Apache, through .htaccess files or in php.ini.

I am talking about PHP errors, but I don't know if this is set in PHP, Apache, or maybe using a third-party library.

I'm not talking about setting a specific folder error_log. I'm talking about if errors are logged in the scripts folder.

Example: I create a folder named test1. Inside it I make some buggy PHP script that throws some errors. When I run the script I can see an error_log file created in the folder. So it works on the fly.

I have tried to ask the hosting company support how they do this, but they haven’t answered me.


I don't know if maybe could be some kind of cPanel setting (BTW, the hosting support stuff doesn't understand this question either, but well.. usually level 1 of support can’t handle technical stuff).

Autocephalous answered 8/5, 2012 at 13:43 Comment(1)
You are talking about PHP errors, not Apache errors, am I right?Ovotestis
A
14

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.

Autocephalous answered 22/5, 2012 at 22:9 Comment(1)
The second "error_log" is confusing (no matter any explanation). Is it literal or not? If not, can you choose another name? (But without "Edit:", "Update:", or similar - the question/answer should appear as if it was written today.)Ritchey
E
1

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.

Earley answered 8/5, 2012 at 14:12 Comment(1)
I know how to setup errror_log by vhost, im looking how to get log inside each folder so i can see only errors form scripts on this folderAutocephalous
G
0

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$" phplog
CustomLog /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_folder1
Customlog /path/to/folder1/php.log env=log_folder1

SetEnvIf Request_URI "/folder2/.*\.php$" log_folder2
Customlog /path/to/folder2/php.log env=log_folder2
Gallaway answered 8/5, 2012 at 14:30 Comment(3)
That is a correct way to redirect php errors to single custom file, But its not what im asking for, thanks anywayAutocephalous
In that case, all I can say is I have no idea what it is you want. I don't understand what you mean by "errors are logged on scripts folder". If you mean that you want all scripts in a particular folder to be logged to a file within that particular folder, then you can still do that with SetEnvIf, by setting the environment depending on which folder it's in and then having a CustomLog for each folder. But this will give you the Apache logs, not the php logs.Gallaway
What i mean is that errors generated by a scripts keep logged on error_log file in folder where the scripts are without have a rule for each folder, i want to reply or copy this behaviour from my hosting company but they didnt tell me how this work, but it does, if a make a new folder and a new file inside and this generate so error. automatically error_log is created on this new folder and log inside, but how i dont know, if its happening because apache or php or Cpanel configuration i dont know, thats what im looking forAutocephalous

© 2022 - 2024 — McMap. All rights reserved.