format_event_string
my $string = Log::Fmt->format_event_string([
key1 => $value1,
key2 => $value2,
]);
Note especially that if any value to encode is a reference toareference, then String::Flogger is used
to encode the referenced value. This means you can embed, in your logfmt, a JSON dump of a structure by
passing a reference to the structure, instead of passing the structure itself.
parse_event_string
my $kv_pairs = Log::Fmt->parse_event_string($string);
Given the kind of string emitted by "format_event_string", this method returns a reference to an array of
key/value pairs.
This isn't exactly a round trip. First off, the formatting can change illegal keys by replacing
characters with question marks, or replacing empty strings with tildes. Secondly, the formatter will
expand some values like arrayrefs and hashrefs into multiple keys, but the parser will not recombined
those keys into structures. Also, there might be other asymmetric conversions. That said, the string
escaping done by the formatter should correctly reverse.
If the input string is badly formed, hunks that don't appear to be value key/value pairs will be
presented as values for the key "junk".
parse_event_string_as_hash
my $hashref = Log::Fmt->parse_event_string_as_hash($line);
This parses the given line as logfmt, then puts the key/value pairs into a hash and returns a reference
to it.
Because nothing prevents a single key from appearing more than once, you should use this with the
understanding that data could be lost. No guarantee is made of which value will be preserved.