Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How to log to different outputs from different packages

by 1nickt (Canon)
on Nov 19, 2017 at 14:33 UTC ( [id://1203781]=note: print w/replies, xml ) Need Help??


in reply to Overriding Global Variables

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.

Replies are listed 'Best First'.
Re^2: How to log to different outputs from different packages
by Mano_Man (Acolyte) on Nov 19, 2017 at 16:23 UTC
    Thanks 1nickt, looking into it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-03-28 18:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found