Apache log line parser


/ Published in: PHP
Save to your folder(s)

I written this code to parse apache log in common log formats. It may have problems with irresponsible formatting like non-concluded into quotes request line. I welcome any modifications to my snippet because it was written if short time and this is my first snippet I publish here. Thanks.


Copy this code and paste it in your HTML
  1. function parse_access_log_line($format, $line) {
  2.  
  3. // Get list of consistent patterns
  4. $patterns = array();
  5. while($res = preg_match('/(([^%]*)%[^ ]*([a-z]))([^%]*)/i', $format, $matches)) {
  6. $patterns[$matches[3]] = '('.preg_quote($matches[2]).'(.+?))'.preg_quote($matches[4]);
  7. // Shift format string
  8. $format = str_replace($matches[1], '', $format);
  9. }
  10.  
  11. // Add beginning of the string to the first pattern and end to the last
  12. $keys = array_keys($patterns);
  13. $patterns[$keys[0]] = '^'.$patterns[$keys[0]];
  14. $patterns[end($keys)] .= '$';
  15.  
  16. $result = array();
  17.  
  18. // Consistently apply patterns to the log line shifting it to the right
  19. foreach ($patterns as $node => $pattern) {
  20. preg_match('/'.$pattern.'/', $line, $matches);
  21. $line = str_replace($matches[1], '', $line);
  22. $result[$node] = $matches[2];
  23. }
  24.  
  25. return $result;
  26. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.