Response to "Our Adventures in Logging"

This is a quick response to Our Adventures in Logging on blogs.perl.org, because the comment system there is broken and hateful.

The author had three proposals, and I'll comment on each one.

LAEP 1: Pass hashref as last argument of log functions on to supporting adapters

The idea of Log::Any is that it ought to work with any backend, without having to interrogate backends for capabilities, etc., because putting that kind of logic into logging slows things down and logging needs to be lightweight.

As you discovered, customizing the proxy is the right way to turn structured data into a string to send to the backend. The docs for Log::Any::Proxy give this example:

# format with String::Flogger instead of the default
use String::Flogger;
use Log::Any '$log', formatter => sub {
    my ($cat, $lvl, @args) = @_;
    String::Flogger::flog( @args );
};

If you look at the capabilities of String::Flogger, you'll see that you can throw hashrefs at it and get JSON out. If you don't like String::Flogger, you can put in your own formatter. Plus, by serializing during the formatting step, you remain compatible with all backends, whether terminal, file, or something more custom that throws data at ElasticSearch or whatever.

LAEP 2: Expose an API to Log::Any for modules to add (localized) context data

I think this is a good idea, though perhaps not quite in the way described. The 'prefix' feature is conceptually similar. It's hard right now with formatter, but not impossible.

Something along these lines might work, but dealing properly with scope is tricky.

our $context = {};

use JSON::MaybeXS;
use Log::Any '$log', formatter => sub {
    my ($cat, $lvl, $msg, $data) = @_;
    return "$msg " . encode_json( { %$context, %$data } );
};

Getting better, general, context tracking into the proxy would be a good thing.

LAEP 3: Change the default Adapter from Null to Stderr

This already exists. See "Setting an alternate default logger":

use Log::Any '$log', default_adapter => 'Stderr';

Summary

I hope you find this useful feedback. I'm pleased to see people using Log::Any and happy to discuss. Please feel free to email if you'd like to get into more specific details.

This entry was posted in perl programming and tagged , . Bookmark the permalink. Both comments and trackbacks are currently closed.

© 2009-2017 David Golden All Rights Reserved