Conditional logging in Apache using CustomLog and SetEnvIf

actived

Well-Known Member
Mar 30, 2012
51
0
56
cPanel Access Level
Website Owner
Hi,
I find my present default logging in Apache ("common" format) isnt upto my needs - I need to deduce / check / think out things rather than the info being easy for me to glean from it.

So I went through google+docs and made up a few rules.
I'm not sure where to put the directives, though.

I want detailed access logs but not for the entries that are generated from the server itself.
The default access_log file is overflowing with entries which begin with 127.0.0.1, eg.
Code:
127.0.0.1 - - [05/Nov/2012:03:06:21 +0800] "OPTIONS * HTTP/1.0" 200 -
127.0.0.1 - - [05/Nov/2012:03:06:22 +0800] "OPTIONS * HTTP/1.0" 200 -
127.0.0.1 - - [05/Nov/2012:03:06:22 +0800] "OPTIONS * HTTP/1.0" 200 -
I dont want these entries in my customised log file which I will use for analysis.

So I have to produce the following effect:
Code:
SetEnvIf Remote_Addr ^127\.0\.0\.1$ localrequest
and

Code:
<IfModule mod_log_config.c>
    LogFormat "%a|%t|%{Referer}i -> %U" referer
    LogFormat "%a|%t|%{User-agent}i" agent
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    LogFormat "%a|%l|%t|%T|%H|%m|%U|%q|\"%r\"|%>s|%b|\"%{Referer}i\"|\"%{User-Agent}i\"|\"%{Cookie}i\"" customised

    CustomLog logs/access_log common
    CustomLog logs/agent_log agent
    CustomLog logs/referer_log referer
    CustomLog logs/customised.log customised env=!localrequest
</IfModule>
But I'm unsure where to write the SetEnvIf line - and whether or not I need to enclose it in
Code:
<IfModule set_env_if.c>
</IfModule>
I guess all this goes in the Include Editor from WHM:
Main >> Service Configuration >> Apache Configuration >> Include Editor
in the "Pre Main Include" section for my Apache version which is 2.2.22

Any help is greatly appreciated.
Thanks in advance,
Regards,
Dave
 

actived

Well-Known Member
Mar 30, 2012
51
0
56
cPanel Access Level
Website Owner
I opened a ticket with cpanel and they quickly reverted with the answer:

The code goes into Post VirtualHost Include via
Main >> Service Configuration >> Apache Configuration >> Include Editor
Code:
<IfModule mod_setenvif.c>
SetEnvIf Remote_Addr "127\.0\.0\.1" localrequest
</IfModule>

<IfModule mod_log_config.c>
LogFormat "%a|%t|%{Referer}i -> %U" referer
LogFormat "%a|%t|%{User-agent}i" agent
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%a|%l|%t|%T|%H|%m|%U|%q|\"%r\"|%>s|%b|\"%{Referer}i\"|\"%{User-Agent}i\"|\"%{Cookie}i\"" customised

CustomLog logs/access_log common
CustomLog logs/agent_log agent
CustomLog logs/referer_log referer
CustomLog logs/customised.log customised env=!localrequest
</IfModule>
If something doesnt seem to work, play with the quotes ( ' or " ) around the IP address regex and the regex itself.

Useful references:

http://www.askapache.com/htaccess/setenvif.html

http://eric.lubow.org/2007/system-administration/a-few-apache-tips/

http://www.devside.net/guides/config/linux/httpd-conf
 
Last edited: