Log::Any - Now With Structured Logging

The first trial release of Log::Any with mephinet's structured and contextual logging support has been released for feedback!

These features make it easier to log structured data which will then be picked up by log parsers. Adding a single, unblessed hashref as the last argument to a log method will write that data structure as a compact entry at the end of your log string:

use Log::Any '$LOG';
use Log::Any::Adapter 'Stdout';
$LOG->info( "Hello, World", { foo => 'bar' } );

$ perl test.pl
Hello, World {foo => "bar"}

Even better, you can add values to the logging context, which will also add those values to the log string as a compact data structure. Using local will automatically remove those values at the end of the scope.

use Log::Any '$LOG';
use Log::Any::Adapter 'Stdout';
local $LOG->context->{fizz} = "buzz";
$LOG->info( "Hello, World" );

$ perl test.pl
Hello, World {fizz => "buzz"}

And you can combine the two:

use Log::Any '$LOG';
use Log::Any::Adapter 'Stdout';
local $LOG->context->{fizz} = "buzz";
$LOG->info( "Hello, World", { foo => "bar" } );

$ perl test.pl
Hello, World {foo => "bar"} {fizz => "buzz"}

Adapters get these structures directly and can decide how to most effectively use them. For example, the Log4perl Log::Any adapter would likely map the Log::Any context to the Log4perl Mapped Diagnostic Context).

This is a trial release and the features may be changed slightly for simplicity and backwards-compatibility. Please give these new features a test and let us know how you like them by commenting here, by opening a ticket on the Log::Any ticket tracker on Github, or by e-mailing me preaction@cpan.org.

4 Comments

Hi, the context feature seems like it might be quite useful. (I'm not sure I see the value of passing structured data since you can already do that with debugf(), infof(), etc...)

I wonder if there's any chance of getting it to support passing a coderef, so that the content of the string emitted could be generated at calltime. For example, it might be nice to be able to create a custom caller string using something like local $log->context->{'caller'} = sub { (caller(0))[3] ... };.

Thanks for the comprehensive reply ++

Thanks for releasing the changes as a trial! For your information: I've migrated my $work's internal Log::Any::Adapter to use structured logging and the context hash - and things are working fine!

Leave a comment

About preaction

user-pic