/usr/share/perl5/Dancer2/Core/Role/Logger.pm is in libdancer2-perl 0.204002+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | package Dancer2::Core::Role::Logger;
# ABSTRACT: Role for logger engines
$Dancer2::Core::Role::Logger::VERSION = '0.204002';
use Dancer2::Core::Types;
use Moo::Role;
use POSIX 'strftime';
use Data::Dumper;
with 'Dancer2::Core::Role::Engine';
sub hook_aliases { +{} }
sub supported_hooks {
qw(
engine.logger.before
engine.logger.after
);
}
sub _build_type {'Logger'}
# This is the only method to implement by logger engines.
# It receives the following arguments:
# $msg_level, $msg_content, it gets called only if the configuration allows
# a message of the given level to be logged.
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 = {
# levels < 0 are for core only
core => -10,
# levels > 0 are for end-users only
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 Encode::decode(
$config->{'charset'} || 'UTF-8',
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 {
Encode::decode(
$config->{'charset'} || 'UTF-8',
POSIX::strftime( "%d/%b/%Y %H:%M:%S", localtime(time) )
);
},
T => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime(time) ) },
u => sub {
Encode::decode(
$config->{'charset'} || 'UTF-8',
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;
__END__
=pod
=encoding UTF-8
=head1 NAME
Dancer2::Core::Role::Logger - Role for logger engines
=head1 VERSION
version 0.204002
=head1 DESCRIPTION
Any class that consumes this role will be able to implement to write log messages.
In order to implement this role, the consumer B<must> implement the C<log>
method. This method will receives as argument the C<level> and the C<message>.
=head1 ATTRIBUTES
=head2 auto_encoding_charset
Charset to use when writing a message.
=head2 app_name
Name of the application. Can be used in the message.
=head2 log_format
This is a format string (or a preset name) to specify the log format.
The possible values are:
=over 4
=item %h
host emitting the request
=item %t
date (local timezone, formatted like %d/%b/%Y %H:%M:%S)
=item %T
date (local timezone, formatted like %Y-%m-%d %H:%M:%S)
=item %u
date (UTC timezone, formatted like %d/%b/%Y %H:%M:%S)
=item %U
date (UTC timezone, formatted like %Y-%m-%d %H:%M:%S)
=item %P
PID
=item %L
log level
=item %D
timer
=item %m
message
=item %f
file name that emit the message
=item %l
line from the file
=item %i
request ID
=item %{$fmt}t
timer formatted with a valid time format
=item %{header}h
header value
=back
=head2 log_level
Level to use by default.
=head1 METHODS
=head2 core
Log messages as B<core>.
=head2 debug
Log messages as B<debug>.
=head2 info
Log messages as B<info>.
=head2 warning
Log messages as B<warning>.
=head2 error
Log messages as B<error>.
=head2 format_message
Provides a common message formatting.
=head1 CONFIGURATION
The B<logger> configuration variable tells Dancer2 which engine to use.
You can change it either in your config.yml file:
# logging to console
logger: "console"
The log format can also be configured,
please see L<Dancer2::Core::Role::Logger/"log_format"> for details.
=head1 AUTHOR
Dancer Core Developers
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2016 by Alexis Sukrieh.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|