How to customize LogFormat for Apache Virtual Hosts?
The trick is to add CustomLog directives inside each VirtualHost in your Apache config files. For example:
<VirtualHost *:80>
ServerName www.site1.com
DocumentRoot /var/www/www.site1.com/htdocs
CustomLog /var/log/apache/www.site1.com-access.log combined
ErrorLog /var/log/apache/www.site1.com-error.log
</VirtualHost>
<VirtualHost *:80>
ServerName www.site2.com
DocumentRoot /var/www/www.site2.com/htdocs
CustomLog /var/log/apache/www.site2.com-access.log combined
ErrorLog /var/log/apache/www.site2.com-error.log
</VirtualHost>
There are more useful examples here.
Related videos on Youtube
![Mina Hafzalla](https://i.stack.imgur.com/OJnpj.jpg?s=256&g=1)
Mina Hafzalla
Updated on September 18, 2022Comments
-
Mina Hafzalla almost 2 years
I really need some help with this. My server has multiple virtual hosts, each virtual host has its access log, I need to be able to customize the LogFormat for each virtual host exist on server. I have modified LogFormat in
httpd.conf
but it actually affects only the master access log and not the virtual hosts.Here is my LogFormat:
<IfModule log_config_module> LogFormat "%v:%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{CF-Connecting-IP}i" combinedvhost LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{CF-Connecting-IP}i" combined LogFormat "%h %l %u %t \"%r\" %>s %b %{CF-Connecting-IP}i" common CustomLog "logs/access_log" combined CustomLog logs/access_log combinedvhost <IfModule logio_module> LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{CF-Connecting-IP}i" combinedio </IfModule> </IfModule> <IfModule mod_log_config.c> LogFormat "%v:%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{CF-Connecting-IP}i" combinedvhost LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{CF-Connecting-IP}i" combined LogFormat "%h %l %u %t \"%r\" %>s %b %{CF-Connecting-IP}i" common LogFormat "%{Referer}i -> %U" referer LogFormat "%{User-agent}i" agent CustomLog logs/access_log common CustomLog logs/access_log combined CustomLog logs/access_log combinedvhost </IfModule>
The above LogFormat only affects the master access log which located at:
/usr/local/apache/logs/access_log
However, it does not affect any of the virtual hosts on server that have their access logs located at:/home/username/access-logs/domain.com
I'm not sure if I have something wrong in the LogFormat itself or I'm missing something. I've spent many hours trying to solve this problem but didn't find a solution. I'd greatly appreciate it if someone could shed some light on this. Thank you.
-
fixer1234 about 9 yearsCan you elaborate on that (how to do it)?