当前位置: 代码迷 >> Web前端 >> 写了一个依照WebLogic日志统计访问人数的Java类
  详细解决方案

写了一个依照WebLogic日志统计访问人数的Java类

热度:152   发布时间:2012-11-04 10:42:41.0
写了一个按照WebLogic日志统计访问人数的Java类
自本人的小网站webdw.vicp.net上线以来,
每天都很想关心一下到底有多少人来看过,
但是看access.log太长了,不方便.
痛定思痛,终于下决心写了一个简单的日志统计类,
统计一下每天有多少IP,多少请求次数.
费话不说了,看代码.
package com.liu;

import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;

/**
 * WebLogic后台的access.log日志访问分析功能类
 * 
 * @author liujunsong
 * 
 */
public class WebLogicLogStat {
	class logline{
		String IP="";		//IP地址
		String date ="";	//访问日期
		int access =0;		//访问次数
	}
	public logline logall[] = new logline[10000];
	public int logid = 0;
	
	public String getLine(int id){
		return "<TR><TD>" +logall[id].IP + "</TD>"
		      +"<TD>" +logall[id].date +"</TD>"
		      +"<TD>" +logall[id].access +"</TD>"
		      +"</TR>";
		
	}
	public String readLogFile() throws Exception{
		StringBuffer slogfile =new StringBuffer("");
		String filepath = "E://bea//user_projects//domains//mydomain//myserver//";
		File beadir = new File(filepath);
		if (!beadir.exists()) {
			filepath = "c://bea//user_projects//domains//mydomain//myserver//";
		}
		String filename = "access.log";
		filename = filepath + filename;
		System.out.println("Want access log file:" + filename);
		File dwfile = new File(filename);
		if (dwfile.exists()) {// 如果文件存在,才操作,否则不操作
			FileReader fstream = new FileReader(filename);
			BufferedReader in = new BufferedReader(fstream);
			String record = "";
			while ((record = in.readLine()) != null) {
				System.out.println("record:"+record);
				slogfile.append((record)+"\r\n"); // 文件输出到输出流去
			}
			in.close(); // 关闭文件输入流
		}
		return slogfile.toString();
	}
	
	/**
	 * 对日志进行统计,按日,IP来进行统计,统计结果为日:IP:访问次数
	 * @param slog
	 * @return
	 */
	public String stat(String slog){
		logid =0;
		String allline[] = slog.split("\r\n");
		int i =0;
		for (i=0;i<allline.length;i++){
			String line = allline[i];
			int pos1 =0;
			int pos2 =0;
			pos1 = line.indexOf("- - [");
			pos2 = line.indexOf("] ");
			if (pos1<0 || pos2 <0 ){
				continue;
			}
			String ip = line.substring(0, pos1-1);
			String d1 = line.substring(pos1 + 5,pos2- 1);
			//System.out.println(ip+",  "+d1);
			int pos3 = d1.indexOf(":");
			if (pos3<0){continue;}
			String d2 = d1.substring(0,pos3);
			//System.out.print("ip,d2="+ip+","+d2);	
			
			int findid = findit(ip,d2);
			if (findid >0){
				logall[findid].access = logall[findid].access +1;
			}else{
				if(logid>1){
					if (logall[logid].date.equals(d2)){
						//do nothing
					}else{
						//add an new line.
						logid ++;
						logall[logid] = new logline();
						logall[logid].IP = "----------------";
						logall[logid].date = d2;
						logall[logid].access = logid;						
					}
				}
				
				logid ++;
				logall[logid] = new logline();
				logall[logid].IP = ip;
				logall[logid].date = d2;
				logall[logid].access = 1;
			}
		}
	
		logid ++;
		logall[logid] = new logline();
		logall[logid].IP = "----------------";
		logall[logid].date = logall[logid-1].date;
		logall[logid].access = logid;						
	
		
		return "";
	}
	
	private int findit(String ip,String date){
		int i =0 ;
		for(i=1;i<=logid;i++){
			if (logall[i].IP.equals(ip) && logall[i].date.equals(date)){
				return i;
			}
		}
		return -1;
	}
}


附件两个压缩包,一个是java类,一个是调用java类的jsp文件
下面是界面的一个切图.


希望对大家有所帮助.
1 楼 cjf068 2012-02-13  
效率不高,应该采用hash或boomfilter来存储ip,快速查找,同一个ip出现后累加即可。
  相关解决方案