2017 © Pedro Peláez
 

library access-log

Middleware to generate access logs

image

middlewares/access-log

Middleware to generate access logs

  • Saturday, January 27, 2018
  • by oscarotero
  • Repository
  • 3 Watchers
  • 5 Stars
  • 4,443 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 14 Versions
  • 34 % Grown

The README.md

middlewares/access-log

Latest Version on Packagist ![Software License][ico-license] Testing ![Total Downloads][ico-downloads], (*1)

Middleware to generate access logs for each request using the Apache's access log format. This middleware requires a Psr log implementation, for example monolog., (*2)

Requirements

Installation

This package is installable and autoloadable via Composer as middlewares/access-log., (*3)

composer require middlewares/access-log

Example

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

//Create the logger
$logger = new Logger('access');
$logger->pushHandler(new StreamHandler(fopen('/access-log.txt', 'r+')));

$dispatcher = new Dispatcher([
    new Middlewares\AccessLog($logger)
]);

$response = $dispatcher->dispatch(new ServerRequest());

Usage

This middleware uses PSR-3 logger standard to store the logs, so you need to pass a Psr\Log\LoggerInterface instance to the constructor., (*4)

format

This option allows to define the format used to save the log messages. You can create your own format (More info about the available options) ou use one of the following constants provided with predefined formats:, (*5)

  • AccessLog::FORMAT_COMMON (Used by default)
  • AccessLog::FORMAT_COMMON_VHOST
  • AccessLog::FORMAT_COMBINED
  • AccessLog::FORMAT_REFERER
  • AccessLog::FORMAT_AGENT
  • AccessLog::FORMAT_VHOST
  • AccessLog::FORMAT_COMMON_DEBIAN
  • AccessLog::FORMAT_COMBINED_DEBIAN
  • AccessLog::FORMAT_VHOST_COMBINED_DEBIAN
use Middlewares\AccessLog;

$format = AccessLog::FORMAT_COMMON_VHOST;

$accessLog = (new AccessLog($logger))->format($format);

ipAttribute

By default uses the REMOTE_ADDR server parameter to get the client ip. This option allows to use a request attribute. Useful to combine with any ip detection middleware, for example client-ip:, (*6)

Dispatcher::run([
    //detect the client ip and save it in "ip" attribute
    (new Middlewares\ClientIP())->attribute('ip'),

    //use that attribute
    (new Middlewares\AccessLog($logger))->ipAttribute('ip')
]);

hostnameLookups

Enable the hostnameLookups flag used to get the remote hostname (%h). By default is false., (*7)

context

By default there is no context passed into the logger. When setting this context callable it will be called each time an request is logged with both the request and response. Letting you set context to the log entry:, (*8)

$dispatcher = new Dispatcher([
    //detect the client ip and save it in ip attribute
    (new Middlewares\ClientIP())->attribute('ip'),

    // Add UUID for the request so we can trace logs later in case somethings goes wrong
    new Middlewares\Uuid(),

    // Use the data from the other two middleware and use it as context for logging
    (new Middlewares\AccessLog($logger))
        ->context(function (ServerRequestInterface $request, ResponseInterface $response) {
            return [
                'request-id' => $request->getHeaderLine('X-Uuid'),
                'client-ip' => $request->getAttribute('ip'),
            ];
        })
]);

Custom format string

The format string tries to mimic the directives described in Apache Httpd server documentation., (*9)

A custom format can be defined by placing "%" directives in the format string, which are replaced in the log file by the values as follows:, (*10)

Format String Description
%% The percent sign.
%a Client IP address of the server request (see the ipAttribute option).
%{c}a Client IP address of the server request (see the ipAttribute option, differs from the original Apache directive behavior).
%A Local IP-address.
%B Size of response in bytes, excluding HTTP headers.
%b Size of response in bytes, excluding HTTP headers. In CLF format, i.e. a - rather than a 0 when no bytes are sent.
%{VARNAME}C The contents of cookie VARNAME in the server request sent to the server.
%D The time taken to serve the request, in microseconds.
%{VARNAME}e The contents of the environment variable VARNAME.
%f Filename.
%h Remote hostname. Will log the IP address if hostnameLookups is set to false, which is the default
%H The request protocol.
%{VARNAME}i The contents of VARNAME: header line(s) in the request sent to the server.
%m The request method.
%{VARNAME}n The contents of attribute VARNAME in the server request (differs from the original Apache directive behavior).
%{VARNAME}o The contents of VARNAME: header line(s) in the reply.
%p The canonical port of the server serving the request.
%{format}p The canonical port of the server serving the request or the string - for remote. Valid formats are canonical, local, or remote (differs from the original Apache directive behavior).
%q The query string (prepended with a ? if a query string exists, otherwise an empty string).
%r First line of request.
%s Status.
%t Time the request was received, in the format [18/Sep/2011:19:18:28 -0400]. The last number indicates the timezone offset from GMT
%{format}t The time, in the form given by format, which should be in an extended strftime(3) format (potentially localized). If the format starts with begin: (default) the time is taken at the beginning of the request processing. If it starts with end: it is the time when the log entry gets written, close to the end of the request processing. In addition to the formats supported by strftime(3), the following format tokens are supported: sec, msec, usec (differs from the original Apache directive behavior).
%T The time taken to serve the request, in seconds.
%{UNIT}T The time taken to serve the request, in a time unit given by UNIT. Valid units are ms for milliseconds, us for microseconds, and s for seconds. Using s gives the same result as %T without any format; using us gives the same result as %D.
%u Remote user if the request was authenticated. May be bogus if return status (%s) is 401 (unauthorized).
%U The URL path requested, not including any query string.
%v The host of the server request (differs from the original Apache directive behavior).
%V The server name that appears in the server param of the request, or the request host if not available (differs from the original Apache directive behavior).
%I Bytes received, including request and headers. Cannot be zero.
%O Bytes sent, including headers.
%S Bytes transferred (received and sent), including request and headers, cannot be zero. This is the combination of %I and %O.

The following Apache Httpd server directives are not implemented in this middleware:, (*11)

Format String Description
%k Will print the string -.
%l Will print the string -.
%L Will print the string -.
%P Will print the string -.
%{format}P Will print the string -.
%R Will print the string -.
%X Will print the string -.
%{VARNAME}^ti Will print the string -.
%{VARNAME}^to Will print the string -.

Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details., (*12)

The MIT License (MIT). Please see LICENSE for more information., (*13)

The Versions

27/01 2018

dev-master

9999999-dev https://github.com/middlewares/access-log

Middleware to generate access logs

  Sources   Download

MIT

The Requires

 

The Development Requires

logger middleware psr-7 logging server http access psr-15

27/01 2018

v1.0.0

1.0.0.0 https://github.com/middlewares/access-log

Middleware to generate access logs

  Sources   Download

MIT

The Requires

 

The Development Requires

logger middleware psr-7 logging server http access psr-15

13/11 2017

v0.10.0

0.10.0.0 https://github.com/middlewares/access-log

Middleware to generate access logs

  Sources   Download

MIT

The Requires

 

The Development Requires

logger middleware psr-7 logging server http access psr-15

01/10 2017

v0.9.0

0.9.0.0 https://github.com/middlewares/access-log

Middleware to generate access logs

  Sources   Download

MIT

The Requires

 

The Development Requires

logger middleware psr-7 logging server http access psr-15

21/09 2017

v0.8.0

0.8.0.0 https://github.com/middlewares/access-log

Middleware to generate access logs

  Sources   Download

MIT

The Requires

 

The Development Requires

logger middleware psr-7 logging server http access psr-15

04/09 2017

v0.7.0

0.7.0.0 https://github.com/middlewares/access-log

Middleware to generate access logs

  Sources   Download

MIT

The Requires

 

The Development Requires

logger middleware psr-7 logging server http access psr-15

07/04 2017

v0.6.1

0.6.1.0 https://github.com/middlewares/access-log

Middleware to generate access logs

  Sources   Download

MIT

The Requires

 

The Development Requires

logger middleware psr-7 logging server http access psr-15

06/04 2017

v0.6.0

0.6.0.0 https://github.com/middlewares/access-log

Middleware to generate access logs

  Sources   Download

MIT

The Requires

 

The Development Requires

logger middleware psr-7 logging server http access psr-15

03/04 2017

v0.5.0

0.5.0.0 https://github.com/middlewares/access-log

Middleware to generate access logs

  Sources   Download

MIT

The Requires

 

The Development Requires

logger middleware psr-7 logging server http access psr-15

01/04 2017

v0.4.1

0.4.1.0 https://github.com/middlewares/access-log

Middleware to generate access logs

  Sources   Download

MIT

The Requires

 

The Development Requires

logger middleware psr-7 logging server http access psr-15

30/03 2017

v0.4.0

0.4.0.0 https://github.com/middlewares/access-log

Middleware to generate access logs

  Sources   Download

MIT

The Requires

 

The Development Requires

logger middleware psr-7 logging server http access psr-15

26/12 2016
27/11 2016
09/10 2016