| 1 | package Zonemaster::Engine::Logger::Entry; | ||
| 2 | |||
| 3 | use v5.16.0; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use version; our $VERSION = version->declare("v1.1.8"); | ||
| 7 | |||
| 8 | use Carp qw( confess ); | ||
| 9 | use Time::HiRes qw[time]; | ||
| 10 | use JSON::PP; | ||
| 11 | use Class::Accessor; | ||
| 12 | |||
| 13 | use Zonemaster::Engine::Profile; | ||
| 14 | |||
| 15 | use base qw(Class::Accessor); | ||
| 16 | |||
| 17 | use overload '""' => \&string; | ||
| 18 | |||
| 19 | our %numeric = ( | ||
| 20 | DEBUG3 => -2, | ||
| 21 | DEBUG2 => -1, | ||
| 22 | DEBUG => 0, | ||
| 23 | INFO => 1, | ||
| 24 | NOTICE => 2, | ||
| 25 | WARNING => 3, | ||
| 26 | ERROR => 4, | ||
| 27 | CRITICAL => 5, | ||
| 28 | ); | ||
| 29 | |||
| 30 | our $start_time = time(); | ||
| 31 | |||
| 32 | my $json = JSON::PP->new->allow_blessed->convert_blessed->canonical; | ||
| 33 | my $test_levels_config; | ||
| 34 | |||
| 35 | __PACKAGE__->mk_ro_accessors(qw(tag args timestamp testcase module)); | ||
| 36 | |||
| 37 | |||
| 38 | sub new { | ||
| 39 | my ( $proto, $attrs ) = @_; | ||
| 40 | # tag, testcase and module required, args optional, other built | ||
| 41 | |||
| 42 | confess "Attribute \(tag\) is required" | ||
| 43 | if !exists $attrs->{tag}; | ||
| 44 | |||
| 45 | confess "Attribute \(testcase\) is required" | ||
| 46 | if !exists $attrs->{testcase}; | ||
| 47 | |||
| 48 | confess "Attribute \(module\) is required" | ||
| 49 | if !exists $attrs->{module}; | ||
| 50 | |||
| 51 | confess "Argument must be a HASHREF: args" | ||
| 52 | if exists $attrs->{args} | ||
| 53 | && ref $attrs->{args} ne 'HASH'; | ||
| 54 | |||
| 55 | my $time = time() - $start_time; | ||
| 56 | $time =~ s/,/\./; | ||
| 57 | $attrs->{timestamp} = $time; | ||
| 58 | |||
| 59 | # lazy attributes | ||
| 60 | $attrs->{_level} = delete $attrs->{level} if exists $attrs->{level}; | ||
| 61 | |||
| 62 | my $class = ref $proto || $proto; | ||
| 63 | return Class::Accessor::new( $class, $attrs ); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub level { | ||
| 67 | my $self = shift; | ||
| 68 | |||
| 69 | # Lazy default value | ||
| 70 | if ( !exists $self->{_level} ) { | ||
| 71 | $self->{_level} = $self->_build_level(); | ||
| 72 | } | ||
| 73 | |||
| 74 | return $self->{_level} | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _build_level { | ||
| 78 | my ( $self ) = @_; | ||
| 79 | my $string; | ||
| 80 | |||
| 81 | if ( !defined $test_levels_config ) { | ||
| 82 | $test_levels_config = Zonemaster::Engine::Profile->effective->get( q{test_levels} ); | ||
| 83 | } | ||
| 84 | |||
| 85 | if ( exists $test_levels_config->{ uc $self->module }{ $self->tag } ) { | ||
| 86 | $string = uc $test_levels_config->{ uc $self->module }{ $self->tag }; | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | $string = 'DEBUG'; | ||
| 90 | } | ||
| 91 | |||
| 92 | if ( defined $numeric{$string} ) { | ||
| 93 | return $string; | ||
| 94 | } | ||
| 95 | else { | ||
| 96 | die "Unknown level string: $string"; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | sub _set_level { | ||
| 101 | my ( $self, $level ) = @_; | ||
| 102 | |||
| 103 | $self->{_level} = $level | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | sub numeric_level { | ||
| 108 | my ( $self ) = @_; | ||
| 109 | |||
| 110 | return $numeric{ $self->level }; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub levels { | ||
| 114 | return %numeric; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub string { | ||
| 118 | my ( $self ) = @_; | ||
| 119 | |||
| 120 | return sprintf( '%s%s:%s %s', $self->module, $self->testcase ? q{:} . $self->testcase : q{}, $self->tag, $self->argstr ); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub argstr { | ||
| 124 | my ( $self ) = @_; | ||
| 125 | |||
| 126 | my $argstr = q{}; | ||
| 127 | ## no critic (TestingAndDebugging::ProhibitNoWarnings) | ||
| 128 | no warnings 'uninitialized'; | ||
| 129 | |||
| 130 | if ( $self->args ) { | ||
| 131 | my $p_args = $self->printable_args; | ||
| 132 | $argstr = join( q{; }, | ||
| 133 | map { $_ . q{=} . ( ref( $p_args->{$_} ) ? $json->encode( $p_args->{$_} ) : $p_args->{$_} ) } | ||
| 134 | sort keys %{$p_args} ); | ||
| 135 | } | ||
| 136 | |||
| 137 | return $argstr; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub printable_args { | ||
| 141 | my ( $self ) = @_; | ||
| 142 | |||
| 143 | if ( $self->args ) { | ||
| 144 | my %p_args; | ||
| 145 | foreach my $key_arg ( keys %{ $self->args } ) { | ||
| 146 | if ( not ref( $self->args->{$key_arg} ) ) { | ||
| 147 | $p_args{$key_arg} = $self->args->{$key_arg}; | ||
| 148 | } | ||
| 149 | elsif ( $key_arg eq q{asn} and ref( $self->args->{$key_arg} ) eq q{ARRAY} ) { | ||
| 150 | $p_args{q{asn}} = join( q{,}, @{ $self->args->{$key_arg} } ); | ||
| 151 | } | ||
| 152 | else { | ||
| 153 | $p_args{$key_arg} = $self->args->{$key_arg}; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | return \%p_args; | ||
| 157 | } | ||
| 158 | |||
| 159 | return; | ||
| 160 | } ## end sub printable_args | ||
| 161 | |||
| 162 | ### | ||
| 163 | ### Class method | ||
| 164 | ### | ||
| 165 | |||
| 166 | sub start_time_now { | ||
| 167 | $start_time = time(); | ||
| 168 | return; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub reset_config { | ||
| 172 | undef $test_levels_config; | ||
| 173 | return; | ||
| 174 | } | ||
| 175 | |||
| 176 | 1; | ||
| 177 | |||
| 178 | =head1 NAME | ||
| 179 | |||
| 180 | Zonemaster::Engine::Logger::Entry - module for single log entries | ||
| 181 | |||
| 182 | =head1 SYNOPSIS | ||
| 183 | |||
| 184 | Zonemaster::Engine->logger->add( TAG => { some => 'arguments' }); | ||
| 185 | |||
| 186 | 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. | ||
| 187 | |||
| 188 | =head1 CLASS METHODS | ||
| 189 | |||
| 190 | =over | ||
| 191 | |||
| 192 | =item new | ||
| 193 | |||
| 194 | Construct a new object. | ||
| 195 | |||
| 196 | =item levels | ||
| 197 | |||
| 198 | Returns a hash where the keys are log levels as strings and the corresponding values their numeric value. | ||
| 199 | |||
| 200 | =item start_time_now() | ||
| 201 | |||
| 202 | Set the logger's start time to the current time. | ||
| 203 | |||
| 204 | =item reset_config() | ||
| 205 | |||
| 206 | Clear the test level cached configuration. | ||
| 207 | |||
| 208 | =back | ||
| 209 | |||
| 210 | =head1 ATTRIBUTES | ||
| 211 | |||
| 212 | =over | ||
| 213 | |||
| 214 | =item module | ||
| 215 | |||
| 216 | The name of the module associated to the entry, or "System". | ||
| 217 | |||
| 218 | =item testcase | ||
| 219 | |||
| 220 | The name of the test case which generated the entry, or "Unspecified". | ||
| 221 | |||
| 222 | =item tag | ||
| 223 | |||
| 224 | The tag that was set when the entry was created. | ||
| 225 | |||
| 226 | =item args | ||
| 227 | |||
| 228 | The argument hash reference that was provided when the entry was created. | ||
| 229 | |||
| 230 | =item timestamp | ||
| 231 | |||
| 232 | The time after the current program started running when this entry was created. This is a floating-point value with the precision provided by | ||
| 233 | L<Time::HiRes>. | ||
| 234 | |||
| 235 | =item level | ||
| 236 | |||
| 237 | The log level associated to this log entry. | ||
| 238 | |||
| 239 | =back | ||
| 240 | |||
| 241 | =head1 METHODS | ||
| 242 | |||
| 243 | =over | ||
| 244 | |||
| 245 | =item string | ||
| 246 | |||
| 247 | Simple method to generate a string representation of the log entry. Overloaded to the stringification operator. | ||
| 248 | |||
| 249 | =item argstr | ||
| 250 | |||
| 251 | Returns the string representation of the message arguments. | ||
| 252 | |||
| 253 | =item printable_args | ||
| 254 | |||
| 255 | Used to transform data from an internal/JSON representation to a "user friendly" representation one. | ||
| 256 | |||
| 257 | =item numeric_level | ||
| 258 | |||
| 259 | Returns the log level of the entry in numeric form. | ||
| 260 | |||
| 261 | =back | ||
| 262 | |||
| 263 | =cut |