Sign in to PrePAN

Log::Redirect Log messages to a file by redirecting STDOUT or STDERR



            use Log::Redirect;

            my $log = Log::Redirect->new("logfile.log");    ## Creates a new log file to write to
            print "INFO: Starting program.\n";              ## This goes into logfile.log
            undef $log;                                     ## This restores STDOUT and STDERR.

            my $log2 = Log::Redirect->new("newlog.log",
                                            verbose => 1);  ## Every print statement is printed both to the screen and newlog.log.
            print "INFO: Stating a new log file.\n";        ## This goes into newlog.log and the screen simultaneously
            warn "WARNING: Something happened...\n";        ## This also goes into newlog.log and STDERR

            my $log3 = Log::Redirect->new("autolog.log");   ## Now redirect to another file called autolog.log
            print "INFO: Interrupted by autolog.log.\n";
            undef $log3                                     ## Automatically remembers and reverts to previous redirection

            print "INFO: Done writing to autolog.log\n";    ## Appears in newlog.log as well as the screen
            undef $log2                                     ## Restore STDOUT and STDERR back to the screen

            my $log4 = Log::Redirect->new('subroutine.log');
            &my_subroutine();                               ## Any print statements in my_subroutine will be in subroutine.log
            undef $log4                                     ## Restores back STDOUT and STDER
            sub my_subroutine {
                    print "INFO: This is a routine test.\n";


A perl module that redirects output log messages into specific log files using simply the print()',printf()', and warn()' built-in Perl sub-routines. The interface allows a programmer to simply write the messages he wants to produce usingprint()', printf' orwarn', and let a different portion of the code take care of where it gets written to. This has a couple of advantages:

1. It allows the program to be independent of the names for the
logfiles, and allows the user to choose his names
2. It is very easy to make a message appear on screen as well as in a
file, without repeating the print statement - this is done using the
`verbose' option.
3. No special logging functions need to be used. The standard `print',
`printf' and `warn' functions do the job.
4. Works correctly even if used with `fork()'.


Writing logs have become exceedingly hard, when in reality it is a
rather simple task: tell the user what is going on. Most modules allow
one to write log messages with message levels and stuff. But sometimes
they are not useful, and sometimes the modules just do not cover all the
message levels that one may want. The end result is that a programmer
has to take special care to choose a good logging interface and that
takes time away from his core work - writing the algorithm.

`Log::Redirect' allows a programmer to think of the algorithm first, and
log messages later. It also allows for a very easy way to display the
log on screen and simultaneously writing to the file. `Log::Redirect'
also writes to the disk instantly and doesn't wait for the buffer to be
full before it begins writing something.



Creates a new object of this class. It requires a filename as a required
argument. In addition, it can accept an optional parameter

        my $mylog = Log::Redirect->new('mylog.log');
        my $verboselog = Log::Redirect->new('verbose.log', verbose => 1);


None by default.


This makes an easy API for a logging system.
But consider implementing this just as an API over an existing Log:: backend.

Also, think about handling "die" for fatal errors.
And using warn for logging application warnings has an issue: you won't be able to distinguish Perl buil-in warnings.
I do not see the benefit over something like log4perl's easy mode: Log::Log4perl qw(:easy)

I also do no like the idea of changing the behavior of built ins (if this is what you do).

How is this different than adding IO redirects on the command line?
I can see how it might be handy for debugging.

I would be wary of "handling die" and dolmen's caveat about warnings should be taken into account.
A better name might be IO::Redirect
Your module does not redirect logs :-)

Please sign up to post a review.