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

功能:

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());
}

%>
时间: 2024-08-15 07:17:25

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

django 异步 查看 服务器日志 | 利用 channels==2.0.2

django 异步 查看 服务器日志 实例 可以查看我编写的这个项目:https://github.com/hequan2017/chain 模块 需要安装以下模块 安装后会有一个版本号报错,不影响 channels==2.0.2 channels-redis==2.1.0 amqp==1.4.9 anyjson==0.3.3 asgi-redis==1.4.3 asgiref==2.3.0 async-timeout==2.0.0 attrs==17.4.0 cd /tmp/ wget htt

SSH查看服务器日志常用语句

SSH客户端查看服务器日志常用语句即为linux进行文件夹进行文件查看的常用语句: 1.cd 2.tail 3.grep [options] pattern file -c:只输出匹配行的计数.-I:不区分大 小写(只适用于单字符).-h:查询多文件时不显示文件名.-l:查询多文件时只输出包含匹配字符的文件名.-n:显示匹配行及 行号.-s:不显示不存在或无匹配文本的错误信息.-v:显示不包含匹配文本的所有行. 面对庞大的日志文件,单独下载并打开文件,从中提取信息的话,很吃力.这时,grep语句

2.Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程

 1  Lucen目录介绍 2  lucene-core-3.6.2.jar是lucene开发核心jar包 contrib  目录存放,包含一些扩展jar包 3  案例 建立第一个Lucene项目:lucene3_day1 (1)需要先将数据转换成为Document对象,每一个数据信息转换成为Field(String name, String value, Field.Store store, Field.Indexindex) (2)指定索引库位置Directorydirectory =

web站点服务器日志管理及分析

管理Web网站不只是监视Web的速度和Web的内容传送.它不仅要关注服务器每天的吞吐量,还要了解这些Web网站的外来访问,了解网站各页面的访问情况.根据各页面的点击频率来改善网页的内容和质量,提高内容的可读性,以及跟踪包含有商业交易的步骤及管理Web网站"幕后"的数据等. 为了更好地提供WWW服务,监控Web服务器的运行情况.了解网站内容的详细访问状况就显得越来越重要和迫切了.而这些要求都可以通过对Web服务器日志文件的统计和分析来实现.本文将对Web服务器日志分析的原理和技术进行讨论

Linux 日志切割工具cronolog

一.前言 二.cronolog 简介 三.cronolog 特点 四.cronolog 安装 五.cronolog 使用 六.cronolog 总结 注,操作系统 CentOS 6.4 x86_64,软件版本 cronolog 1.6.2,软件下载http://cronolog.org/download/index.html. 一.前言 大家都知道apache服务器,默认日志文件是不分割的,一个整文件既不易于管理,也不易于分析统计.本博文主要讲解Web服务器日志切割工具cronolog,下面我们

前段时间,接手一个项目使用的是原始的jdbc作为数据库的访问,发布到服务器上在运行了一段时间之后总是会出现无法访问的情况,登录到服务器,查看tomcat日志发现总是报如下的错误。    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected est

前段时间,接手一个项目使用的是原始的jdbc作为数据库的访问,发布到服务器上在运行了一段时间之后总是会出现无法访问的情况,登录到服务器,查看tomcat日志发现总是报如下的错误. Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too man

Linux服务器限制ssh登录,查看登录日志

网络上的服务器很容易受到攻击,最惨的就是被人登录并拿到root权限.有几个简单的防御措施: 1. 修改ssh服务的默认端口 ssh服务的默认端口是22,一般的恶意用户也往往扫描或尝试连接22端口.所以第一步就是修改这个默认端口 打开/etc/ssh/sshd_config,找到 Port 22 然后将22修改为其它没有被占用的端口,如1022.最好在1-1024之间,防止与用户进程端口冲突. 然后重启sshd即可 sudo /etc/init.d/ssh restart 2. 限制IP 首先修改

linux下搭建HTTP网站服务器和网站日志分析工具AWStats的使用

服务器IP地址:192.168.4.5 服务器主机名:srv5.tarena.com 1.在服务器端安装httpd软件包 [[email protected] /]# yum -y install httpd [[email protected] /]# service httpd start [[email protected] /]# chkconfig httpd on 2.在客户机端验证 在浏览器中输入192.168.4.5 如果显示欢迎页面表示服务器搭建成功 3.部署网页文档 首先将欢

[转]Oracle DB 查看预警日志

"Database(数据库)"主页>"Related Links相关链接)"区域> "Alert Log Content (预警日志内容)" 查看预警日志 每个数据库都有一个alert_<sid >.log文件.此文件位于数据库所在的服务器中,如果设置了$ORACLE_BASE,则此文件默认存储在$ORACLE_BASE/diag/rdbms/<db_name>/<SID>/trace中. 数据库预