Forum Moderators: phranque

Message Too Old, No Replies

Dynamic Log Filename in httpd.conf

Multiple servers fighting over a shared log file

         

komodo9

2:02 am on Oct 12, 2008 (gmt 0)

10+ Year Member



Hi,

I run a cluster of 6 apache servers that share a common httpd.conf (through nfs). The problem I'm facing is with the log files.

I do not want each server to store the log file on their own hard drives because of space restrictions, and I instead require the log files to be on the NFS mount (located at /array/).

All six servers are writing to the log files at the same time, causing spliced lines and nearly unreadable output. My question is regarding this line:

CustomLog /array/log/httpd-access.log combined

Is there any way to save the log file to a dynamic path, such as:

CustomLog /array/log/__$LOCAL_HOST_NAME$__/httpd-access.log

Or even better, does anyone have any ideas on how to keep a single log file for all servers, without splicing and mutilating the output, and without slowing down each server, waiting for locks on the file to release?

I look forward to your help. Thanks in advance. :)

jdMorgan

2:29 am on Oct 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The usual way to do this is to log per-server, using the ServerName or HTTP_HOST variable as a conditional-logging-control argument to mod_log_config.

Or, I suppose you could pipe the log-lines through a script and have it enforce "per-record" locking, but beware of deadlocks caused by individual server crashes and by failed or flaky "flocks." Use timeouts to break these deadlocks after a fairly short period of time (milliseconds, not seconds) -- It's better to have "splices" than to lock up your logging process, for even the surviving servers!

Jim

komodo9

10:01 pm on Oct 13, 2008 (gmt 0)

10+ Year Member



jdMorgan, I appreciate your incite, but in looking around, I don't quite see how mod_log_config would allow me to save the logs to a dynamic path (interpolating the hostname).

Would you mind expanding on this?

Thank you!

skelly

11:40 pm on Nov 28, 2008 (gmt 0)

10+ Year Member



Hi,

I found your question on google looking for the same answer: clustered apache logs that automatically select per-host filenames without per-host config files. Based on that objective and on what jdMorgan mentioned above, I remembered seeing something some time ago relatve to this, and here's what I came up with:

1) Keep an httpd.conf file per host server. It can have a minimal number of directives in it and include external shared files that are common to all your hosts, but start with a unique file per host.

2) Inside httpd.conf per host, include the following directive:

# On server 1:
SetEnv www1 www1

# On server 2:
SetEnv www2 www2

# and so on for each host...

3) In your vhost container where the CustomLog directives live, create a CustomLog config line that matches each host depending on these environment variables:

# Per vhost, add both lines:
CustomLog "¦ /usr/local/apache/bin/rotatelogs /logs/www1_access_log.%Y%m%d 86400 -420" combined env=www1
CustomLog "¦ /usr/local/apache/bin/rotatelogs /logs/www2_access_log.%Y%m%d 86400 -420" combined env=www2

If there are 6 hosts, each vhost will have six such lines. Each line logs data to a different log file depending on which host has served the request based on the environment variable checking.

That's it, and works for me on latest version of Apache 2.2 sitting on Centos Linux, placing the logs all the the same shared NFS mounted directory.

Regards,
SK

jdMorgan

8:19 pm on Dec 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Another approach is to use one common log file declaration, naming a PERL script as the log file. Then have this PERL script look at the HTTP_HOST and/or other appropriate variables and write the incoming log entry to an appropriately-constructed filepath.

Pay close attention to your operating system's file-locking semantics in order to avoid 'spliced' log entries. It shouldn't be necessary, since writing a record is normally an 'atomic' function, but if you are having trouble try using the 'flock' function to lock the log file while any given process or thread is appending a new record.

Jim