Skip to content

How to implement a NCSA Common Line Format (CLF) logger middleware? #199

@shouze

Description

@shouze

I've tried to procude NCSA CLF compatible logs for some REST Http server managed by aerys, here's the current state of my work:

class NcsaClfLogger implements Aerys\Bootable
{
    private $logger;

    function boot(Aerys\Server $server, Psr\Log\LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    function __invoke(Aerys\Request $request, Aerys\Response $response)
    {
        // We should probably return and/or partialy collect info until the response is in ended state?
        // NCSA / CLF log line format
        // @see https://en.wikipedia.org/wiki/Common_Log_Format
        $logLine = sprintf(
            '%s %s %s [%s] "%s %s HTTP/%s" %s %s',
            $request->getHeader('X-Forwarded-For') ?? $request->getConnectionInfo()['client_addr'],
            '-',
            '-',
            date('d/M/Y:H:i:s O'),
            $request->getMethod(),
            $request->getUri(),
            $request->getProtocolVersion(),
            $response->__debugInfo()[':status'], // no other way to get the response status + we should ensure it's the final status
            0 // how to get response size? We should probable get it when response is in ended state but how?
        );
        $this->logger->info($logLine);
    }
}

And here's the result in aerys logs:

aerys -c bin/server
[2017-10-26 13:57:01] warning 203.0.113.195, 70.41.3.18, 150.172.238.178 - - [26/Oct/2017:13:57:01 +0000] "GET /health HTTP/1.1" 200 0
[2017-10-26 13:57:04] warning 172.17.0.1 - - [26/Oct/2017:13:57:04 +0000] "GET /health HTTP/1.1" 200 0
[2017-10-26 13:57:06] warning 203.0.113.195, 70.41.3.18, 150.172.238.178 - - [26/Oct/2017:13:57:06 +0000] "GET /health HTTP/1.1" 200 0
[2017-10-26 13:57:07] warning 172.17.0.1 - - [26/Oct/2017:13:57:07 +0000] "GET /health HTTP/1.1" 200 0

I have few questions:

  • How to get & return the response size ? Response always seem to be in state NONE in the middleware.
  • Is there a better way to get the response status and to ensure that's the correct one? Here in my implementation it will always return 200. (also related to response state of course).
  • How can I avoid aerys to prefix log lines by itself? Where can I override the default log line format/handler?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions