| 1 | package Log::Log4perl::Layout::JSON::Readable; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use parent 'Log::Log4perl::Layout::JSON'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | our $VERSION = '1.0.3'; # VERSION | |||||||||||||||||||
| 7 | # ABSTRACT: JSON layout, but some fields always come first | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 2 | ▶ | use Class::Tiny +{ | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 2 | ▶ | first_fields => sub { [qw(time pid level)] }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 2 | }; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 4 | ▶ | sub BUILDARGS { | ||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||
| 15 | 4 | my ($class, @etc) = @_; | |||||||||||||||||||
| 16 | # the parent class does not have a BUILDARGS, but it may get one | ||||||||||||||||||||
| 17 | # in the future, let's handle both cases | ||||||||||||||||||||
| 18 | 4 | ▶ | my $args = $class->maybe::next::method(@etc) || $etc[0]; | ||||||||||||||||||
|
Condition33%1/3
$class->maybe::next::method(@etc) || $etc[0]
MC/DC0%0/2
$class->maybe::next::method(@etc) || $etc[0]
$class->maybe::next::method(@etc)
$etc[0]
Truth table33%1/3
A || B
A= $class->maybe::next::method(@etc)B= $etc[0]
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 4 | ▶ | if (my $first_fields = delete $args->{first_fields}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | $args->{first_fields} = [ | ||||||||||||||||||||
| 22 | 3 | grep { length } | |||||||||||||||||||
| 23 | 3 | map { my $v = $_; $v =~ s/\s+//g; $v } | |||||||||||||||||||
| 24 | split /\s*,\s*/, | ||||||||||||||||||||
| 25 | $first_fields->{value}, | ||||||||||||||||||||
| 26 | 2 | ]; | |||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 4 | return $args; | |||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | # HACK!! the parent class C<warn>s when it sees an argument it doesn't | ||||||||||||||||||||
| 33 | # expect. To prevent that, we consume it first | ||||||||||||||||||||
| 34 | 4 | ▶ | sub BUILDALL { | ||||||||||||||||||
|
Subroutine
BUILDALL: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 35 | 4 | my ($self, $args, @etc) = @_; | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 4 | ▶ | if (my $first_fields = delete $args->{first_fields}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 2 | $self->first_fields($first_fields); | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 4 | return $self->next::method($args,@etc); | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 9 | ▶ | sub render { | ||||||||||||||||||
|
Subroutine
render: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 45 | 9 | my $self = shift; | |||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 9 | my $json = $self->SUPER::render(@_); | |||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 9 | ▶ | if (my $first_fields = $self->first_fields) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | 9 | for my $key (reverse @{$first_fields}) { | |||||||||||||||||||
| 51 | 19 | _move_field_first(\$json, $key); | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 9 | return $json; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 19 | ▶ | sub _move_field_first { | ||||||||||||||||||
|
Subroutine
_move_field_first: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 59 | 19 | my ($json_ref, $key) = @_; | |||||||||||||||||||
| 60 | # a JSON value starts with double quotes, and ends with a | ||||||||||||||||||||
| 61 | # non-backslash-escaped double quotes | ||||||||||||||||||||
| 62 | 19 | ${$json_ref} =~ s/^{(.+?),("$key":".*?(?<!\\)")/\{$2,$1/; | |||||||||||||||||||
| 63 | 19 | return; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 2 | 1; | |||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | __END__ | ||||||||||||||||||||