Nicer dumping of data structure in Log::Any

When you log data structures with Log::Any, e.g.:

% DEBUG=1 perl -MLog::Any::App='$log' -e '... $log->debugf("ccls=%s", $ccls); ...'

do you often see something like this?

[38] ccls=[{fmt => ['integer','integers'],text => ['integer','integers'],type => 'noun'},{expr => 1,fmt => 
'default value %s',text => 'default value 1',type => 'clause'},{expr => 1,fmt => '%(modal_verb)s be divisi
ble by %s',multi => 1,text => 'must be divisible by 2 and 3',type => 'clause'},{expr => 1,fmt => '%(modal_
verb)s be at least %s',multi => 1,text => 'must be at least 1',type => 'clause'},{expr => 1,fmt => '%(moda
l_verb)s be at most %s',multi => 1,text => 'must be at most 100',type => 'clause'}]

Log::Any uses Data::Dumper with Indent setting of 0. This is compact, but hard to read. Tired of scanning hash keys with my fingers or mouse pointer, I finally spent about 5 minutes creating two patch modules to fix this annoyance. The first one is Log::Any::Adapter::Core::Patch::SetDumperIndent to let you set indent level, and the second is Log::Any::Adapter::Core::Patch::UseDataDump to use Data::Dump instead of Data::Dumper.

For convenience when typing these module aliases are provided: Log::Any::{DD,DI1,DI2,DI3}.

Now you can conveniently do this:

% DEBUG=1 perl -MLog::Any::App='$log' -MLog::Any::DD -e'... $log->debugf("ccls=%s", $ccls); ...'

and see something like:

[38] ccls=[
  {
    fmt  => ["integer", "integers"],
    text => ["integer", "integers"],
    type => "noun",
  },
  {
    expr => 1,
    fmt  => "default value %s",
    text => "default value 1",
    type => "clause",
  },
  {
    expr  => 1,
    fmt   => "%(modal_verb)s be divisible by %s",
    multi => 1,
    text  => "must be divisible by 2 and 3",
    type  => "clause",
  },
  {
    expr  => 1,
    fmt   => "%(modal_verb)s be at least %s",
    multi => 1,
    text  => "must be at least 1",
    type  => "clause",
  },
  {
    expr  => 1,
    fmt   => "%(modal_verb)s be at most %s",
    multi => 1,
    text  => "must be at most 100",
    type  => "clause",
  },
]

Small stuff, but my eyes have already thanked me for it.

I'm considering making Log::Any::App supporting or picking a nice default for this.

5 Comments

This is very nice.

There probably isn't any advantage over the Data::Dump format, which is already quite nice, but you could also try it with Data::Dumper::Perltidy.

Hi Steven

I would have patched Log::Any to use Data::Dumper::Concise.

Leave a comment

About Steven Haryanto

user-pic A programmer (mostly Perl 5 nowadays).