File Coverage

blib/lib/Log/Saftpresse/Output/JSON.pm
Criterion Covered Total %
statement 3 27 11.1
branch 0 10 0.0
condition 0 3 0.0
subroutine 1 4 25.0
pod 0 2 0.0
total 4 46 8.7


line stmt bran cond sub pod time code
1             package Log::Saftpresse::Output::JSON;
2              
3 1     1   950 use Moose;
  1         2  
  1         6  
4              
5             # ABSTRACT: plugin to dump events to in JSON to stdout
6             our $VERSION = '1.5'; # VERSION
7              
8             extends 'Log::Saftpresse::Output';
9              
10             has 'json' => (
11             is => 'ro', isa => 'JSON', lazy => 1,
12             default => sub {
13             my $j = JSON->new;
14             $j->utf8(1); $j->pretty(1); $j->allow_blessed(1);
15             return $j;
16             },
17             );
18              
19              
20             sub output {
21 0     0 0   my ( $self, @events ) = @_;
22              
23 0           foreach my $event (@events) {
24 0           my %output = %$event;
25 0 0 0       if( defined $output{'time'} &&
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     0     my $self = shift;
38 0 0         if( defined $self->{'_backend'} ) {
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 0         if( ! $@ ) {
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     0 0   my ( $self, $data ) = @_;
53              
54 0           my $backend = $self->_backend;
55              
56 0 0         if( $backend eq 'JSON::Color' ) {
    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;
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