Hi, you should use tools built for the purpose, for example Log::Any to produce log output, and a Log::Any::Adapter to consume (and display, or not) the log output. Then you can can control the output of your debugging as well as the levels at which information is displayed/saved or whatever.

In the example below I am using Log::Dispatch as an adapter to process the log output, since you indicated that you want output from different packages handled differently. You can use a simple adapter if all your log output should go to one place. I generally use Log::Dispatch by default, as logging needs typically grow over time.

MyFirst.pm :

package MyFirst; use strict; use warnings; use feature 'say'; use Log::Any qw/ $log /; sub do_this { say 'Hello from MyFirst'; $log->debug('This is a debug message'); } 1;
MySecond.pm :
package MySecond; use strict; use warnings; use feature 'say'; use Log::Any qw/ $log /; sub do_that { say 'Hello from MySecond'; $log->warn('This is a warning!'); } 1;
1203778.pl (or another package, whatever...) :
use strict; use warnings; use feature 'say'; use lib '.'; use MyFirst; use MySecond; use Log::Any::Adapter; use Log::Any::Adapter::Dispatch; my $logger = Log::Dispatch->new( outputs => [ [ 'Screen', min_level => 'warning' ], [ 'File', min_level => 'debug', filename => 'logfile', newline +=> 1 ], ]); Log::Any::Adapter->set( { category => qr/^My/ }, 'Dispatch', dispatcher => $logger, ); MyFirst::do_this(); MySecond::do_that(); __END__
Output:
perl 1203778.pl Hello from MyFirst Hello from MySecond This is a warning!
Log file:
$ cat logfile This is a debug message This is a warning!

You can easily create your own Log::Any::Adapter class, which you will need to do if you want timestamp, caller info, etc.

(Note that with the "category" mechanism (which defaults to the logging packages's package name) you can control which output goes to which destination, including to /dev/null if you really want to throw it away. I.e. you could forgo the convenience of Log::Dispatch, create multiple Log::Any::Adapter objects, and route your log data using the categories. Here I used a regexp to route output from all matching packages to the adapter, and let Log::Dispatch handle the routing.)

( For a simpler scenario, just import $log from Log::Any into your package and use use Log::Any::Adapter qw/ Stderr /; in your script, and you are done. )

Hope this helps!


The way forward always starts with a minimal test.

In reply to Re: How to log to different outputs from different packages by 1nickt
in thread Overriding Global Variables by Mano_Man

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.