kassner / log-parser

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

custom nginx access log format #39

Closed jrichet closed 5 years ago

jrichet commented 5 years ago

I am trying to parse custom nginx access logs and keep getting a Kassner\LogParser\FormatException. This exception is not terribly verbose and I can't figure out what I did wrong. Can someone explain why this isn't working?

//nginx format (as written in nginx.conf)
//'$remote_addr - $remote_user [$time_local] ($host) "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"'

$parser = new \Kassner\LogParser\LogParser();

$parser->addPattern('%NGXREMOTEADDR', '(?P<remoteaddr>.+)');
$parser->addPattern('%NGXDT', '(?P<datetime>[\d+/ :]+)');
$parser->addPattern('%NGXHST', '(?P<host>.+)');
$parser->addPattern('%NGXREQ', '(?P<request>.+)');
$parser->addPattern('%NGXSTATUS', '(?P<status>.+)');
$parser->addPattern('%NGXBYTES', '(?P<response_bytes>.+)');
$parser->addPattern('%NGXREF', '(?P<referer>.+)');
$parser->addPattern('%NGXAGENT', '(?P<user_agent>.+)');

$parser->setFormat('%NGXREMOTEADDR - %u [%NGXDT] (%NGXHST) "%NGXREQ" %NGXSTATUS %NGXBYTES "%NGXREF" "%NGXAGENT"');

at first I tried to do it the "easy way" like so but also got a format exception from the following:

$parser->setFormat('%h - %u [%t] (%h) "%r" %>s %b "%{Referer}i" \\"%{User-Agent}i"');

Having looked into the code a bit, it seems that i might need to escape some literals. I'm trying something like the following with the same result:

$parser->setFormat('%h \- %u \[%t\] \(%v\) \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"');

going back to the readme, I noticed the "default nginx" example, which led me to the following, which is still throwing the exception:

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

This turned out to be inconsistencies in our log files and was in no way the fault of your code.