Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing Log files 1

Status
Not open for further replies.

venkatpavan

Programmer
Feb 18, 2006
42
0
6
SG
Good Morning all,

I'm a beginner in perl script,I wrote a script and wanted each and every failure/success points to be logged in a log file.Could somebody help me on how to log everything in a logfile.

Thanks in advance!

Thanks,
V
 
are you asking for script success/fail or each commands output success/fail? Are you using any pragmas like strict or warning? You can also looke into eval function.
 
Hello V

create your log file when the script starts, like this

open(LOG,">>logfile.txt") or die;
print LOG "Log file opened\n";
close LOG;

Each time you want to log something, open the file (for append, like above), write to and close the file. Like this

open(LOG,">>logfile.txt") or die;
print LOG "Log file entry\n";
close LOG;

Mike

 
Hi

Mike said:
Each time you want to log something, open the file (for append, like above), write to and close the file.
That can slow down the entire script considerably. Loggers usually not open & close the destination for each write. ( That is why you have to empty the log files of running applications with [tt]cat /dev/null > /log/file[/tt] instead of [tt]rm /log/file[/tt]. ) The only trick is to set autoflush, so no data vanishes accidentally and the log can be watched with [tt]tail -f /log/file[/tt]
Code:
[gray]#!/usr/bin/perl[/gray]

[gray]# open the file and set the autoflush[/gray]
[b]open[/b] LOG[teal],[/teal][green][i]'>> /log/file'[/i][/green] or [b]die[/b] $[teal]!;[/teal]
[navy]$oldsel[/navy][teal]=[/teal][b]select[/b] LOG[teal];[/teal]
$[teal]|=[/teal][purple]1[/purple][teal];[/teal]
[b]select[/b] [navy]$oldsel[/navy][teal];[/teal]
[teal]&[/teal][b]log[/b][teal]([/teal][green][i]'starting'[/i][/green][teal]);[/teal]

[gray]# convenience function to add timestamp ( maybe PID, UID, etc. ) and formatting[/gray]
[b]sub[/b] [b]log[/b][teal]()[/teal]
[teal]{[/teal]
  [b]print[/b] LOG scalar localtime[teal],[/teal][green][i]"\t@_\n"[/i][/green][teal];[/teal]
[teal]}[/teal]

[gray]# close the file before exiting[/gray]
END [teal]{[/teal]
  [teal]&[/teal][b]log[/b][teal]([/teal][green][i]'stopping'[/i][/green][teal]);[/teal]
  [b]close[/b] LOG[teal];[/teal]
[teal]}[/teal]

[gray]# then just use it[/gray]
[teal]&[/teal][b]log[/b][teal]([/teal][green][i]'something performed here'[/i][/green][teal]);[/teal]
Of course, logger classes can do much more :
[ul]
[li]process log messages differently based on their log level or sender ( for example, write to system log messages with level INFO or above generated by the login module )[/li]
[li]send the log messages to more devices ( for example, write to file messages with level DEBUG or above and send in e-mail messages with level FATAL )[/li]
[/ul]
So before taking a decision, take a look at the available logger modules on CPAN.

Feherke.
 
Speed really isn't the issue, opening and closing the log file each time will work on every Perl platform, all of them. You're right about the Unix boxes of course.

But you shouldn't be writing fast scripts anyway :) you should be writing scripts that you can read, that work and can be made to run fast *enough*.... Make it work *then* make it quick.

Mike

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top