August 2017 Archives

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.

About preaction

user-pic