一个j2ee测试神器,方便查看服务器日志的工具

2 篇文章 0 订阅
1 篇文章 0 订阅

功能:

1,能够将服务器上的指定的文件通过浏览器倒序显示处理,如果文件是系统日志,则可以用来查日志信息。

2,可以倒序显示指定的行数,默认100行,通过参数count=500可以修改显示行数。

3,只按行显示行数,默认最大10000行,支持大文件或超大文件,就算文件有1G。

4,可以与操作事件配合,执行一个动作,刷一下就能查看最新的。

5,结合浏览器本身的查找功能,可以方便查找想要的信息。


其他功能:

1,filter支持关键字查询,如:filter=findData,则只显示含有findData的日志信息。

2,支持stopKey,如:stopKey=findData,则只要找到含有findData的信息,就停止继续查询。

3,文件同时也可以配置成其他日志文件,如:nginx日志,或其他日志,效果一样。


使用方式:

将以下代码,存成jsp,如:logs.jsp,并修改代码中的filePath为实际的日志文件。

然后通过浏览器访问,如:http://localhost:8080/logs.jsp


<%@ page contentType="text/html;charset=UTF-8"
    pageEncoding="UTF-8" import="java.net.*,java.io.*,java.util.*"%>

<%!
int MAX_TOTAL_ROW_READ=10000;
List<String> readLineFile(File file, int maxCount, Map<String, String> filterMap, String charset) throws Exception {
	long time=System.currentTimeMillis();
	
	List<String> lineList = new ArrayList();
	if(charset==null ||"".equals(charset)){
		charset = "UTF-8";
	}
	
	int count = 0;
	int totalCount=0;
	
	RandomAccessFile rf = null;
	try {
		rf = new RandomAccessFile(file, "r");
		long len = rf.length();
		long start = rf.getFilePointer();
		long nextend = start + len - 1;
		String line;
		rf.seek(nextend);
		int c = -1;
		String filter=filterMap.get("filter");
		String stop_key=filterMap.get("stop_key");
		String hide_key=filterMap.get("hide_key");
		if(stop_key!=null && (stop_key.equals("") ||stop_key.equals("null"))){
			stop_key=null;
		}
		if(hide_key!=null && (hide_key.equals("") ||hide_key.equals("null"))){
			hide_key=null;
		}
		String[] hides=null;
		if(hide_key!=null){
			hides=hide_key.split(",");
		}
		
		
		boolean isHide=false;
		while (nextend > start) {
			c = rf.read();
			if (c == '\n' || c == '\r') {
				totalCount++;
				if(totalCount>MAX_TOTAL_ROW_READ){
					break;
				}
				if (count > maxCount) {
					break;
				}
				line = rf.readLine();
				if (line != null) {
					line = new String(line.getBytes("ISO-8859-1"), charset);
					//stop
					if(stop_key!=null && line.indexOf(stop_key)>=0){
						break;
					}
					//hide
					if(hides!=null){
						isHide=false;
						for(String hide:hides){
							if(hide.equals("hide_empty") && "".equals(line)){
								isHide=true;
								break;
							}
							else if(line.indexOf(hide)>=0){
								isHide=true;
								break;
							}
						}
						if(isHide){
							continue;
						}
					}
					//filter
					if(filter==null|| line.indexOf(filter)>=0){
						count++;
						lineList.add(line);
					}
				}
				nextend--;
			}
			nextend--;
			rf.seek(nextend);
			if (nextend == 0) {// output first line on seek file head
				line = rf.readLine();
				line = new String(line.getBytes("ISO-8859-1"), charset);
				lineList.add(line);
			}
		}
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		try {
			if (rf != null)
				rf.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	System.out.println("totalCount="+totalCount+" foundCount="+count+" time="+(System.currentTimeMillis()-time));
	return lineList;
}
%>

<%
String filePath="d:/log4j/logs.log";
int max_line_count=100;
String counts=request.getParameter("count");//stop on find count
String filter=request.getParameter("filter");//find by filter value
String stopKey=request.getParameter("stopKey");//find stop on find stopKey
String hideKey=request.getParameter("hideKey");//hide find hideKey

long time=System.currentTimeMillis();

try{
	int cout=Integer.parseInt(counts);
	max_line_count=cout;
}catch(Exception e){
	
}

try{
	File file = new File(filePath);
	if(!file.exists()){
		String info=filePath+" not found!";
		out.print(info);
		System.out.println(info);
	}
	else{
		Map<String, String> filterMap=new HashMap();
		filterMap.put("filter", filter);
		filterMap.put("stop_key", stopKey);
		filterMap.put("hide_key", hideKey);
		List<String> lineList=readLineFile(file, max_line_count, filterMap, null);
		for(String line:lineList){
			out.print(line+"<br/>\n");
		}
	}
}
catch(Exception e){
	out.println(e.getMessage());
}


%>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值