Log::Any::App
This is a draft/RFC document.
Log::Any is great if you're writing modules. You only need to say:
use Log::Any qw($log);
and then you're off producing logs with:
$log->debug(...);
$log->warn(...);
# etc
But if you're writing scripts/applications (and thus need to "consume" or display the logs as well), it becomes a bit of a hassle. For example, if you want to display logs to the screen with Log::Dispatch, this is the incantation you need:
use Log::Any qw($log);
use Log::Any::Adapter;
use Log::Dispatch;
Log::Any::Adapter->set('Dispatch');
my $disp = Log::Dispatch->new(outputs => ["Screen", min_level=>"debug", newline=>1]);
Log::Any::Adapter->set("Dispatch", dispatcher=>$disp);
...
$log->warn(...);
which I'm sure I'll never ever remember and will just copy paste everytime.
The goal is to be able to write only this in your scripts:
use Log::Any::App qw($log);
and Log::Any::App (which currently does not exist) will take care of all the rest:
- Choose the best available adapter(s);
- Configure the adapter(s) with the best defaults, e.g. to screen, as well as to file /var/log/SCRIPTNAME.log (if running as root) or ~/SCRIPTNAME.log (if running as user). The defaults can of course be changed via configuration;
- Pick configuration from various sources, like environment variables (e.g. turning level to debug if DEBUG is set to true), command line options (e.g. log level from --log_level/--log-level/--debug/--verbose/etc, as well as detecting result from Getopt::Long or App::Options so we can avoid parsing by ourselves)
I think the main challenge is arranging a set of defaults that are acceptable and comfortable for a lot of people, and working together nicely with available modules like adapter modules (Log::Any::Adapter::Dispatch, Log::Any::Adapter::Log4perl), command line parsing modules, configuration modules, etc.
And later should you refactor your script into modules, the logging part can be left untouched as they already use the Log::Any framework.
Modules to look at: Log::Dispatchouli.
I simply use a logging config file. So it's really just a few lines of code:
use Log::Log4perl;
use Log::Any qw($log);
Log::Log4perl::init('logging.conf');
Log::Any::Adapter->set('Log::Log4perl');
And for new projects just copy the logging.conf and adjust to my varying needs.
Of course something like this would be preferred:
use Log::Any::App qw(logger => $log, config => 'logging.conf');