use
5.014002;
use
version;
our
$VERSION
= version->declare(
"v1.0.8"
);
has
'entries'
=> (
is
=>
'ro'
,
isa
=>
'ArrayRef[Zonemaster::Engine::Logger::Entry]'
,
default
=>
sub
{ [] }
);
has
'callback'
=> (
is
=>
'rw'
,
isa
=>
'CodeRef'
,
required
=> 0,
clearer
=>
'clear_callback'
);
sub
add {
my
(
$self
,
$tag
,
$argref
) =
@_
;
my
$new
=
Zonemaster::Engine::Logger::Entry->new( {
tag
=>
uc
(
$tag
),
args
=>
$argref
} );
$self
->_check_filter(
$new
);
push
@{
$self
->entries },
$new
;
if
(
$self
->callback and
ref
(
$self
->callback ) eq
'CODE'
) {
eval
{
$self
->callback->(
$new
) };
if
( $@ ) {
my
$err
= $@;
if
( blessed(
$err
) and
$err
->isa(
"Zonemaster::Engine::Exception"
) ) {
die
$err
;
}
else
{
$self
->clear_callback;
$self
->add(
LOGGER_CALLBACK_ERROR
=> {
exception
=>
$err
} );
}
}
}
return
$new
;
}
sub
_check_filter {
my
(
$self
,
$entry
) =
@_
;
my
$config
= Zonemaster::Engine::Profile->effective->get(
q{logfilter}
);
if
(
$config
) {
if
(
$config
->{
$entry
->module } ) {
my
$match
= 0;
foreach
my
$rule
( @{
$config
->{
$entry
->module }{
$entry
->tag }} ) {
foreach
my
$key
(
keys
%{
$rule
->{
when
} } ) {
my
$cond
=
$rule
->{
when
}{
$key
};
if
(
ref
(
$cond
) and
ref
(
$cond
) eq
'ARRAY'
) {
if
( any {
$_
eq
$entry
->args->{
$key
} }
@$cond
) {
$match
= 1;
}
else
{
$match
= 0;
last
;
}
}
else
{
if
(
$cond
eq
$entry
->args->{
$key
} ) {
$match
= 1;
}
else
{
$match
= 0;
last
;
}
}
}
if
(
$match
) {
$entry
->_set_level(
$rule
->{set} );
last
;
}
}
}
}
return
;
}
sub
start_time_now {
Zonemaster::Engine::Logger::Entry->start_time_now();
return
;
}
sub
reset_config {
Zonemaster::Engine::Logger::Entry->reset_config();
return
;
}
sub
clear_history {
my
(
$self
) =
@_
;
my
$r
=
$self
->entries;
splice
@$r
, 0,
scalar
(
@$r
);
return
;
}
sub
get_max_level {
my
(
$self
) =
@_
;
my
%levels
=
reverse
Zonemaster::Engine::Logger::Entry->levels();
my
$level
= 0;
foreach
( @{
$self
->entries } ) {
$level
=
$_
->numeric_level
if
$_
->numeric_level >
$level
;
}
return
$levels
{
$level
};
}
sub
json {
my
(
$self
,
$min_level
) =
@_
;
my
$json
= JSON::PP->new->allow_blessed->convert_blessed->canonical;
my
%numeric
= Zonemaster::Engine::Logger::Entry->levels();
my
@msg
= @{
$self
->entries };
if
(
$min_level
and
defined
$numeric
{
uc
(
$min_level
) } ) {
@msg
=
grep
{
$_
->numeric_level >=
$numeric
{
uc
(
$min_level
) } }
@msg
;
}
my
@out
;
foreach
my
$m
(
@msg
) {
my
%r
;
$r
{timestamp} =
$m
->timestamp;
$r
{module} =
$m
->module;
$r
{testcase} =
$m
->testcase;
$r
{tag} =
$m
->tag;
$r
{level} =
$m
->level;
$r
{args} =
$m
->args
if
$m
->args;
push
@out
, \
%r
;
}
return
$json
->encode( \
@out
);
}
no
Moose;
__PACKAGE__->meta->make_immutable;
1;