当前位置 : 主页 > 编程语言 > java >

对localhost_access_log日志解析,实现流量统计

来源:互联网 收集:自由互联 发布时间:2021-06-28
实时读取tomcat 下localhost_access_log 文件,将用户的请求信息记录到数据库方便分析 static{jfinalConfig.mainInit();logs = PropKit.get("logs");task = new AbstractTaskPool (5) { //参考https://gitee.com/lxycx_xc/codes/
实时读取tomcat 下localhost_access_log 文件,将用户的请求信息记录到数据库方便分析
static{
		jfinalConfig.mainInit();
		logs = PropKit.get("logs");
		task = new AbstractTaskPool
 
  (5) {
            //参考https://gitee.com/lxycx_xc/codes/v1czm3akjqdu58txlp2eg87
			@Override
			protected boolean exec(String len) throws Exception {
                //String[] datas = len.split(" ");
                //0,1IP,4时间,5请求方式 6访问的URL,7访问协议8返回标识,9数据大小
				return parse(len);//对一行日志进行处理
			}
		};
	}
	
	public static void main(String[] args) throws FileNotFoundException{
		
		String day = DateUtil.toDay("yyyy-MM-dd");
		int index=Log2db.findLen(day);//查询数据库从指定的位置开始读取
		
		while(true){
			if((index= reader(index,day))==0){//如果index 返回0 说明已经是第二天了,需要更新下文件名称
				day = DateUtil.toDay("yyyy-MM-dd");
			}
		}
		
	}
	
	/**
	 * 日志读取核心操作
	 * @author lxycx_xc
	 */
	public static int reader(int line,String day) throws FileNotFoundException{//从指定行数开始读取
		
		String filename = "localhost_access_log."+day+".txt";
		File file = new File(logs+filename);
		BufferedReader br = new BufferedReader(new FileReader(file),1048576);
		//long d1 = System.currentTimeMillis();
		long filelen = file.length();
		int index=0;
		String len;
		while(true){
			try {
				len = br.readLine();
			} catch (Exception e) {//如果读取数据发生意外退出,返回当前读取的行数
				try {
					br.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				return index;
			}
			
			if(++index>line){//即将读取的行数
				
				if(len!=null){
					//开始读取
					if(len.indexOf(".mp4")>=0||len.indexOf(".MP4")>=0){
						Log2db.upLen(day,index);
						task.offer(len+" "+day+" "+index);//添加待处理任务
					}
				}else{
					if(DateUtil.toDay("yyyy-MM-dd").equals(day)||filelen
 
网友评论