I need a solution which prints STDOUT and STDERR both to the screen and to a file at the same time.
So, here is one solution. Maybe it is too complicated, but your question smells like logging anyhow.

use strict; use Log::Log4perl qw(get_logger); # Define configuration for file test.log, STDERR, and STDOUT my $conf = q( log4perl.logger = ALL, FileApp, Screen +STDERR, ScreenSTDOUT log4perl.appender.FileApp = Log::Log4perl::Appen +der::File log4perl.appender.FileApp.filename = test.log log4perl.appender.FileApp.layout = PatternLayout log4perl.appender.FileApp.layout.ConversionPattern = %m%n log4perl.appender.ScreenSTDERR = Log::Log4perl::Appen +der::Screen log4perl.appender.ScreenSTDERR.stderr = 1 log4perl.appender.ScreenSTDERR.layout = PatternLayout log4perl.appender.FcreenSTDERR.layout.ConversionPattern = %m%n log4perl.appender.ScreenSTDOUT = Log::Log4perl::Appen +der::Screen log4perl.appender.ScreenSTDOUT.stderr = 0 log4perl.appender.ScreenSTDOUT.layout = PatternLayout log4perl.appender.ScreenSTDOUT.layout.ConversionPattern = %m%n ); # Initialize logging behaviour Log::Log4perl->init( \$conf ); # Obtain a logger instance my $logger = get_logger("Chess::GUI"); $logger->info("This is going to test.log, STDERR, and STDOUT.");

Log::Log4perl is quite powerful. You could try experimenting with different conversion patterns as explained in Log::Log4perl::Layout::PatternLayout.


In reply to Re: Both print and redirect STDOUT and STDERR by Perlbotics
in thread Both print and redirect STDOUT and STDERR by chessgui

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.