| 1 | package Zonemaster::Logger v1.0.0; | ||
| 2 | |||
| 3 | use 5.14.2; | ||
| 4 | use Moose; | ||
| 5 | |||
| 6 | use Zonemaster::Logger::Entry; | ||
| 7 | use Zonemaster; | ||
| 8 | use List::MoreUtils qw[none]; | ||
| 9 | use Scalar::Util qw[blessed]; | ||
| 10 | use JSON::XS; | ||
| 11 | |||
| 12 | has 'entries' => ( | ||
| 13 | is => 'ro', | ||
| 14 | isa => 'ArrayRef[Zonemaster::Logger::Entry]', | ||
| 15 | default => sub { [] } | ||
| 16 | ); | ||
| 17 | has 'callback' => ( is => 'rw', isa => 'CodeRef', required => 0, clearer => 'clear_callback' ); | ||
| 18 | |||
| 19 | sub add { | ||
| 20 | my ( $self, $tag, $argref ) = @_; | ||
| 21 | |||
| 22 | my $new = | ||
| 23 | Zonemaster::Logger::Entry->new( { tag => uc( $tag ), args => $argref } ); | ||
| 24 | $self->_check_filter( $new ); | ||
| 25 | push @{ $self->entries }, $new; | ||
| 26 | |||
| 27 | if ( $self->callback and ref( $self->callback ) eq 'CODE' ) { | ||
| 28 | eval { $self->callback->( $new ) }; | ||
| 29 | if ( $@ ) { | ||
| 30 | my $err = $@; | ||
| 31 | if ( blessed( $err ) and $err->isa( "Zonemaster::Exception" ) ) { | ||
| 32 | die $err; | ||
| 33 | } | ||
| 34 | else { | ||
| 35 | $self->clear_callback; | ||
| 36 | $self->add( LOGGER_CALLBACK_ERROR => { exception => $err } ); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | return $new; | ||
| 42 | } ## end sub add | ||
| 43 | |||
| 44 | sub _check_filter { | ||
| 45 | my ( $self, $entry ) = @_; | ||
| 46 | my $config = Zonemaster->config->logfilter; | ||
| 47 | |||
| 48 | if ( $config ) { | ||
| 49 | if ( $config->{ $entry->module } ) { | ||
| 50 | if ( my $rule = $config->{ $entry->module }{ $entry->tag } ) { | ||
| 51 | foreach my $key ( keys %{ $rule->{when} } ) { | ||
| 52 | my $cond = $rule->{when}{$key}; | ||
| 53 | if ( ref( $cond ) and ref( $cond ) eq 'ARRAY' ) { | ||
| 54 | # No match in list, so overall fail, so return | ||
| 55 | ## no critic (TestingAndDebugging::ProhibitNoWarnings) | ||
| 56 | no warnings 'uninitialized'; | ||
| 57 | return if none { $_ eq $entry->args->{$key} } @$cond; | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | # No match, so overall fail, so return | ||
| 61 | ## no critic (TestingAndDebugging::ProhibitNoWarnings) | ||
| 62 | no warnings 'uninitialized'; | ||
| 63 | return if $cond ne $entry->args->{$key}; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | # Still here, so all rules matched | ||
| 67 | $entry->_set_level( $rule->{set} ); | ||
| 68 | } | ||
| 69 | } ## end if ( $config->{ $entry...}) | ||
| 70 | } ## end if ( $config ) | ||
| 71 | } ## end sub _check_filter | ||
| 72 | |||
| 73 | sub start_time_now { | ||
| 74 | Zonemaster::Logger::Entry->start_time_now(); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub clear_history { | ||
| 78 | my ( $self ) = @_; | ||
| 79 | |||
| 80 | my $r = $self->entries; | ||
| 81 | splice @$r, 0, scalar( @$r ); | ||
| 82 | |||
| 83 | return; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub json { | ||
| 87 | my ( $self, $min_level ) = @_; | ||
| 88 | my $json = JSON::XS->new->allow_blessed->convert_blessed->canonical; | ||
| 89 | my %numeric = Zonemaster::Logger::Entry->levels(); | ||
| 90 | |||
| 91 | my @msg = @{ $self->entries }; | ||
| 92 | |||
| 93 | if ( $min_level and defined $numeric{ uc( $min_level ) } ) { | ||
| 94 | @msg = grep { $_->numeric_level >= $numeric{ uc( $min_level ) } } @msg; | ||
| 95 | } | ||
| 96 | |||
| 97 | my @out; | ||
| 98 | foreach my $m ( @msg ) { | ||
| 99 | my %r; | ||
| 100 | $r{timestamp} = $m->timestamp; | ||
| 101 | $r{module} = $m->module; | ||
| 102 | $r{tag} = $m->tag; | ||
| 103 | $r{level} = $m->level; | ||
| 104 | $r{args} = $m->args if $m->args; | ||
| 105 | |||
| 106 | push @out, \%r; | ||
| 107 | } | ||
| 108 | |||
| 109 | return $json->encode( \@out ); | ||
| 110 | } ## end sub json | ||
| 111 | |||
| 112 | no Moose; | ||
| 113 | __PACKAGE__->meta->make_immutable; | ||
| 114 | |||
| 115 | 1; | ||
| 116 | |||
| 117 | =head1 NAME | ||
| 118 | |||
| 119 | Zonemaster::Logger - class that holds L<Zonemaster::Logger::Entry> objects. | ||
| 120 | |||
| 121 | =head1 SYNOPSIS | ||
| 122 | |||
| 123 | my $logger = Zonemaster::Logger->new; | ||
| 124 | $logger->add( TAG => {some => 'arguments'}); | ||
| 125 | |||
| 126 | =head1 ATTRIBUTES | ||
| 127 | |||
| 128 | =over | ||
| 129 | |||
| 130 | =item entries | ||
| 131 | |||
| 132 | A reference to an array holding L<Zonemaster::Logger::Entry> objects. | ||
| 133 | |||
| 134 | =item callback($coderef) | ||
| 135 | |||
| 136 | If this attribute is set, the given code reference will be called every time a | ||
| 137 | log entry is added. The referenced code will be called with the newly created | ||
| 138 | entry as its single argument. The return value of the called code is ignored. | ||
| 139 | |||
| 140 | If the called code throws an exception, and the exception is not an object of | ||
| 141 | class L<Zonemaster::Exception> (or a subclass of it), the exception will be | ||
| 142 | logged as a system message at default level C<CRITICAL> and the callback | ||
| 143 | attribute will be cleared. | ||
| 144 | |||
| 145 | If an exception that is of (sub)class L<Zonemaster::Exception> is called, the | ||
| 146 | exception will simply be rethrown until it reaches the code that started the | ||
| 147 | test run that logged the message. | ||
| 148 | |||
| 149 | =back | ||
| 150 | |||
| 151 | =head1 METHODS | ||
| 152 | |||
| 153 | =over | ||
| 154 | |||
| 155 | =item add($tag, $argref) | ||
| 156 | |||
| 157 | Adds an entry with the given tag and arguments to the logger object. | ||
| 158 | |||
| 159 | =item json([$level]) | ||
| 160 | |||
| 161 | Returns a JSON-formatted string with all the stored log entries. If an argument | ||
| 162 | is given and is a known severity level, only messages with at least that level | ||
| 163 | will be included. | ||
| 164 | |||
| 165 | =back | ||
| 166 | |||
| 167 | =head1 CLASS METHOD | ||
| 168 | |||
| 169 | =over | ||
| 170 | |||
| 171 | =item start_time_now() | ||
| 172 | |||
| 173 | Set the logger's start time to the current time. | ||
| 174 | |||
| 175 | =item clear_history() | ||
| 176 | |||
| 177 | Remove all known log entries. | ||
| 178 | |||
| 179 | =back | ||
| 180 | |||
| 181 | =cut |