creating log file

K

king

Suppose I have written a perl script and a lot of print commands are
there in the script.
I want to keep all the things I am printing into a test file.

Or you can say I want to create a log file.

How can I keep everything i am printing into a log file.

Regards
 
O

Owen

king said:
Suppose I have written a perl script and a lot of print commands are
there in the script.
I want to keep all the things I am printing into a test file.

Or you can say I want to create a log file.

How can I keep everything i am printing into a log file.


Open a file to write to

open (FH,">name_of_file")or die "Cant open name_of_file $!\n";
(check the difference between > and >> when opening files for writing
to)
Then print to it.

print FH "whatever you were goind to print with a normal print
statement\n";



Xemoth
 
D

David Squire

Owen said:
Open a file to write to

open (FH,">name_of_file")or die "Cant open name_of_file $!\n";
(check the difference between > and >> when opening files for writing
to)

Better (safer, and scoped):

open my $FH, '>', 'name_of_file' or die "Can't open name_of_file $!\n";
Then print to it.

print FH "whatever you were goind to print with a normal print
statement\n";

print $FH "whatever you were going to print with a normal print
statement\n";

or you could look at redirecting STDOUT, or check out the various
logging modules available at cpan.

DS
 
R

Renato

Owen said:
open (FH,">name_of_file")or die "Cant open name_of_file $!\n";
(check the difference between > and >> when opening files for writing
to)
Then print to it.

print FH "whatever you were goind to print with a normal print
statement\n";

close FH;

;)

--renato
 
C

CsB

I have a logging subroutine I commonly use. I modify it to insert
date/time stamp, pid, script name, etc. as required. Here's the skinny
version. Hope it helps

# Define log file var at the beginnig of script
$LogFile = "/my/log/dir/output.log";

# use &output_log("data"); each time you want to send something to the
log file.
&output_log("Some statement I want in the log file.");
&output_log("And another.");

# Subroutine for opening, printing output, and closing log file.
sub output_log {
my ($log_output) = @_;
open( LOG, ">>$LogFile" ) || die "Cannot open $LogFile\n $!\n";
print LOG "$log_output\n";
close(LOG) || die "WARNING: Can't close $LogFile\n $!\n";
}
 
T

Ted Zlatanov

I have a logging subroutine I commonly use. I modify it to insert
date/time stamp, pid, script name, etc. as required. ....
&output_log("And another.");

I think shorter is better for commonly used functions, so this should
be named l() or something like that.

Also, it's always a good idea to provide an optional log level:

my $level = shift @_ || 5;

in the function. Then you can turn the verbosity up or down, if you
choose to use log levels in your code, and if not everything will work
just fine. I find log levels very useful as a program grows, and it's
very little work to include them from the start.

Ted
 
C

CsB

I like the log level option. I have another version of the subroutine
where I read the value of $logging_enabled and just return if the value
is 0 (don't log).

So, I can go through my script and enable/disable logging at various
points. I'll include the $level option next time I use it. Maybe
output to different files accordingly or something. Thanks.
 
D

DJ Stunks

CsB said:
I like the log level option. I have another version of the subroutine
where I read the value of $logging_enabled and just return if the value
is 0 (don't log).

So, I can go through my script and enable/disable logging at various
points. I'll include the $level option next time I use it. Maybe
output to different files accordingly or something. Thanks.

top posting tastes bad, dude.

you, and the OP, may want to check out Log::Log4perl, the preeminent
logging module. I used to use a subroutine I rolled my self (much like
yours) until I discovered this module. it is very flexible and
powerful.

-jp
 
T

Ted Zlatanov

you, and the OP, may want to check out Log::Log4perl, the preeminent
logging module. I used to use a subroutine I rolled my self (much like
yours) until I discovered this module. it is very flexible and
powerful.

The nice thing about a consistent log()/l() function is that, when
necessary, you can make it call Log::Log4perl. I think the vast
majority of Perl programs under 300 lines don't need a module to do
their logging, but they do need a verbosity level.

The important thing is to shorten the function name, so it's easy to
type. It should be as easy or easier than print(), to tempt the
programmer :)

Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top