Logging for different requested URI in Apache HTTP Server

Written on September 1st, 2009 by Thorno shouts

Sometimes,we might want to log differently for different requested URI in Apache http server.

Activate custom logging in your Apache http server

First,write a line of custom log format and name it as CustomLogFormat

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" CustomLogFormat

Then add another line to activate the logging.
CustomLog /tmp/logForAll.log CustomLogFormat

Restart the webserver and you shall see the every visits to your server are logged in the log file.

But wait,what if I want to separate the log files when user visits login.php(or whatsoever file)?

After the LogFormat line,we add another line to specify the condition.

SetEnvIf Request_URI "^/login\.php$" isLoginPage

Then we modified the previous CustomLog line to exclude logging on login page visit.
CustomLog /tmp/logForAll.log CustomLogFormat env=!isLoginPage

At last,we add another line to log only login page visit.
CustomLog /tmp/logOnlyForLogin.log CustomLogFormat env=isLoginPage

Restart your web server and we are done!

Checkout the Apache log files documentation for more info.

Happy coding. :)

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • DZone
  • LinkedIn
  • Live
  • Reddit
Filed under Programming Tags:

Leave a Reply

(required)

(required)