467,081 Members | 1,225 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,081 developers. It's quick & easy.

Saving a log without deleting the previous logs?

Thekid
100+
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
  • viewed: 1063
Share:
4 Replies
eWish
Expert 512MB
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 2GB
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+
Ahh.....thanks! It was the double '>>' that I needed!
Aug 26 '08 #4
numberwhun
Expert Mod 2GB
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.

Similar topics

reply views Thread by marfi95@yahoo.com | last post: by
7 posts views Thread by ehendrikd@gmail.com | last post: by
3 posts views Thread by vb@sharedvision.com | last post: by
1 post views Thread by marfi95@yahoo.com | last post: by
8 posts views Thread by Joey Chömpff | last post: by
1 post views Thread by Jim Andersen | last post: by
14 posts views Thread by Simon | last post: by
3 posts views Thread by Andy_Khosravi@bcbsmn.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.