preaction / Log-Any

Simple, fast Perl logging API compatible with any logging system
Other
13 stars 19 forks source link

Import logging routines into the current namespace #50

Open preaction opened 7 years ago

preaction commented 7 years ago

Log::Log4perl has an :easy mode to help people easily get started with Log4perl. It has some caveats, but it is a lot easier to do:

use Log::Log4perl qw( :easy );
DEBUG "I'm debugging!";

Than it is to do:

use Log::Log4perl;
my $LOG = Log::Log4perl->get_logger();
$LOG->debug( "I'm debugging!" );

Log::Any is a bit easier still: We already have a shortcut for getting a logger:

use Log::Any qw( $LOG );
$LOG->debug( "I'm debugging!" );
die $LOG->ERROR( "Unable to do the thing: %s", $! );

But we have to remember about $LOG always. Instead, we could totally, on-demand, build a bunch of subroutines in the importing namespace that set the right category and use the formatting versions of the log methods (debugf() instead of just debug()) and return the formatted log message as expected. This way, using Log::Any requires less knowledge:

use Log::Any qw( :easy );
DEBUG "I'm debugging!";
die ERROR "Unable to do the thing: %s", $!;

We need to make sure this has the same (or similar) performance profile as the other ways, or else nobody will use it. We also likely need to do two flavors: One for the log4perl log levels, and one for the syslog log levels.

We also need to be careful about just blindly using the package name as a category, as Log4perl also needs: https://metacpan.org/pod/distribution/Log-Log4perl/lib/Log/Log4perl.pm#Pitfalls-with-Categories.