Log::Fmt(3)

NAME
Log::Fmt - a little parser and emitter of structured log lines

VERSION
version 3.011

OVERVIEW
This library primarily exists to service Log::Dispatchouli's "log_event" methods. It converts an arrayref of key/value pairs to a string that a human can scan tolerably well, and which a machine can parse about as well. It can also do that tolerably-okay parsing for you.

PERL VERSION
This library should run on perls released even a long time ago. It should work on any version of perl released in the last five years.

Although it may work on older versions of perl, no guarantee is made that the minimum required version will not be increased. The version may be increased for any reason, and there is no promise that patches will be accepted to lower the minimum required perl.

METHODS

  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 to a reference, 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.

AUTHOR
Ricardo SIGNES <cpan@semiotic.systems>

COPYRIGHT AND LICENSE
This software is copyright (c) 2025 by Ricardo SIGNES.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.