forked from timbunce/Log-Log4perl-Layout-JSON
-
Notifications
You must be signed in to change notification settings - Fork 3
Open
Description
Hi,
The module fails to produce JSON compliant output when it encounters NULL and number (integer/float) values.
One of the problems seems to be that the Log::Log4perl::Layout::PatternLayout module automatically turns undef to a string ("[undef]").
The other is probably a Perl autovivification issue; numbers are represented as strings.
Impacted version is 0.56.
Output generated:
{"level":"DEBUG","location_id":"[undef]","timestamp":"2019-03-20 12:31:45.544","pid":"25388","program":"test","message":"Database connection object created"}
{"level":"INFO","location_id":"[undef]","timestamp":"2019-03-20 16:51:36.152","pid":"25388","program":"test","message":"Signal received, quitting..."}
{"level":"INFO","location_id":"[undef]","timestamp":"2019-03-20 16:51:36.153","pid":"25388","program":"test","message":"Main program finished"}
I could manage to patch this by adding:
my $fields_to_encode = { @fields };
%$fields_to_encode = map { (defined($fields_to_encode->{$_}) and ($fields_to_encode->{$_} =~ m/^\d+$/s or $fields_to_encode->{$_} =~ m/^\d+\.\d+$/s)) ? ($_ => $fields_to_encode->{$_}+0) : ($_ => $fields_to_encode->{$_}) } (keys %$fields_to_encode);
%$fields_to_encode = map { (defined($fields_to_encode->{$_}) and $fields_to_encode->{$_} =~ m/^\[undef\]$/s) ? ($_ => undef) : ($_ => $fields_to_encode->{$_}) } (keys %$fields_to_encode);
$json = $self->codec->encode($fields_to_encode);
# $json = $self->codec->encode(+{ @fields });
which produces the expected output:
{"level":"INFO","location_id":null,"timestamp":"2019-03-20 16:51:37.137","pid":26290,"program":"test","message":"Main program started"}
{"level":"DEBUG","location_id":null,"timestamp":"2019-03-20 16:51:37.158","pid":26290,"program":"test","message":"Database connection object created"}
It only seems to be working however using Cpanel::JSON::XS, but not with JSON::XS.
Could this be looked into please?
Many thanks.
Metadata
Metadata
Assignees
Labels
No labels