import java.util.Date; import java.util.regex.Pattern; import java.util.regex.Matcher; import java.text.SimpleDateFormat; public class LogEntry { private static Pattern epat = Pattern.compile("^(\\S+) (\\S+) (\\S+) \\[(.+)\\] \"(.+)\" (\\d{3}) (\\d+|-) \"(.*?)\" \"(.*?)\"$"); private static Pattern rpat = Pattern.compile("\\A(\\S+)\\s+(\\S+)\\s+(\\S+)\\Z"); private String host = null; private String user = null; private String auth = null; private Date date = null; private String referrer = null; private String ua = null; private int rcode = 0; private double nbytes = 0; private String url = null; public LogEntry(String line) throws Exception { Matcher matcher = epat.matcher(line); if (matcher.matches()){ host = matcher.group(1); user = matcher.group(2); auth = matcher.group(3); String dates = matcher.group(4); String request = matcher.group(5); String code = matcher.group(6); String bs = matcher.group(7); referrer = matcher.group(8); ua = matcher.group(9); SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z"); date = sdf.parse(dates); rcode = Integer.parseInt(code); nbytes = (bs.equals("-") ? 0 : Double.parseDouble(bs)); Matcher matcher2 = rpat.matcher(request); if (matcher2.matches()){ url = matcher2.group(2); } } } public String getHost(){ return host; } public String getUser(){ return user; } public String getAuth(){ return auth; } public Date getDate(){ return date; } public String getReferrer(){ return referrer; } public String getUa(){ return ua; } public int getRCode(){ return rcode; } public double getNByes(){ return nbytes; } public String getURL(){ return url; } public String toString(){ return "LogEntry[host:" + host + ", date:" + date + ", referrer:" + referrer + ", url:" + url + ", ua:" + ua + ", user:" + user + ", auth:" + auth + ", rcode:" + rcode + ", nbytes:" + nbytes + "]"; } public static void main(String[] args) throws Exception { String line = "65.209.65.37 - - [19/Oct/2005:14:26:36 -0500] \"GET /xpb4j/xmldata/google/res1.xml HTTP/1.1\" 200 11294 \"http://www.pankaj-k.net/xpb4j/docs/Measurements-May30/measurements-May30-2002.html\" \"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)\""; LogEntry le = new LogEntry(line); System.out.println(le); } }