Zonemaster/Engine/Logger.pm untested

filter: Statementstmt 0.0% 0 / 121 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 211 CC 15 SCAR 31.2
CC
15
Cov
0%
_check_filter
72.0
add
20.0
json
12.0
CRAP 22.5
1 package Zonemaster::Engine::Logger;
2  
3 use v5.16.0;
4 use warnings;
5  
6 use version; our $VERSION = version->declare("v1.0.8");
7  
8 use Class::Accessor "antlers";
9  
10 use Carp qw( confess );
11 use Data::Dumper;
12 use JSON::PP;
13 use List::MoreUtils qw[none any];
14 use Scalar::Util qw[blessed];
15 use Zonemaster::Engine::Profile;
16 use Zonemaster::Engine::Logger::Entry;
17  
18 our $TEST_CASE_NAME = 'Unspecified';
19 our $MODULE_NAME = 'System';
20  
21 has 'entries' => (
22 is => 'ro',
23 isa => 'ArrayRef[Zonemaster::Engine::Logger::Entry]',
24 );
25 has 'callback' => (
26 is => 'rw',
27 isa => 'CodeRef',
28 );
29  
30 my $logfilter;
31  
32 sub new {
33 my $proto = shift;
34 confess "must be called without arguments"
35 if scalar( @_ ) != 0;
36  
37 my $class = ref $proto || $proto;
38 return Class::Accessor::new( $class, { entries => [] } );
39 }
40  
41 sub add {
42 my ( $self, $tag, $argref, $module, $testcase ) = @_;
43  
44 $module //= $MODULE_NAME;
45 $testcase //= $TEST_CASE_NAME;
46  
47 my $new =
48 Zonemaster::Engine::Logger::Entry->new( { tag => uc( $tag ), args => $argref, testcase => $testcase, module => $module } );
49 $self->_check_filter( $new );
50 push @{ $self->entries }, $new;
51  
52 if ( $self->callback and ref( $self->callback ) eq 'CODE' ) {
53 eval { $self->callback->( $new ) };
54 if ( $@ ) {
55 my $err = $@;
56 if ( blessed( $err ) and $err->isa( "Zonemaster::Engine::Exception" ) ) {
57 die $err;
58 }
59 else {
60 $self->callback( undef );
61 $self->add( LOGGER_CALLBACK_ERROR => { exception => $err } );
62 }
63 }
64 }
65  
66 return $new;
67 } ## end sub add
68  
69 sub _check_filter {
70 my ( $self, $entry ) = @_;
71  
72 if ( ! defined $logfilter ) {
73 $logfilter = Zonemaster::Engine::Profile->effective->get(q{logfilter});
74 }
75  
76 if ( $logfilter ) {
77 if ( $logfilter->{ uc $entry->module } ) {
78 my $match = 0;
79 foreach my $rule ( @{$logfilter->{ uc $entry->module }{ $entry->tag }} ) {
80 foreach my $key ( keys %{ $rule->{when} } ) {
81 my $cond = $rule->{when}{$key};
82 if ( ref( $cond ) and ref( $cond ) eq 'ARRAY' ) {
83 if ( any { $_ eq $entry->args->{$key} } @$cond ) {
84 $match = 1;
85 } else {
86 $match = 0;
87 last;
88 }
89 }
90 else {
91 if ( $cond eq $entry->args->{$key} ) {
92 $match = 1;
93 } else {
94 $match = 0;
95 last;
96 }
97 }
98 }
99 if ( $match ) {
100 $entry->_set_level( $rule->{set} );
101 last;
102 }
103 }
104 }
105 }
106 return;
107 } ## end sub _check_filter
108  
109 sub start_time_now {
110 Zonemaster::Engine::Logger::Entry->start_time_now();
111 return;
112 }
113  
114 sub reset_config {
115 $logfilter = undef;
116 Zonemaster::Engine::Logger::Entry->reset_config();
117 return;
118 }
119  
120 sub clear_history {
121 my ( $self ) = @_;
122  
123 my $r = $self->entries;
124 splice @$r, 0, scalar( @$r );
125  
126 return;
127 }
128  
129 # get the max level from a log, return as a string
130 sub get_max_level {
131 my ( $self ) = @_;
132  
133 my %levels = reverse Zonemaster::Engine::Logger::Entry->levels();
134 my $level = 0;
135  
136 foreach ( @{ $self->entries } ) {
137 $level = $_->numeric_level if $_->numeric_level > $level;
138 }
139  
140 return $levels{$level};
141 }
142  
143 sub json {
144 my ( $self, $min_level ) = @_;
145 my $json = JSON::PP->new->allow_blessed->convert_blessed->canonical;
146 my %numeric = Zonemaster::Engine::Logger::Entry->levels();
147  
148 my @msg = @{ $self->entries };
149  
150 if ( $min_level and defined $numeric{ uc( $min_level ) } ) {
151 @msg = grep { $_->numeric_level >= $numeric{ uc( $min_level ) } } @msg;
152 }
153  
154 my @out;
155 foreach my $m ( @msg ) {
156 my %r;
157 $r{timestamp} = $m->timestamp;
158 $r{module} = $m->module;
159 $r{testcase} = $m->testcase;
160 $r{tag} = $m->tag;
161 $r{level} = $m->level;
162 $r{args} = $m->args if $m->args;
163  
164 push @out, \%r;
165 }
166  
167 return $json->encode( \@out );
168 } ## end sub json
169  
170 1;
171  
172 =head1 NAME
173  
174 Zonemaster::Engine::Logger - class that holds L<Zonemaster::Engine::Logger::Entry> objects.
175  
176 =head1 SYNOPSIS
177  
178 my $logger = Zonemaster::Engine::Logger->new;
179 $logger->add( TAG => {some => 'arguments'});
180  
181 =head1 CONSTRUCTORS
182  
183 =over
184  
185 =item new
186  
187 Construct a new object.
188  
189 my $logger = Zonemaster::Engine::Logger->new;
190  
191 =back
192  
193 =head1 ATTRIBUTES
194  
195 =over
196  
197 =item entries
198  
199 A reference to an array holding L<Zonemaster::Engine::Logger::Entry> objects.
200  
201 =item callback($coderef)
202  
203 If this attribute is set, the given code reference will be called every time a
204 log entry is added. The referenced code will be called with the newly created
205 entry as its single argument. The return value of the called code is ignored.
206  
207 If the called code throws an exception, and the exception is not an object of
208 class L<Zonemaster::Engine::Exception> (or a subclass of it), the exception will be
209 logged as a system message at default level C<CRITICAL> and the callback
210 attribute will be cleared.
211  
212 If an exception that is of (sub)class L<Zonemaster::Engine::Exception> is called, the
213 exception will simply be rethrown until it reaches the code that started the
214 test run that logged the message.
215  
216 =back
217  
218 =head1 METHODS
219  
220 =over
221  
222 =item add($tag, $argref, $module, $testcase)
223  
224 Adds an entry with the given tag and arguments to the logger object.
225  
226 C<$module> is optional and will default to
227 C<$Zonemaster::Engine::Logger::MODULE_NAME> if not set.
228  
229 C<$testcase> is optional and will default to
230 C<$Zonemaster::Engine::Logger::TEST_CASE_NAME> if not set.
231  
232 The variables C<$Zonemaster::Engine::Logger::MODULE_NAME> and
233 C<$Zonemaster::Engine::Logger::TEST_CASE_NAME> can be dynamically set to
234 change the default module ("System") or test case name ("Unspecified").
235  
236 =item json([$level])
237  
238 Returns a JSON-formatted string with all the stored log entries. If an argument
239 is given and is a known severity level, only messages with at least that level
240 will be included.
241  
242 =item get_max_level
243  
244 Returns the maximum log level from the entire log as the level string.
245  
246 =back
247  
248 =head1 CLASS METHODS
249  
250 =over
251  
252 =item start_time_now()
253  
254 Set the logger's start time to the current time.
255  
256 =item clear_history()
257  
258 Remove all known log entries.
259  
260 =item reset_config()
261  
262 Clear the test level cached configuration.
263  
264 =back
265  
266 =head1 SUBROUTINES
267  
268 =over
269  
270 =item _check_filter($entry)
271  
272 Apply the C<logfilter> defined rules to the entry. See
273 L<Zonemaster::Engine::Profile/"logfilter">.
274  
275 =back
276  
277 =cut