3

Is it possible to configure Apache to use a different log file for each subdomain, even if all of the subdomains are within the same VirtualHost container?

So, I have only one VirtualHost:

<VirtualHost *:80>
  ServerName example.com
  ServerAlias test1.example.com test2.example.com test3.example.com
</VirtualHost>

And I want to end up with the following log files:

/var/log/httpd-access_test1.log
/var/log/httpd-access_test2.log
/var/log/httpd-access_test3.log

I know I can probably do this with a custom log format and split-logs, but I was wondering if there's a way to just have Apache do it for me.

2 Answers 2

3

You just specify ErrorLog and CustomLog within each VirtualHost directive. With that in mind, you would not be able to use ServerAlias and have separate log files for each host via normal performance without specifying separate VirtualHost.

However, you could pipe the log through a script, and have the script make the separate files. Look at the piped logging documentation.

You could also use a post processing script, such as utilizing grep to parse out the logs. A post processing script could be specified in the nightly logrotate under the postrotate or preprotate sections.

Apache 2.2 Piped Logging

4
  • 1
    Having a different server log for each VirtualHost doesn't help me. I have only one VirtualHost. Within that single VirtualHost there are multiple subdomains. I need separate logs for each subdomain within the single VirtualHost. Mar 24, 2010 at 15:53
  • Separate your ServerAlias subdomains into VirtualHost s with identical configurations (See the Apache documentation Re: NameVirtualHost), or proceed using piped logging & a custom processing script as Warner describes above.
    – voretaq7
    Mar 24, 2010 at 17:59
  • 1
    I'd really rather not do that, the VirtualHost has a lot of configuration with it, and it would be a lot of copy/paste configuration stuff. Maybe there's a way I can do an Include with the generic stuff... Mar 24, 2010 at 18:11
  • piped logging as Warner describe will probably be a good bed for you.
    – corprew
    Mar 24, 2010 at 21:54
1

You should be able to use apache environment variables to get this working:

LogFormat "%h \"%r\" %>s \"%{Referer}i\" \"%{User-Agent}i\"" vhostformat

SetEnvIfNoCase Host example\.com mainlog
CustomLog /var/log/httpd-example.log vhostformat env=mainlog

SetEnvIfNoCase Host test1\.example\.com vhost1log
CustomLog /var/log/httpd-access_test1.log vhostformat env=vhost1log

SetEnvIfNoCase Host test2\.example\.com vhost2log
CustomLog /var/log/httpd-access_test2.log vhostformat env=vhost2log

SetEnvIfNoCase Host test3\.example\.com vhost3log
CustomLog /var/log/httpd-access_test3.log vhostformat env=vhost3log

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .