Zonemaster/Logger/Entry.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 105 CC 7 SCAR 17.3
CC
7
Cov
0%
_build_level
12.0
string
12.0
_build_trace
6.0
CRAP 5.6
1 package Zonemaster::Logger::Entry v1.0.0;
2  
3 use 5.14.2;
4 use Time::HiRes qw[time];
5 use JSON;
6 use Moose;
7 use Zonemaster;
8  
9 use overload '""' => \&string;
10  
11 our %numeric = (
12 DEBUG3 => -2,
13 DEBUG2 => -1,
14 DEBUG => 0,
15 INFO => 1,
16 NOTICE => 2,
17 WARNING => 3,
18 ERROR => 4,
19 CRITICAL => 5,
20 );
21  
22 our $start_time = time();
23  
24 my $json = JSON::XS->new->allow_blessed->convert_blessed->canonical;
25  
26 has 'module' => ( is => 'ro', isa => 'Str', lazy_build => 1 );
27 has 'tag' => ( is => 'ro', isa => 'Str', required => 1 );
28 has 'args' => ( is => 'ro', isa => 'Maybe[HashRef]', required => 0 );
29 has 'timestamp' => ( is => 'ro', isa => 'Num', default => sub { time() - $start_time } );
30 has 'trace' => ( is => 'ro', isa => 'ArrayRef[ArrayRef]', builder => '_build_trace' );
31 has 'level' => ( is => 'ro', isa => 'Str', lazy_build => 1, writer => '_set_level' );
32  
33 sub _build_trace {
34 my ( $self ) = @_;
35 my @trace;
36  
37 my $i = 0;
38  
39 # 0 1 2 3 4 5 6 7 8 9 10
40 # $package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash
41 while ( my @line = caller( $i++ ) ) {
42 next unless index( $line[3], 'Zonemaster' ) == 0;
43 push @trace, [ @line[ 0, 3 ] ];
44 }
45  
46 return \@trace;
47 }
48  
49 sub _build_module {
50 my ( $self ) = @_;
51  
52 foreach my $e ( @{ $self->trace } ) {
53 if ( $e->[1] eq 'Zonemaster::Util::info'
54 and $e->[0] =~ /^Zonemaster::Test::(.*)$/ )
55 {
56 return uc $1;
57 }
58 }
59  
60 return 'SYSTEM';
61 }
62  
63 sub _build_level {
64 my ( $self ) = @_;
65 my $string;
66  
67 if ( Zonemaster->config->policy->{ $self->module }{ $self->tag } ) {
68 $string = uc Zonemaster->config->policy->{ $self->module }{ $self->tag };
69 }
70 else {
71 $string = 'DEBUG';
72 }
73  
74 if ( defined $numeric{$string} ) {
75 return $string;
76 }
77 else {
78 die "Unknown level string: $string";
79 }
80 }
81  
82 sub numeric_level {
83 my ( $self ) = @_;
84  
85 return $numeric{ $self->level };
86 }
87  
88 sub levels {
89 return %numeric;
90 }
91  
92 sub string {
93 my ( $self ) = @_;
94 my $argstr = '';
95 ## no critic (TestingAndDebugging::ProhibitNoWarnings)
96 no warnings 'uninitialized';
97  
98 $argstr = join( ', ',
99 map { $_ . '=' . ( ref( $self->args->{$_} ) ? $json->encode( $self->args->{$_} ) : $self->args->{$_} ) }
100 sort keys %{ $self->args } )
101 if $self->args;
102  
103 return sprintf( '%s:%s %s', $self->module, $self->tag, $argstr );
104 }
105  
106 ###
107 ### Class method
108 ###
109  
110 sub start_time_now {
111 $start_time = time();
112 }
113  
114 no Moose;
115 __PACKAGE__->meta->make_immutable;
116  
117 1;
118  
119 =head1 NAME
120  
121 Zonemaster::Logger::Entry - module for single log entries
122  
123 =head1 SYNOPSIS
124  
125 Zonemaster->logger->add( TAG => { some => 'arguments' });
126  
127 There should never be a need to create a log entry object in isolation. They should always be associated with and created via a logger object.
128  
129 =head1 CLASS METHODS
130  
131 =over
132  
133 =item levels
134  
135 Returns a hash where the keys are log levels as strings and the corresponding values their numeric value.
136  
137 =item start_time_now()
138  
139 Set the logger's start time to the current time.
140  
141 =back
142  
143 =head1 ATTRIBUTES
144  
145 =over
146  
147 =item module
148  
149 An auto-generated identifier of the module that created the log entry. If it was generated from a module under Zonemaster::Test, it will be an
150 uppercased version of the part of the name after "Zonemaster::Test". For example, "Zonemaster::Test::Basic" gets the module identifier "BASIC". If the
151 entry was generated from anywhere else, it will get the module identifier "SYSTEM".
152  
153 =item tag
154  
155 The tag that was set when the entry was created.
156  
157 =item args
158  
159 The argument hash reference that was provided when the entry was created.
160  
161 =item timestamp
162  
163 The time after the current program started running when this entry was created. This is a floating-point value with the precision provided by
164 L<Time::HiRes>.
165  
166 =item trace
167  
168 A partial stack trace for the call that created the entry. Used to create the module tag. Almost certainly not useful for anything else.
169  
170 =back
171  
172 =head1 METHODS
173  
174 =over
175  
176 =item string
177  
178 Simple method to generate a string representation of the log entry. Overloaded to the stringification operator.
179  
180 =item numeric_level
181  
182 Returns the log level of the entry in numeric form.
183  
184 =back
185  
186 =cut