Create a gist now

Instantly share code, notes, and snippets.

#daimonscala 19-2 "Apache access_log(combined) parser" Blank
object Main {
import java.net._
import java.util.Date
case class Access(
ipAddress: InetAddress,
ident: String,
user: String,
time: Date,
method: String,
uri: URI,
version: String,
status: Int,
bytes: Int,
referrer: String,
userAgent: String
)
import util.parsing.combinator._
object AccessLogParser extends JavaTokenParsers {
import java.text.SimpleDateFormat
val timeFormat = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z")
def lines
def parse(json: String): ParseResult[Any] = parseAll(lines, json)
}
def main(args: Array[String]) {
val accessLog = """66.249.69.220 - - [03/Oct/2011:01:22:54 +0900] "GET /blog/23/ HTTP/1.1" 200 22716 "-" "SAMSUNG-SGH-E250/1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 UP.Browser/6.2.3.3.c.1.101 (GUI) MMP/2.0 (compatible; Googlebot-Mobile/2.1; +http://www.google.com/bot.html)"
64.233.172.34 - - [16/Oct/2011:04:03:27 +0900] "GET /api/search/?format=atom&q=testtest HTTP/1.1" 200 20 "-" "Rome Client (http://tinyurl.com/64t5n) Ver: UNKNOWN AppEngine-Google; (+http://code.google.com/appengine; appid: xxxxx)"
64.233.172.36 - - [19/Oct/2011:05:18:52 +0900] "GET / HTTP/1.1" 304 - "http://twitter.com/" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1"
"""
val result = AccessLogParser.parse(accessLog)
println(result)
//println(result.get)
}
}
// vim: set ts=4 sw=4 et:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment