Web >> Development >> PHP >> How to parse IIS web server log

<?php $logfile = "/windows/system32/logfiles/W3SVC309283988/ex100623.log"; $logs = explode( "\n", file_get_contents( $logfile )); #Fields: #date|time|s-ip|cs-method|cs-uri-stem|cs-uri-query|s-port|cs-username|c-ip|cs(User-Agent)|sc-status| #sc-substatus sc-win32-status #Sample line: #2010-06-23 01:12:56 75.127.87.116 GET /robots.txt - 80 - 66.249.71.70 #Mozilla/5.0+(compatible;+Googlebot/2.1;++http://www.google.com/bot.html) 200 0 0 $line = ''; $entries = array(); $log = array(); $complete_match = ''; print("date|time|ip|method|uri_stem|uri_query|port|username|ip|ua|status|substatus|wstatus<br>\n"); foreach( $logs as $line ) { $pattern = '/(\d{4}[-]\d{2}[-]\d{2}) (\d{2}:\d{2}:\d{2}) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (.*) (.*) (.*) (\d+) (.*) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (.*) (.*)/U'; #$pattern = '/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (.*) (.*) (\[.*\]) (".*") (\d\d\d) (.*) (".*") (".*")/U'; preg_match( $pattern, $line, $entries ); list( $complete_match, $log['date'], $log['time'], $log['s_ip'], $log['method'], $log['uri_stem'], $log['uri_query'], $log['port'], $log['user'], $log['c_ip'], $log['ua'] ) = $entries; # do something here with the data print("{$log['date']} | {$log['time']} | {$log['s_ip']} | {$log['method']} | {$log['uri_stem']} | {$log['uri_query']} | {$log['port']} | "); print("{$log['user']} | {$log['c_ip']} | {$log['ua']} | "); print("<br>\n"); } ?> # do something here with the data print("{$log['date']} | {$log['time']} | {$log['s_ip']} | {$log['method']} | {$log['uri_stem']} | {$log['uri_query']} | {$log['port']} | "); print("{$log['user']} | {$log['c_ip']} | {$log['ua']} | "); print("<br>\n"); } ?>