kassner / log-parser

PHP Web Server Log Parser Library
Apache License 2.0
334 stars 64 forks source link

Apache combined log - parsing throws exception #30

Closed ToeiRei closed 7 years ago

ToeiRei commented 7 years ago

I tried to parse the apache 'combined' log file as used in debian and other distros by default

Apache config: LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

Log-Parser: $parser->setFormat('"%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\""'); $parser->setFormat('%h %l %u %t "%r" %>s %O "%{Referer}i" \\"%{User-Agent}i"');

Couldn't get it to work with the formats used above. What am I doing wrong?

kassner commented 7 years ago

@ToeiRei

Sorry for being late, vacation season :P

You should be able to use the setFormat without escaping the double-quotes when you put them inside single-quotes. This should work:

$parser->setFormat('%h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i"');

I created a small test for it, let me know if you need further help.

<?php

namespace Kassner\Teste\LogParser\Issue;

use Kassner\LogParser\LogParser;

class Issue30Test extends \PHPUnit_Framework_TestCase
{
    public function testCustomFormat()
    {
        $parser = new LogParser();
        $parser->setFormat('%h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i"');
        $entry = $parser->parse('127.0.0.1 - - [25/Jun/2017:10:14:35 +0000] "GET / HTTP/1.1" 200 11576 "-" "curl/7.47.0"');

        $this->assertEquals('127.0.0.1', $entry->host);
        $this->assertEquals('-', $entry->logname);
        $this->assertEquals('-', $entry->user);
        $this->assertEquals('25/Jun/2017:10:14:35 +0000', $entry->time);
        $this->assertEquals('GET / HTTP/1.1', $entry->request);
        $this->assertEquals('200', $entry->status);
        $this->assertEquals('11576', $entry->sentBytes);
        $this->assertEquals('-', $entry->HeaderReferer);
        $this->assertEquals('curl/7.47.0', $entry->HeaderUserAgent);
    }
}