The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Conditional logging in Apache using CustomLog and SetEnvIf

Discussion in 'EasyApache' started by actived, Nov 4, 2012.

  1. actived

    actived Well-Known Member

    Joined:
    Mar 30, 2012
    Messages:
    51
    Likes Received:
    0
    Trophy Points:
    6
    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:
    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
     
  2. actived

    actived Well-Known Member

    Joined:
    Mar 30, 2012
    Messages:
    51
    Likes Received:
    0
    Trophy Points:
    6
    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
    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
     
    #2 actived, Dec 3, 2012
    Last edited: Dec 3, 2012
Loading...
Similar Threads - Conditional logging Apache
  1. Harikalar Kutusu
    Replies:
    4
    Views:
    128
  2. RWH Tech
    Replies:
    9
    Views:
    227

Share This Page