$Dancer2::Core::Role::Logger::VERSION
=
'1.1.2'
;
sub
hook_aliases { +{} }
sub
supported_hooks {
qw(
engine.logger.before
engine.logger.after
)
;
}
sub
_build_type {
'Logger'
}
requires
'log'
;
has
auto_encoding_charset
=> (
is
=>
'ro'
,
isa
=> Str,
);
has
app_name
=> (
is
=>
'ro'
,
isa
=> Str,
default
=>
sub
{
'-'
},
);
has
log_format
=> (
is
=>
'rw'
,
isa
=> Str,
default
=>
sub
{
'[%a:%P] %L @%T> %m in %f l. %l'
},
);
my
$_levels
= {
core
=> -10,
debug
=> 1,
info
=> 2,
warn
=> 3,
warning
=> 3,
error
=> 4,
};
has
log_level
=> (
is
=>
'rw'
,
isa
=> Enum[
keys
%{
$_levels
}],
default
=>
sub
{
'debug'
},
);
sub
_should {
my
(
$self
,
$msg_level
) =
@_
;
my
$conf_level
=
$self
->log_level;
return
$_levels
->{
$conf_level
} <=
$_levels
->{
$msg_level
};
}
sub
format_message {
my
(
$self
,
$level
,
$message
) =
@_
;
chomp
$message
;
$message
= Encode::encode(
$self
->auto_encoding_charset,
$message
)
if
$self
->auto_encoding_charset;
my
@stack
=
caller
(8);
my
$request
=
$self
->request;
my
$config
=
$self
->config;
my
$block_handler
=
sub
{
my
(
$block
,
$type
) =
@_
;
if
(
$type
eq
't'
) {
return
POSIX::strftime(
$block
,
localtime
(
time
) );
}
elsif
(
$type
eq
'h'
) {
return
(
$request
&&
$request
->header(
$block
) ) ||
'-'
;
}
else
{
Carp::carp(
"{$block}$type not supported"
);
return
"-"
;
}
};
my
$chars_mapping
= {
a
=>
sub
{
$self
->app_name },
t
=>
sub
{ POSIX::strftime(
"%d/%b/%Y %H:%M:%S"
,
localtime
(
time
) ) },
T
=>
sub
{ POSIX::strftime(
"%Y-%m-%d %H:%M:%S"
,
localtime
(
time
) ) },
u
=>
sub
{ POSIX::strftime(
"%d/%b/%Y %H:%M:%S"
,
gmtime
(
time
) ) },
U
=>
sub
{ POSIX::strftime(
"%Y-%m-%d %H:%M:%S"
,
gmtime
(
time
) ) },
P
=>
sub
{$$},
L
=>
sub
{
$level
},
m
=>
sub
{
$message
},
f
=>
sub
{
$stack
[1] ||
'-'
},
l
=>
sub
{
$stack
[2] ||
'-'
},
h
=>
sub
{
(
$request
&& (
$request
->remote_host ||
$request
->address ) ) ||
'-'
},
i
=>
sub
{ (
$request
&&
$request
->id ) ||
'-'
},
};
my
$char_mapping
=
sub
{
my
$char
=
shift
;
my
$cb
=
$chars_mapping
->{
$char
};
if
( !
$cb
) {
Carp::carp
"%$char not supported."
;
return
"-"
;
}
$cb
->(
$char
);
};
my
$fmt
=
$self
->log_format;
$fmt
=~ s/
(?:
\%\{(.+?)\}([a-z])|
\%([a-zA-Z])
)
/ $1 ?
$block_handler
->($1, $2) :
$char_mapping
->($3) /egx;
return
$fmt
.
"\n"
;
}
sub
_serialize {
my
@vars
=
@_
;
return
join
q{}
,
map
+(
ref
$_
? Data::Dumper->new( [
$_
] )->Terse(1)->Purity(1)->Indent(0)
->Sortkeys(1)->Dump()
: (
defined
(
$_
) ?
$_
:
'undef'
)
),
@vars
;
}
around
'log'
=>
sub
{
my
(
$orig
,
$self
,
@args
) =
@_
;
$self
->execute_hook(
'engine.logger.before'
,
$self
,
@args
);
$self
->
$orig
(
@args
);
$self
->execute_hook(
'engine.logger.after'
,
$self
,
@args
);
};
sub
core {
my
(
$self
,
@args
) =
@_
;
$self
->_should(
'core'
) and
$self
->
log
(
'core'
, _serialize(
@args
) );
}
sub
debug {
my
(
$self
,
@args
) =
@_
;
$self
->_should(
'debug'
) and
$self
->
log
(
'debug'
, _serialize(
@args
) );
}
sub
info {
my
(
$self
,
@args
) =
@_
;
$self
->_should(
'info'
) and
$self
->
log
(
'info'
, _serialize(
@args
) );
}
sub
warning {
my
(
$self
,
@args
) =
@_
;
$self
->_should(
'warning'
) and
$self
->
log
(
'warning'
, _serialize(
@args
) );
}
sub
error {
my
(
$self
,
@args
) =
@_
;
$self
->_should(
'error'
) and
$self
->
log
(
'error'
, _serialize(
@args
) );
}
1;