Saftpresse/Output/JSON.pm

filter: Statementstmt 19.4% 7 / 36 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 20.0% 1 / 5 Podpod 0.0% 0 / 2 totaltotal 13.7% 8 / 58 SCAR 40.9
CC
9
Cov
14%
output
20.0
_backend
20.0
dump_json_data
12.0
CRAP 60.0
« Saftpresse/Plugin/Postfix/Utils.pm ↑ Summary Saftpresse/Output/Graphite.pm »
1 package Log::Saftpresse::Output::JSON;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: plugin to dump events to in JSON to stdout
6 1 our $VERSION = '1.5'; # VERSION
7  
8 1 extends 'Log::Saftpresse::Output';
9  
10 has 'json' => (
11 is => 'ro', isa => 'JSON', lazy => 1,
12 default => sub {
13 0 ▶ my $j = JSON->new;
Subroutine __ANON__: not called CC=1 SCAR=6.9
14 0 $j->utf8(1); $j->pretty(1); $j->allow_blessed(1);
15 0 return $j;
16 },
17 1 );
18  
19  
20 sub output {
21 0 ▶ my ( $self, @events ) = @_;
Subroutine output: not called CC=4 SCAR=30.0
Pod undocumented
22  
23 0 foreach my $event (@events) {
24 0 my %output = %$event;
25 0 ▶ if( defined $output{'time'} &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output{"time"} and ref $output{"time"} eq "Time::Piece") 0 0 0
Condition0%0/3
defined $output{"time"} and ref $output{"time"} eq "Time::Piece"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $output{"time"} and ref $output{"time"} eq "Time::Piece"
defined $output{"time"} ref $output{"time"} eq "Time::Piece"
Truth table0%0/3
A and B
A= defined $output{"time"}B= ref $output{"time"} eq "Time::Piece"
ABresult
0X0
100
111
26 ref($output{'time'}) eq 'Time::Piece' ) {
27 0 $output{'@timestamp'} = $output{'time'}->datetime;
28 0 delete $output{'time'};
29 }
30 0 $self->dump_json_data( \%output );
31 }
32  
33 0 return;
34 }
35  
36 sub _backend {
37 0 ▶ my $self = shift;
Subroutine _backend: not called CC=4 SCAR=30.0
38 0 ▶ if( defined $self->{'_backend'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_backend"}) 0 0 0
39 0 return $self->{'_backend'} ;
40 }
41 0 foreach my $module ( 'JSON::Color', 'JSON') {
42 0 my $require = "require $module;";
43 0 eval $require; ## no critic
44 0 ▶ if( ! $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
45 0 return $module;
46 }
47 }
48 0 die('could not find supported JSON output module. Install JSON::Color or JSON.');
49 }
50  
51 sub dump_json_data {
52 0 ▶ my ( $self, $data ) = @_;
Subroutine dump_json_data: not called CC=3 SCAR=24.8
Pod undocumented
53  
54 0 my $backend = $self->_backend;
55  
56 0 ▶ if( $backend eq 'JSON::Color' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($backend eq 'JSON::Color') { } 0 0 0
elsif ($backend eq 'JSON') { } 0 0 0
57 0 print JSON::Color::encode_json( $data, { pretty => 1 } )."\n";
58 } elsif( $backend eq 'JSON' ) {
59 0 print $self->json->encode( $data );
60 } else {
61 0 die("unknown JSON backend module or not defined?!");
62 }
63 0 return;
64 }
65  
66 1 1;
67  
68 __END__
69  
70 =pod
71  
72 =encoding UTF-8
73  
74 =head1 NAME
75  
76 Log::Saftpresse::Output::JSON - plugin to dump events to in JSON to stdout
77  
78 =head1 VERSION
79  
80 version 1.5
81  
82 =head1 AUTHOR
83  
84 Markus Benning <ich@markusbenning.de>
85  
86 =head1 COPYRIGHT AND LICENSE
87  
88 This software is Copyright (c) 1998 by James S. Seymour, 2015 by Markus Benning.
89  
90 This is free software, licensed under:
91  
92 The GNU General Public License, Version 2, June 1991
93  
94 =cut
« Saftpresse/Plugin/Postfix/Utils.pm ↑ Summary Saftpresse/Output/Graphite.pm »