942,959 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 17782
  • Perl RSS
Jan 14th, 2005
0

Log file rotation script

Expand Post »
I'm trying to find or create a Perl script that at runtime will simply take a specified logfile or logfiles, and append todays date onto the end of it's name. If possible, I would also like the option of zipping the new file up. I can then run this as a Cron job every month, and keep a nice labelled archive of logs.

I've found quite a few on the net already, but they either require compiling and some form of installation of files into areas I can't access on my host, or they're just too complex and try to do too much - my requirements are very very simple, it's really just a renaming script.

Does anyone know of something that does just this already, or do you have any code that could be adapted easily? As a last resort, could someone give me a framework or pointers on how easy it would be for an absolute beginner in Perl - I've done some basic C and VB before.

Thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TimmyRaa is offline Offline
20 posts
since Sep 2004
Jan 14th, 2005
0

Re: Log file rotation script

I need more information, exactly on what you want... like... what you have access to all. Let me put it this way, if you want to give me the paths to the log files, and the paths to where you want them saved, I'll build it for you. I'm not 100% sure about the zipping.... I know I could tar them, but tarring individual files? I am sure I could bzip it or something..... let me know, my e-mail is available on this site by clicking my name, and following the menus.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jan 18th, 2005
0

Re: Log file rotation script

Looks simple. If I understand correctly, you just have to rename the file to todays date and also compress it.
Reputation Points: 10
Solved Threads: 0
Light Poster
YUPAPA is offline Offline
42 posts
since Dec 2004
Jan 18th, 2005
0

Re: Log file rotation script

Yes. Truth is, it's already done, and he's already using it. I coded it, and commented it line by line, so he could learn from and modify it for future needs.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
May 12th, 2005
0

Re: Log file rotation script

Someone E-mailed Me, Requesting The Source For This, So, Here is the actual script (with modified paths for privacy):

Perl Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl
  2.  
  3. # The Line Above Might Need To Be Changed To Wherever Perl is Located
  4.  
  5. #################################
  6. # Custom Defined User Variables #
  7. #################################
  8.  
  9. $logpath = "/home/sites/path2your/logs";
  10. $orgfile1 = "youraccess_log";
  11. $orgfile2 = "cabmirror.youraccess_log"; # Assuming You Have A Mirror Log
  12.  
  13. $use_rename = "true"; # Rename The Original Logfile After The Date is Added?
  14. $use_zip = "true"; # Use Zip Compression On The Renamed Logfiles?
  15. $remove_original_after_zip = "true"; # Remove The Orignal Logfile AFTER it has been zipped? # Or Have a logfile, and a zipped logfile?
  16.  
  17. ###################################
  18. # End of Custom Defined Variables #
  19. ###################################
  20.  
  21. # /* Get The Formated Date For Today */
  22. $today = &get_todays_date;
  23.  
  24. # /* If We Are Renaming The Original Log File */
  25. if ($use_rename eq "true") {
  26.  
  27. # /* Call Sub-Routine To Rename Log File */
  28. &rename_logfile;
  29.  
  30. } else {
  31.  
  32. # /* Call Sub-Routine To Xerox Log File With New Name */
  33. &xerox_logfile;
  34. }
  35.  
  36. # /* If We Are Going To Zip The Log Files */
  37. if ($use_zip eq "true") {
  38. # /* Call The Sub-Routine To Zip Logfiles of "Today" */
  39. &zip_logfiles;
  40. }
  41.  
  42. # /* Stop This Program From Running */
  43. exit;
  44.  
  45. sub get_todays_date
  46. {
  47. # /* Get The Current Date (Open As A new Thread) */
  48. open(DT, "date +\%D |");
  49.  
  50. # /* Store The Date In Variable $rawdate, and remove any new line characters */
  51. chomp($rawdate = <DT>);
  52.  
  53. # /* Close The "Date" Process */
  54. close(DT);
  55.  
  56. # /* Rip Apart The Raw Date Into Respective Parts */
  57. ($month, $day, $year) = split(/\//, $rawdate);
  58.  
  59. # /* Format The Parts Of The Date Into Our Desired Format */
  60. $good_date = "20$year-$month-$day";
  61.  
  62. # /* Return The Formated Date Back To Our Main Program From This Sub */
  63. return $good_date;
  64. }
  65.  
  66. sub rename_logfile
  67. {
  68.  
  69. # /* If The First Logfile Exists, Then Archive It, Otherswise Do Nothing */
  70. if (-e "$logpath/$orgfile1") {
  71.  
  72. # /* Rename The Original File To The New File With The Appended Date */
  73. `mv $logpath/$orgfile1 $logpath/$orgfile1.$today`;
  74. }
  75.  
  76. # /* If The Second Logfile Exists, Then Archive It, Otherwise Do Nothing */
  77. if (-e "$logpath/$orgfile2") {
  78.  
  79. # /* Rename The Original File To The New File With The Appended Date */
  80. `mv $logpath/$orgfile2 $logpath/$orgfile2.$today`;
  81. }
  82. }
  83.  
  84. sub xerox_logfile
  85. {
  86.  
  87. # /* If The First Logfile Exists, Then Archive It, Otherswise Do Nothing */
  88. if (-e "$logpath/$orgfile1") {
  89.  
  90. # /* Copy The Original File To The New File With The Appended Date */
  91. `cp $logpath/$orgfile1 $logpath/$orgfile1.$today`;
  92. }
  93.  
  94. # /* If The Second Logfile Exists, Then Archive It, Otherwise Do Nothing */
  95. if (-e "$logpath/$orgfile2") {
  96.  
  97. # /* Copy The Original File To The New File With The Appended Date */
  98. `cp $logpath/$orgfile2 $logpath/$orgfile2.$today`;
  99. }
  100.  
  101. }
  102.  
  103. sub zip_logfiles
  104. {
  105.  
  106. # /* If The Logfile With Appended Date Exists */
  107. if (-e "$logpath/$orgfile1.$today") {
  108.  
  109. # /* Add The Logfile To It's Own Zip Acrchives */
  110. `zip $logpath/$orgfile1.$today.zip $logpath/$orgfile1.$today`;
  111. }
  112.  
  113. # /* If The Logfile (With Appended Date) Exists */
  114. if (-e "$logpath/$orgfile2.$today") {
  115.  
  116. # /* Add The Logfile To It's Own Zip Archive */
  117. `zip $logpath/$orgfile2.$today.zip $logpath/$orgfile2.$today`;
  118. }
  119.  
  120. # /* If We Are Going To Remove The Original Logfile (Not The Zip) After it's been Zipped */
  121. if ($remove_original_after_zip eq "true") {
  122.  
  123. # /* If The First Logfile (Uncompressed) Exists */
  124. if (-e "$logpath/$orgfile1.$today") {
  125.  
  126. # /* Then Delete The File (The Uncompressed Logfile, Not The Zip) */
  127. unlink "$logpath/$orgfile1.$today";
  128.  
  129. }
  130.  
  131. # /* If The Second Logfile (Uncompressed) Exists */
  132. if (-e "$logpath/$orgfile2.$today") {
  133.  
  134. # /* Then Delete The File (The Uncompressed Logfile, Not The Zip) */
  135. unlink "$logpath/$orgfile2.$today";
  136.  
  137. }
  138. }
  139. }
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
May 12th, 2005
0

Re: Log file rotation script

Thank You for posting.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sretech is offline Offline
1 posts
since May 2005
Jun 8th, 2005
0

Re: Log file rotation script

greetings,

i know this is a perl forum, but we we rotate logs at my job using a ksh script on a cron every night at 11:59 pm:

[PHP]#!/bin/ksh
DATE=`date +%Y%m%d`
FILES=`find /home/shared/logs/ \( -name \*.start -o -name \*.log \)`
for logfile in $FILES
do
newlogfile=$logfile.$DATE
CMD="cp $logfile $newlogfile"
echo $CMD
$CMD
cat /dev/null > $logfile
rotated_logs="$rotated_logs $newlogfile"
done
for newlogfile in $rotated_logs
do
nice -20 /usr/bin/gzip -9 $newlogfile
done[/PHP]

it appends the date to the end of the file before zipping. we also run another script afterwards to remove logs older than 2 weeks:

[PHP]find /home/shared/logs/ \( -name \*.start.\* -o -name \*.log.????????\* -o -name \*start-\* -o -name \*.log.\* -o -name \*.log-\* -o -name \*.log????? -o -name \*.log.????????.gz \) -mtime +14 -exec rm -f {} \;[/PHP]

good luck!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
optomystique is offline Offline
6 posts
since Jun 2005
Jun 8th, 2005
0

Re: Log file rotation script

Yes, It Is A Perl Forum.... But Nice Script.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jun 8th, 2005
0

Re: Log file rotation script

Quote originally posted by Comatose ...
Yes, It Is A Perl Forum.... But Nice Script.
thanks... sorry about posting non-perl code :o
Reputation Points: 10
Solved Threads: 0
Newbie Poster
optomystique is offline Offline
6 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Perl Whitespaces
Next Thread in Perl Forum Timeline: Finding a number in a line





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC