kassner / log-parser

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

Fatal error when Apache field 3 is a quoted empty string #47

Closed maidentaiwan closed 4 years ago

maidentaiwan commented 4 years ago

log-parser can't handle Apache log entries where the third field is "" rather than a dash. I see these log entries every so often.

88.99.141.3 - "" [18/Oct/2017:16:56:46 -0400] "GET / HTTP/1.1" ...

Trace:

PHP Fatal error:  Uncaught Kassner\LogParser\FormatException: 88.99.141.3 - "" [02/Jul/2017:21:35:06 -0400] "GET / HTTP/1.1" 200 16153 "http://example.com/" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0"
 in /whatever/Database/vendor/kassner/log-parser/src/LogParser.php:82
Stack trace:
#0 /whatever/Database/import-log.php(21): Kassner\LogParser\LogParser->parse('88.99.141.3 - "...')
#1 /whatever/Database/import-log.php(40): parseFile(Object(Kassner\LogParser\LogParser), 'logs/./logs09/a...')
#2 {main}
  thrown in /whatever/Database/vendor/kassner/log-parser/src/LogParser.php on line 82
maidentaiwan commented 4 years ago

Thanks for making such a helpful parser!!

kassner commented 4 years ago

I'm not exactly sure which variable you're using as the 3rd field, but assuming it is %u, it will indeed cause such issue. I haven't used Apache in half a decade maybe now, so I'm unaware if this was changed or not. PRs are welcome if so.

Again, assuming you're talking about %u, if the value is always enclosed by double quotes, you can alternatively use "%{XUser}i" instead, which will result in a HeaderXUser property in the result object. Not ideal, but gets things working if you don't care about the correctness of the format string.