Combine daily access logs to single monthly log cron job

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Combine daily access logs to single monthly log cron job

Post by JAB Creations »

I just added this to a sever I use though am waiting for the cron job to execute.

My question though is how can I amend the file name to include the numerical month and year?

So I want the monthly access log files to look like...
mydomain.com-2007-01.gz
mydomain.com-2007-02.gz
mydomain.com-2007-03.gz
mydomain.com-2007-04.gz

Here is the basic code. I don't know if this will generate a log file that grows so long as you leave the original daily access logs though? If I get this working I'm so going to post a whole page on my site dedicated to conquering this issue alone!

Code: Select all

zcat access.log.*.gz > monthlylog.txt
gzip monthlylog.txt
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

No one here has any idea how to use a Unix console?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

You could have a daily cron job that creates tomorrow's log file as a link to it's monthly log. That way you won't need to combine them, the logger will simply append to the monthly one through the link. You could use the same cron to tear down the link from yesterday to keep things tidy.

Does that make any sense?

I could break out my Bash book if you want code.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

That would be most appreciated Kieran! Anything you think that would be constructive towards achieving this goal would be great!
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Code: Select all

# set the domain (I assume it will be looped  for many domains)
DOMAIN="domain.com"

# create tomorrow's monthly log if it doesn't exist
touch `date --date='tomorrow' +$DOMAIN-%Y-%m.log`

# create tomorrow's daily log as a link
ln -s `date --date='tomorrow' +$DOMAIN-%Y-%m.log` `date --date='tomorrow' +$DOMAIN-%Y-%m-%d.log`

# now remove yesterday's daily log file
rm `date --date='yesterday' +$DOMAIN-%Y-%m-%d.log`
Post Reply