
davorg
Thaumaturge
/ Moderator
Jun 3, 2005, 7:06 AM
Post #11 of 16
(1861 views)
|
I assume that you're talking about a daemon process that is running constantly. You want to create a new log file for every day. Something like this should work (untested).
#!/usr/bin/perl use strict; use warnings; use POSIX 'strftime'; my $date_fmt = '%d-%m-%Y'; my $curr_date = strftime($date_fmt, localtime); my $file = '/some/path/to/your/logfile'; open LOGFILE, '>', "$file-$curr_date.log" or die $!; while (1) { # do whatever your program needs to do, writing log data # to LOGFILE # Check the date at the end of each processing loop my $this_date = strftime($date_fmt, localtime); # If the date has changed... if ($this_date eq $curr_date) { # change the current date variable... $curr_date = $this_date; # close the existing logfile... close LOGFILE; # and open a new one open LOGFILE, '>', "$file-$curr_date.log" or die $!; } } -- Dave Cross, Perl Hacker, Trainer and Writer http://www.dave.org.uk/ Get more help at Perl Monks
|