Plack-Middleware-TrafficLog
view release on metacpan or search on metacpan
lib/Plack/Middleware/TrafficLog.pm view on Meta::CPAN
package Plack::Middleware::TrafficLog;
=head1 NAME
Plack::Middleware::TrafficLog - Log headers and body of HTTP traffic
=head1 SYNOPSIS
=for markdown ```perl
# In app.psgi
use Plack::Builder;
builder {
enable "TrafficLog", with_body => 1;
};
=for markdown ```
=head1 DESCRIPTION
This middleware logs the request and response messages with detailed
information about headers and the body.
The example log:
=for markdown ```console
[08/Aug/2012:16:59:47 +0200] [164836368] [127.0.0.1 -> 0:5000] [Request ]
|GET / HTTP/1.1|Connection: TE, close|Host: localhost:5000|TE: deflate,gzi
p;q=0.3|User-Agent: lwp-request/6.03 libwww-perl/6.03||
[08/Aug/2012:16:59:47 +0200] [164836368] [127.0.0.1 <- 0:5000] [Response]
|HTTP/1.0 200 OK|Content-Type: text/plain||Hello World
=for markdown ```
This module works also with applications that have delayed response. In that
case, each chunk is logged separately and shares the same unique ID number and
headers.
The body of the request and response is not logged by default. For streaming
responses, only the first chunk is logged by default.
=for readme stop
=cut
use 5.008;
use strict;
use warnings;
our $VERSION = '0.0404';
use parent 'Plack::Middleware';
use Plack::Util::Accessor qw(
with_request with_response with_date with_body with_all_chunks eol body_eol logger
_counter _call_id _strftime
);
use Plack::Util;
use Plack::Request;
use Plack::Response;
use POSIX ();
use POSIX::strftime::Compiler ();
use Scalar::Util ();
sub prepare_app {
my ($self) = @_;
# the default values
$self->with_request(Plack::Util::TRUE) unless defined $self->with_request;
$self->with_response(Plack::Util::TRUE) unless defined $self->with_response;
$self->with_date(Plack::Util::TRUE) unless defined $self->with_date;
$self->with_body(Plack::Util::FALSE) unless defined $self->with_body;
$self->with_all_chunks(Plack::Util::FALSE) unless defined $self->with_all_chunks;
$self->body_eol(defined $self->eol ? $self->eol : ' ') unless defined $self->body_eol;
$self->eol('|') unless defined $self->eol;
$self->_strftime(POSIX::strftime::Compiler->new('%d/%b/%Y:%H:%M:%S %z'));
$self->_counter(0);
}
sub _log_message { ## no critic(Subroutines::ProhibitManyArgs)
my ($self, $type, $env, $status, $headers, $body) = @_;
my $logger = $self->logger || sub { $env->{'psgi.errors'}->print(@_) };
my $server_addr = sprintf '%s:%s', $env->{SERVER_NAME}, $env->{SERVER_PORT};
my $remote_addr
= defined $env->{REMOTE_PORT}
? sprintf '%s:%s', $env->{REMOTE_ADDR}, $env->{REMOTE_PORT}
: $env->{REMOTE_ADDR};
my $eol = $self->eol;
my $body_eol = $self->body_eol;
$body =~ s/\015?\012/$body_eol/gs if defined $body_eol;
lib/Plack/Middleware/TrafficLog.pm view on Meta::CPAN
};
}
)
: $res;
}
1;
__END__
=head1 CONFIGURATION
=over 4
=item logger
Sets a callback to print log message to. It prints to C<psgi.errors> output
stream by default.
=for markdown ```ini
# traffic.l4p
log4perl.logger.traffic = DEBUG, LogfileTraffic
log4perl.appender.LogfileTraffic = Log::Log4perl::Appender::File
log4perl.appender.LogfileTraffic.filename = traffic.log
log4perl.appender.LogfileTraffic.layout = PatternLayout
log4perl.appender.LogfileTraffic.layout.ConversionPattern = %m{chomp}%n
=for markdown ```
=for markdown ```perl
# app.psgi
use Log::Log4perl qw(:levels get_logger);
Log::Log4perl->init('traffic.l4p');
my $logger = get_logger('traffic');
enable "Plack::Middleware::TrafficLog",
logger => sub { $logger->log($INFO, join '', @_) };
=for markdown ```
=item with_request
The false value disables logging of the request message.
=item with_response
The false value disables logging of the response message.
=item with_date
The false value disables logging of the current date.
=item with_body
The true value enables logging of the message's body.
=item with_all_chunks
The true value enables logging of every chunk for streaming responses.
=item eol
Sets the line separator for the message's headers and the body. The default
value is the pipe character C<|>.
=item body_eol
Sets the line separator for message's body only. The default is the space
character C< >. The default value is used only if B<eol> is also undefined.
=back
=for readme continue
=head1 SEE ALSO
L<Plack>, L<Plack::Middleware::AccessLog>.
=head1 BUGS
This module has an unstable API and it can be changed in the future.
The log file can contain binary data if the PSGI server provides binary
files.
If you find the bug or want to implement new features, please report it at
L<http://github.com/dex4er/perl-Plack-Middleware-TrafficLog/issues>
The code repository is available at
L<http://github.com/dex4er/perl-Plack-Middleware-TrafficLog>
=head1 AUTHOR
Piotr Roszatycki <dexter@cpan.org>
=head1 LICENSE
Copyright (c) 2012, 2014-2015, 2023 Piotr Roszatycki <dexter@cpan.org>.
This is free software; you can redistribute it and/or modify it under
the same terms as perl itself.
See L<http://dev.perl.org/licenses/artistic.html>
( run in 1.992 second using v1.01-cache-2.11-cpan-39bf76dae61 )