Apache vhost log rotate by date

apache-2.2logginglogrotatevirtualhost

I've got Apache 2.2 running on Debian 7 (on a VPS), with virtual hosts defined for several different websites.

I've set up individual error and access logs for each vhost, but I've not found a way to rotate/archive the logs to my liking.

Of course the default Apache logs on Debian are rotated by logrotate, but I don't find this particularly satisfactory for various reasons – primarily because I want archived logs to be named by date, and because configuring it separately for each vhost seems cumbersome. I'm also unsure about deleting old logs automatically; I might want to do this manually only after downlading them from the server.

My ideal solution would do the following every month (or maybe week) :

  • Append the date to the filename, or move the file to a date-named subfolder.
  • Gzip the archived files.

I also need this to be easy to set up for multiple vhosts.

Ideally, it should not involve restarting apache (though a graceful restart is not the end of the world).

What is the best way to set this up? I'm sure it's been done before…

Best Answer

(Do I detect a litlle bit of NIH?)

Here's why you shouldn't do that all by yourself:

  1. It's better to extend than to reinvent: In essential, what you want to do is already covered with logrotate. If you don't like the default behaviour of it, you can thankfully configure it to behave differently, via it's pre/post-actions, naming conventions etc.

  2. Solving an already solved problem: The guys that have contributed to logrotate all have encountered your problem before, and then some. At some point, you most likely run into the same detail problems they faced and solved with way more experience and feedback...do you really want to waste your energy to solve them again?

  3. Not deleting old logs is a very bad idea. This results in a waiting game for filling up your log partition – depending on your setup, your applications, or even your whole server, will come to a screeching halt. Preferably at the height of your day-to-day business.

    If you need to keep old logs around for accounting reasons (security, financial, gov), you have to implement an archiving solution for them.

  4. Supporting homebrew: If you ever run into problems, there's no one else to ask, since only you use this solution - whereas logrotate is used by many others.

  5. Setting up is easy: Just cp and sed your vhost configurations accordingly. Strive for automation.

Note that above reasons are not cast in stone, and do not apply everywhere. It's not a bad idea to challenge existing solutions from time to time (there might be a better solution), but in this case, I would consider this an exercise in futility.