Apr
16

Output Logging Routine

By

This code will allow logging into a file and optionally, to the screen as well. This will create the file if necessary.


sub mlog {
    my ($msg) = @_;
    open (FH, ">> /tmp/logfile.log")
        or croak "error opening logfile: $!\n";

    my $timestamp = localtime;
    print FH "$timestamp: $msg\n";
    close FH;

    print "$timestamp: $msg\n";    # also log to the screen
}

And to use this in your code:


mlog("This is a test log");
Categories : Perl

Leave a Comment

You must be logged in to post a comment.