Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Creating log files for errors and warnings

by tej (Scribe)
on Aug 27, 2010 at 06:31 UTC ( [id://857618]=perlquestion: print w/replies, xml ) Need Help??

tej has asked for the wisdom of the Perl Monks concerning the following question:

I want to create a log file for errors and some other data while my script is running.

If i am using warn for error handeling how can i print those warnings in log file (.txt file)

If its not possible with warn how can i do it?

Thank you

  • Comment on Creating log files for errors and warnings

Replies are listed 'Best First'.
Re: Creating log files for errors and warnings
by stevieb (Canon) on Aug 27, 2010 at 07:21 UTC

    What have you tried?:

    #!/usr/bin/perl use warnings; use strict; open my $fh, ">", 'logfile.txt' or die "Can't open the fscking file: $!"; my $need_work = 10; my $i_tried = 0; while ( $need_work > $i_tried ) { $i_tried++; my $statement = "I've tried $i_tried things as a test\n"; print $fh $statement; } __END__
    % cat logfile.txt I've tried 1 things as a test I've tried 2 things as a test I've tried 3 things as a test I've tried 4 things as a test I've tried 5 things as a test I've tried 6 things as a test I've tried 7 things as a test I've tried 8 things as a test I've tried 9 things as a test I've tried 10 things as a test
    Steve
Re: Creating log files for errors and warnings
by Khen1950fx (Canon) on Aug 27, 2010 at 07:32 UTC
    On a very simple level, open STDOUT for "some other data' and open STDERR for errors.
    #!/usr/bin/perl use strict; use warnings; use diagnostics; open(STDOUT, '>', 'results.log') or die "Can't open log"; open(STDERR, '>', 'results_error.log') or die "Can't open log"; system("pmpath ExtUtils::MakeMaker"); system("pmpath Acme::AandB");
Re: Creating log files for errors and warnings
by Marshall (Canon) on Aug 27, 2010 at 09:20 UTC
    When your Perl program starts, it inherits two file handles for output, STDOUT and STDERR. Both go to the console with a bit of a difference, STDOUT is buffered and STDERR is not.

    Run time warnings as well as anything that you code as warn "xy"; goes to STDERR. The default print goes to STDOUT. Many of my scripts just send their output to STDOUT (don't even open a file for output).

    To cause the STDERR output to go to the same place, this can be done with the shell, to send the STDOUT and STDERR of something.pl into the more program...
    perl something.pl 2>&1 | more
    inside something.pl, the statement $|=1; will be important to turn off buffering so that the time sequence of prints to STDOUT and STDERR will be in order.

    To cause the STDOUT and STDERR of something.pl to go to the same file: output.txt:
    perl something.pl > output.txt 2<&1

    To cause STDERR to go to some already opened file handle within a Perl program and also have this "work out right" in terms of output order is something that I hadn't done before and this took some fiddling. Below is my attempt at it and it appears to work. Normally you shouldn't have to do this! But "create a log file for errors and ...data" implies the need for something like this.

    This is an abnormal situation -- at least it is a first time for me - Normally you want the "real output" to go one place and the "error messages" to go to another place!

    #!/usr/bin/perl use warnings; use strict; use IO::Handle; open (my $fh, '>', 'logfile.txt') || die "can't open logfile.txt"; open (STDERR, ">>&=", $fh) || die "can't redirect STDERR"; $fh->autoflush(1); my $need_work = 5; my $i_tried = 0; while ( $need_work > $i_tried ) { my $a; $i_tried++; print "$a\n"; #this generates a run time warning!!!!!! warn "this is warning $i_tried"; #an explict warning print $fh "I've tried $i_tried things as a test\n"; } __END__ logfile.txt contains: Use of uninitialized value $a in concatenation (.) or string at C:\TEM +P\junk2.pl line 18. this is warning 1 at C:\TEMP\junk2.pl line 19. I've tried 1 things as a test Use of uninitialized value $a in concatenation (.) or string at C:\TEM +P\junk2.pl line 18. this is warning 2 at C:\TEMP\junk2.pl line 19. I've tried 2 things as a test Use of uninitialized value $a in concatenation (.) or string at C:\TEM +P\junk2.pl line 18. this is warning 3 at C:\TEMP\junk2.pl line 19. I've tried 3 things as a test Use of uninitialized value $a in concatenation (.) or string at C:\TEM +P\junk2.pl line 18. this is warning 4 at C:\TEMP\junk2.pl line 19. I've tried 4 things as a test Use of uninitialized value $a in concatenation (.) or string at C:\TEM +P\junk2.pl line 18. this is warning 5 at C:\TEMP\junk2.pl line 19. I've tried 5 things as a test
Re: Creating log files for errors and warnings
by stevieb (Canon) on Aug 27, 2010 at 07:50 UTC

    ...or, with a bit more complexity for `error checking'. Bit of a smartass, but it does copy/paste, and it does provide an example of named-ref style params :)

    #!/usr/bin/perl use warnings; use strict; my $need_work = 10; my $i_tried = 0; my $die_if_its_broke = 0; # bool: true is fatal while ( $need_work > $i_tried ) { $i_tried++; handle_error({ value => $i_tried, die_if_broke => $die_if_its_broke, }); print "From main(), I've tried $i_tried\n"; } sub handle_error { my $params = shift; my $value_to_check = $params->{ value }; my $death = $params->{ die_if_broke }; my $error_condition = 5; if ( $value_to_check == $error_condition ) { die "Fatality!!: $!" if $death; open my $fh, ">", 'logfile.txt' or die "Can't open the fscking file: $!"; my $error_statement = "We've hit the error threshold " . ": $error_condition!\n"; print $fh $error_statement; } }
    Steve
Re: Creating log files for errors and warnings
by vek (Prior) on Aug 27, 2010 at 21:05 UTC

    Just to take a different approach, you could also do this on the command line without any changes to your code. Take that with a pinch of salt though as you didn't give us any code to go on.

    If you want all output from your program to go into a file:

    ./yourprog > combined_file.txt 2>&1

    If you want regular data in one file, error data in another:

    ./yourprog 1> regular_data.txt 2> error_data.txt

      Thank you folks for your reply..

      I can now generate log file

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://857618]
Approved by Ratazong
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2024-04-18 10:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found