kassner / log-parser

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

Format Exception with Default Nginx Format #58

Closed SAH62 closed 1 year ago

SAH62 commented 1 year ago

I've set my parser to use the default Nginx format as described on the project README:

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

A line in my nginx log is causing a format exception to be thrown:

PHP Fatal error: Uncaught Kassner\LogParser\FormatException: 162.243.142.22 - - [17/Feb/2023:02:50:10 -0500] "MGLNDD_1.2.3.4_80" 400 150 "-" "-" in /test/vendor/kassner/log-parser/src/LogParser.php:83 Stack trace:

0 /test/test.php(12): Kassner\LogParser\LogParser->parse()

1 {main}

thrown in /test/vendor/kassner/log-parser/src/LogParser.php on line 83

I don't see how this line fails to match the specified format. Am I missing something?

kassner commented 1 year ago

%r will be parsed using the request format METHOD URL HTTP/VERSION, i.e.: POST /users/add HTTP/1.0

https://github.com/kassner/log-parser/blob/6b9e55a958448a8e067602a83635b7083e6a8fa9/src/LogParser.php#L30

The line's request is MGLNDD_1.2.3.4_80, which is an invalid HTTP request (hence why Nginx it responded with 400 Bad Request).

If you want a workaround to parse out those lines, check https://github.com/kassner/log-parser/issues/50#issuecomment-706707736.

SAH62 commented 1 year ago

Thanks!