我想做一个分析日志的代码,把一段日志203.208.60.248 - - [17/Apr/2010:00:00:05 +0800] "GET /mall/article/NianMan18SuiQieJuBeiYingWenYueDuNengLiZheJin.html HTTP/1.1" 200 13231 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
他拆分出来,给他分成IP , 时间,访问的资源地址,访问的状态码,访问的浏览器,访问者所来的地址等。我现在有一些代码,谢能帮我看看则么改改,能实现http://ludou.co.tv/logreader/这种日志分析器的功能,给他都分开了放对象里。package com.test;import java.util.Date;public class Log implements java.io.Serializable { /**
*
*/
private static final long serialVersionUID = 7956751372792048100L;
private Integer id;
private String ip;
private Date ctime;
private String ac;
private String Agent;
private Integer code; public Log() {
} public Log(String ip, Date ctime, String ac, Integer code,String Agent) {
this.ip = ip;
this.ctime = ctime;
this.ac = ac;
this.code = code;
this.Agent = Agent;
} public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} public String getIp() {
return this.ip;
} public void setIp(String ip) {
this.ip = ip;
} public Date getCtime() {
return this.ctime;
} public void setCtime(Date ctime) {
this.ctime = ctime;
} public String getAc() {
return this.ac;
} public void setAc(String ac) {
this.ac = ac;
} public Integer getCode() {
return this.code;
} public void setCode(Integer code) {
this.code = code;
} public String getAgent() {
return Agent;
} public void setAgent(String agent) {
Agent = agent;
}}package com.test;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;import clover.org.apache.oro.text.regex.MalformedPatternException;
import clover.org.apache.oro.text.regex.MatchResult;
import clover.org.apache.oro.text.regex.Pattern;
import clover.org.apache.oro.text.regex.PatternCompiler;
import clover.org.apache.oro.text.regex.PatternMatcher;
import clover.org.apache.oro.text.regex.Perl5Compiler;
import clover.org.apache.oro.text.regex.Perl5Matcher;
public class SplitTool { public static Log genLog(String source) throws MalformedPatternException {
String regexp = "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})\\s-\\s-\\s\\[([^\\]]+)\\]\\s'([^']+)'\\s(\\d{3})";
PatternCompiler compiler = new Perl5Compiler();
Pattern pattern = compiler.compile(regexp);
PatternMatcher matcher = new Perl5Matcher();
Log log = new Log();
Locale locale = Locale.US;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z",
locale);
if (matcher.contains(source, pattern)) {
MatchResult result = matcher.getMatch();
log.setIp(result.group(1));
try {
log.setCtime(sdf.parse(result.group(2)));
} catch (ParseException e) {
e.printStackTrace();
}
log.setAc(result.group(3));
try {
log.setCode(Integer.parseInt(result.group(4)));
log.setAgent(result.group(5));
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
return log;
} public static void main(String[] args) {
String entry = "203.208.60.248 - - [17/Apr/2010:00:00:05 +0800] 'GET /mall/article/NianMan18SuiQieJuBeiYingWenYueDuNengLiZheJin.html HTTP/1.1' 200 13231 '-' 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'";
try {
Log log = genLog(entry);
System.out.println(log.getIp()+","+log.getAc()+","+log.getCode()+","+log.getCtime()+","+log.getAgent());
} catch (MalformedPatternException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}谁能帮我改改,我这个有得分不出来的,日志会有的项没有,那就给他显示空的,有的就访对象里面,谁做过这样的代码能给我看看吗,谢了,比较着急!!!
他拆分出来,给他分成IP , 时间,访问的资源地址,访问的状态码,访问的浏览器,访问者所来的地址等。我现在有一些代码,谢能帮我看看则么改改,能实现http://ludou.co.tv/logreader/这种日志分析器的功能,给他都分开了放对象里。package com.test;import java.util.Date;public class Log implements java.io.Serializable { /**
*
*/
private static final long serialVersionUID = 7956751372792048100L;
private Integer id;
private String ip;
private Date ctime;
private String ac;
private String Agent;
private Integer code; public Log() {
} public Log(String ip, Date ctime, String ac, Integer code,String Agent) {
this.ip = ip;
this.ctime = ctime;
this.ac = ac;
this.code = code;
this.Agent = Agent;
} public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} public String getIp() {
return this.ip;
} public void setIp(String ip) {
this.ip = ip;
} public Date getCtime() {
return this.ctime;
} public void setCtime(Date ctime) {
this.ctime = ctime;
} public String getAc() {
return this.ac;
} public void setAc(String ac) {
this.ac = ac;
} public Integer getCode() {
return this.code;
} public void setCode(Integer code) {
this.code = code;
} public String getAgent() {
return Agent;
} public void setAgent(String agent) {
Agent = agent;
}}package com.test;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;import clover.org.apache.oro.text.regex.MalformedPatternException;
import clover.org.apache.oro.text.regex.MatchResult;
import clover.org.apache.oro.text.regex.Pattern;
import clover.org.apache.oro.text.regex.PatternCompiler;
import clover.org.apache.oro.text.regex.PatternMatcher;
import clover.org.apache.oro.text.regex.Perl5Compiler;
import clover.org.apache.oro.text.regex.Perl5Matcher;
public class SplitTool { public static Log genLog(String source) throws MalformedPatternException {
String regexp = "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})\\s-\\s-\\s\\[([^\\]]+)\\]\\s'([^']+)'\\s(\\d{3})";
PatternCompiler compiler = new Perl5Compiler();
Pattern pattern = compiler.compile(regexp);
PatternMatcher matcher = new Perl5Matcher();
Log log = new Log();
Locale locale = Locale.US;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z",
locale);
if (matcher.contains(source, pattern)) {
MatchResult result = matcher.getMatch();
log.setIp(result.group(1));
try {
log.setCtime(sdf.parse(result.group(2)));
} catch (ParseException e) {
e.printStackTrace();
}
log.setAc(result.group(3));
try {
log.setCode(Integer.parseInt(result.group(4)));
log.setAgent(result.group(5));
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
return log;
} public static void main(String[] args) {
String entry = "203.208.60.248 - - [17/Apr/2010:00:00:05 +0800] 'GET /mall/article/NianMan18SuiQieJuBeiYingWenYueDuNengLiZheJin.html HTTP/1.1' 200 13231 '-' 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'";
try {
Log log = genLog(entry);
System.out.println(log.getIp()+","+log.getAc()+","+log.getCode()+","+log.getCtime()+","+log.getAgent());
} catch (MalformedPatternException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}谁能帮我改改,我这个有得分不出来的,日志会有的项没有,那就给他显示空的,有的就访对象里面,谁做过这样的代码能给我看看吗,谢了,比较着急!!!
解决方案 »
- 从数据库表反转生成的hibernate映射文件hbm.xml中的comment注释是乱码
- Linux下搭建jsp环境使用的jdbc和windows下一样吗
- document.body.innerHTML的问题,急救!!!(全部身家)
- 如何使用servlet实现计划任务
- [SSH整合问题] Resource: B.hbm.xml not found
- 如何动态构建流程管理设计器
- 请教各位,如何使用PrepareStatement和CallableStatement连接数据库??
- Hibernate中的综合查询问题
- APPLET到底怎么了?
- 一个html页面的搜索框不是form表单,那么如何往controller中传值?
- 整合struts2+hibernate+spring的问题
- 请问在vb中的byte s=&H1在java中怎么实现?
给你点建议:进一步调试这个正则表达式吧,其他的代码没什么问题。现成的代码我没有,不好意思。
public static Log genLog(String source) {
String regexp = "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})\\s*-\\s*-\\s*\\[([^\\]]+)\\]\\s*'([^']+)'\\s*(\\d{3})";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(source);
Log log = new Log();
Locale locale = Locale.US;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z",
locale);
if (matcher.find()) {
log.setIp(matcher.group(1));
try {
log.setCtime(sdf.parse(matcher.group(2)));
log.setAc(matcher.group(3));
log.setCode(Integer.parseInt(matcher.group(4)));
log.setAgent(matcher.group(5));
} catch (Exception e) {
System.out.println(e.getMessage());
} }
return log;
} public static void main(String[] args) {
String entry = "203.208.60.248 - - [17/Apr/2010:00:00:05 +0800] 'GET /mall/article/NianMan18SuiQieJuBeiYingWenYueDuNengLiZheJin.html HTTP/1.1' 200 13231 '-' 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'";
Log log = genLog(entry);
System.out.println("ip:" + log.getIp());
System.out.println("code:" + log.getCode());
System.out.println("AC:" + log.getAc());
System.out.println("Ctime:" + log.getCtime());
System.out.println("Agent:" + log.getAgent()); }因为不知道Agent是哪块,也没有做匹配。也就是说,现在只有匹配到group4,5的话就是越界。
如果你告诉我Agent是哪个,在正则中再添加一个组就行了。
我这有一条日志
192.168.1.100 - - [17/Mar/2004:01:29:21 -0800] "GET / HTTP/1.1" 200 429 "http://tw.search.yahoo.com/search/kimo?p=%AB%D8%B3%5D%5D%B6%7D%B5o%AE%D7&u=B" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)"
你们可以通过http://ludou.co.tv/logreader/这个工具来试试,他就可以把他按照这几项拆分出来。所以我的log对象也要改成这样private Integer id;
private String ip;
private String ctime; //时间
private String HttpVersion;
private String requestResources; //请求的资源
private String Agent; //访问的浏览器
private Integer code; //状态码
private String SizeinBytes;//
private String Referer;//访问者过来的地址你们可以先用我这条日志在 http://ludou.co.tv/logreader/这个工具下试下就都明白了,他就可能全部都分出来,如果要是其中哪项没有他就空着,我现在就想实现这种效果,我的正则改了半天也不行,因为每条日志是不一样的,没准这条日志是全的,没准他就缺了某一项。谁能帮我看看这个可的怎么弄
203.208.60.248 - - [17/Apr/2010:00:00:05 +0800] "GET /mall/article/NianMan18SuiQieJuBeiYingWenYueDuNengLiZheJin.html HTTP/1.1" 200 13231 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
这条 Referer 的信息就没有,http://ludou.co.tv/logreader/这个工具可以分出来,只不过Referer 他是空着的,要用正则怎么弄!