LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Create an Apache log for a particular <location> (https://www.linuxquestions.org/questions/linux-server-73/create-an-apache-log-for-a-particular-location-886460/)

markdibley 06-15-2011 05:36 AM

Create an Apache log for a particular <location>
 
Hi

I have an apache server setup where I have different rules using <location>. Essentially, I have a test environment "/test/" and I would like to capture the logs for activity in this location and direct it into a separate log.

I know that perhaps I should have set up virtual hosts, but I wasn't aware of them at the time and the change over can not be accommodated at the moment (the change control wouldn't be agreed). I was wondering if it could be done using environment variables.

At the moment I use

Code:

tail -f /var/log/httpd/error_log | grep "/test/"
but this does miss out some messages, particularly multiple line SOAP messages.

Any suggestions would be welcome. Thanks.

Mark

bathory 06-15-2011 06:48 AM

Hi,

You can use SetEnvIf for this, even though I also think that a virtualhost approach would be a better solution.
Anyway, use in httpd.conf (or whatever .conf file apache uses) the follwoing:
Code:

SetEnvIf Request_URI "^/test/" testlog
and 2 Customlog directives, like:
Code:

CustomLog /path/to/access_log common env=!testlog
Customlog /path/to/test.log common env=testlog

Regards

markdibley 06-15-2011 08:00 AM

That is great. I had most of the first part, but not the second part.

I presume the ^ in "^/test/" is part of a regex. If so that makes it a very powerful.

I will definitely look at virtual hosts the next time round.

Thanks for the solution.

Mark

bathory 06-15-2011 08:31 AM

You're welcome.
BTW, you're right: ^ is used in a regex that matches everything that starts with the string following the ^ character

Regards

markdibley 06-15-2011 11:31 AM

Hello

Sorry to open this up again, but having tried this I realise that CustomLog appears to be logging access logs only. However, it is the ErrorLog that I wish to redirect into separate logs.

Having read further I can see that ErrorLog is non-configurable, but I was wondering if the error is part of an environment variable that I can output using CustomLog.

Thanks again

bathory 06-16-2011 12:02 AM

Hi,

Indeed you cannot customize error_log. Anyway if you're using a CustomLog as above (i.e. using the common format), you can filter the test.log based on the response code (penultimate field):
Quote:

192.168.254.1 - - [16/Jun/2011:07:59:54 +0300] "GET /error.htm HTTP/1.1" 404 207
You can create a script to look for a 404 (or better yet, for a 4XX, as all these are error codes) in that field and pipe the output to another file

Regards


All times are GMT -5. The time now is 09:22 PM.