By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,177 Members | 976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,177 IT Pros & Developers. It's quick & easy.

Saving a log without deleting the previous logs?

Thekid
100+
P: 145
Hi, how can I get this Perl script to save the log without automatically deleting the previous logs? It only saves the new one:

Expand|Select|Wrap|Line Numbers
  1. print '> Hello Kid ' . $ENV{'USER'} . '.' . "\n";
  2. open(PLANNERLOG, '>/var/log/planner');
  3. print '> Please enter your log data here, end with a "." on a single
  4. line.' . "\n";
  5. my $LogText;
  6. print '> ';
  7. while () {
  8.        unless ($_ ne '.' . "\n") {
  9.                last;
  10.        }
  11.        $LogText .= $_;
  12.        print '> ';
  13. }
  14.  
  15. print '> Log is being saved to /var/log/planner' . "\n";
  16. $DateTime = localtime();
  17. print PLANNERLOG ' -- START OF LOG -- ' . "\n";
  18. print PLANNERLOG 'Date/Time: ' . $DateTime . "\n";
  19. print PLANNERLOG 'Log      : ' . $LogText;
  20. print PLANNERLOG ' -- END OF LOG -- ' . "\n";
  21. die('> Log saved! Now exiting.' . "\n");
  22.  
  23.  
Aug 26 '08 #1
Share this Question
Share on Google+
4 Replies


eWish
Expert 100+
P: 971
Are you wanting to append the file?

perlfaq5 - "How come when I open a file read-write it wipes it out?"

--Kevin
Aug 26 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
Read the link eWish posted. What you want is to open the file in append /create mode:

Expand|Select|Wrap|Line Numbers
  1. open(PLANNERLOG, '>>', '/var/log/planner');
note the double >> instead of single > like you had it.
Aug 26 '08 #3

Thekid
100+
P: 145
Ahh.....thanks! It was the double '>>' that I needed!
Aug 26 '08 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
Ahh.....thanks! It was the double '>>' that I needed!
My personal suggestion would be to add a date/time stamp on to the file, or even just a date stamp (for one file per day). You can do a check to see if the date matches and if not, create a new log for that day. Its a little more coding, sure, but in my opinion, worth it.

Regards,

Jeff
Aug 26 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.