“Catch-All” access log with Apache Virtual Hosts

apache-2.2log-filesvirtualhost

I have many virtual hosts set up on a web server, each one having its own error and access log. The relevant lines of httpd.conf are something like this:

ErrorLog /var/log/httpd-error.log
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /var/log/httpd-access.log combined

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName myhost.com
    ServerAlias www.myhost.com
    DocumentRoot /var/www/myhost.com/htdocs
    ErrorLog /var/www/myhost.com/log/error.log
    CustomLog /var/www/myhost.com/log/access.log combined
</VirtualHost>

# ... many more VirtualHosts

Currently, I'm getting some random errors in /var/log/httpd-error.log, but I'm getting nothing in /var/log/httpd-access.log. Is it possible to have ALL accesses and errors duplicated to a shared logfile? Is it possible to do this without adding new entries to every single VirtualHost?

Best Answer

See http://httpd.apache.org/docs/2.2/logs.html#virtualhost

If CustomLog or ErrorLog directives are placed inside a section, all requests or errors for that virtual host will be logged only to the specified file. Any virtual host which does not have logging directives will still have its requests sent to the main server logs.

In other words, if you place Logging directives within a VirtualHost section, it will override the Logging directives within the main server configuration. If you want to log to a single logfile, then remove the log configuration from your VirtualHost sections.

For simplicity, I prefer to log all Access data to a single logfile. Later, you can process the logs and split the logfiles into logfiles for the Virtual Hosts. Also, writing to a single logfile is a more efficient use of computer resources then writing to 30 logfiles at once. Just make sure your LogFormat includes the '%v', which will log the name of the Virtual Host.

Is it possible to have ALL accesses and errors duplicated to a shared logfile?

You can log all errors and access to a shared logfile, but the logfile is ugly. First, send the Apache log data to syslog, and then use syslog to send to a local file or a remote log server.

# Send access logs to syslog
LogFormat "%v %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog "|/usr/bin/logger -t httpd -i -p local7.notice" combined
# Send error logs to syslog
ErrorLog syslog:local7

And then in /etc/syslog.conf

# Send all HTTP log data to this file
local7.*        /var/log/http-all.log