Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: What is the best way to dump data structures to logfiles?

by radiantmatrix (Parson)
on Mar 14, 2007 at 20:13 UTC ( #604884=note: print w/replies, xml ) Need Help??

in reply to What is the best way to dump data structures to logfiles?

You really haven't given all your requirements here, so I apologize if this is a useless suggestion.

I've had success using Data::Dumper or YAML (or its XS replacement YAML::Syck) to dump structures to a file, which is then referenced in the log file. I use a sub that looks something like:

# assumes-> use File::Temp ':POSIX'; for tempnam() # assumes-> use YAML 'Dump'; (or) use YAML::Syck 'Dump'; for Dump() sub dump { my $message = shift; my $structures = scalar @_; # yes, 'scalar' is redudant, but reada +ble # create file in curdir with prefix 'dump.' my ($dump_h, $dump_n) = tempnam('.','dump.'); print $dump_h, Dump($_) for @_; close $dump_h; $message.=sprintf '(%d dumped to "%s")', $structures, $dump_n; }

And would be called something like:

$logger->fatal( dump('Unable to foo, dumping object $foo_doer.', $foo_ +doer) );

The log line would look something like:

20070314 15:09 FATAL: Unable to foo, dumping object $foo_doer.(1 dumpe +d to "dump.foobar1")

And the file "dump.foobar1" would contain the $foo_doer serialization.

You can equally use Data::Dumper or any other serialization module you prefer using this model.

Ramblings and references
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://604884]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2022-08-09 08:43 GMT
Find Nodes?
    Voting Booth?

    No recent polls found