fREWdiculous!
7 Dec
I just released Log::Sprintf and Log::Structured to CPAN. They are both very simple modules, but they allow some powerful stuff.
Log::Sprintf will convert a hashref into a string given a specification almost conformant to Log::log4perl’s log specs. The example from the SYNOPSIS is as follows:
1 2 3 4 5 6 7 8 9 10 11 |
Also it was made with subclassing in mind from the start, so it is easy to add more flags as needed.
Log::Structured is a more generic tool but arguably more powerful. All it does is generate a “simple” (easily serializable) data structure and call a coderef that you give it with the data structure. What I hope to do with that is log to standard error using Log::Sprintf, but then log to a file using newline separated JSON documents. That means I can parse the log file DEAD easily and do what I want with it. Here’s the SYNOPSIS (after Log::Sprintf-ification) for that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | use Log::Structured; use Log::Sprintf; my $formatter = Log::Sprintf->new({ format => "[%d][%F:%L][%p][%c] %m" }); my $structured_log = Log::Structured->new({ category => 'Web Server', log_category => 1, priority => 'trace', log_priority => 1, log_file => 1, log_line => 1, log_date => 1, log_event_listeners => [sub { warn $formatter->sprintf($_[1]) }, sub { open my $fh, '>>', 'log'; print {$fh} encode_json($_[1]) . "\n"; }], }); $structured_log->log_event({ message => 'Starting web server' }); $structured_log->log_event({ message => 'Oh no! The database melted!', priority => 'fatal', category => 'Core', }); |
Anyway, hope you find a handy use for these!
Leave a reply