PrePAN

Sign in to PrePAN

Logging::Trivial The simplest useful logging module

Good

Synopsis

 use Logging::trivial;
 Logging::trivial::setlevel("DEBUG");      # Only print messages at this level and above. Global setting.
 Logging::trivial::setLogFile("myScript.log"); # Print detailed logs to this file as well

 INFO "I'm giving an informative message"; # Methods write "LEVEL: thing" to STDERR
 DEBUG "Variable name" => {foo=>"bar"};    # DEBUG uses Data::Dumper to prettify a variable...
 DETAIL "A large variable I sometimes need" => [1,2,3,4,5]; # ...and so does DETAIL
 doSomething() if DEBUG;                   # Methods return 1 if that level is appropriate
 WARN "Careful how you do that, $user!";
 ERROR "Die with stack trace" if $someAwfulThingHappened;

Description

EDIT

Log4Perl's easy-mode is the best solution to this problem.


I wanted to be able to write

INFO "blah"; WARN "foo";

to output:

INFO: "blah" WARN: "foo"

and so on, and have logging to a file, and found the following modules:

Name            | Why I didn't want to use it            
--------------- | -------------------------------------- 
Log::Simplest | Ugly interface & not enough levels     
Log::Trivial  | OO interface is overkill for this task 
Log::Log4Perl | Too complicated!                       
Carp          | Not enough levels                      

So I wrote my own :)

The problems are:

  • The namespace is cluttered and I don't think my name is that useful
  • Exporting 5 functions by default is quite a few, but you wouldn't use this module if you didn't want them
  • I know that this goes against the 'don't reinvent the wheel' doctrine.

Comments

I will recommend you to check following modules out.

* https://metacpan.org/pod/Log::Minimal
* https://metacpan.org/pod/Log::Minimal::Instance
Log::Log4Perl has an easy interface, just search the doc for "easy".
anonymouse
Anonymous
As you pointed out there's already a Log::Trivial, so naming it Logging::Trivial is not a good idea.
The minute you're adding some logging to modules (instead of scripts/applications), you'll want Log::Any. Perhaps you can base your module on Log::Any (e.g. implementing it as a Log::Any::Adapter?) It has enough levels for you :)
This is probably not the answer you were hoping for, but I'd also advise going with Log::Any. CPAN is full of logging modules. Before I knew about them, I even wrote one myself. However, now I use Log::Any for everything. It is lightweight and its clean interface makes it easy to collect logs. It provides data dumping via 'f' methods (debugf,infof,...) and you can check your level at runtime with is_ methods (is_debug,is_info). You can initialize dispatching quickly with Log::Log4Perl's :easy and easy_init and after that you can get as complex as your use cases demand. The real beauty of Log::Any is that it allows you to separate *collecting* logs from *dispatching* them. This means long-lasting, maintainable code. I also recommend this great article on the benefits of scalable logging: http://www.perl.com/pub/2002/09/11/log4perl.html. Example usage of Log::Any with Log4perl dispatching follows:
> cat test.pl
#!/usr/bin/perl
use strict; use warnings;
use Log::Any qw($log);
use Log::Any::Adapter;
# Send all logs to Log::Log4perl
Log::Any::Adapter->set('Log4perl');
# Initialize logging via Log4Perl
use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init($DEBUG);
# Test out some Log::Any methods
$log->info("hello");
$log->debugf("%s",{a=>1,b=>[2,3,4]});
die if $log->is_warn;
> perl test.pl
2015/02/08 03:45:43 hello
2015/02/08 03:45:43 {a => 1,b => [2,3,4]}
Died at test.pl line 16.

Please sign up to post a review.