middlewares / access-log
Middleware to generate access logs
Installs: 88 908
Dependents: 4
Suggesters: 0
Security: 0
Stars: 19
Watchers: 3
Forks: 8
Open Issues: 0
Requires
- php: ^7.2 || ^8.0
- psr/http-server-middleware: ^1.0
- psr/log-implementation: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- laminas/laminas-diactoros: ^2.3
- middlewares/utils: ^3.0
- monolog/monolog: ^2.0
- oscarotero/php-cs-fixer-config: ^1.0
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^8|^9
- squizlabs/php_codesniffer: ^3.0
README
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.
Requirements
- PHP >= 7.2
- A PSR-7 http library
- A PSR-15 middleware dispatcher
- A PSR-3 logger
Installation
This package is installable and autoloadable via Composer as middlewares/access-log.
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.
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:
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:
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
.
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:
$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.
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:
The following Apache Httpd server directives are not implemented in this middleware:
Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.