测试例子—通过shell命令读取Tomcat的日志
public static String getLog() throws IOException { Connection conn = getConnection(); //Tomcat的日志文件名带有日期,因此可以设置一个日期时间 格式日期格式 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); //获取当前时间 Date date = new Date(); String now = simpleDateFormat.format(date); //编写Tomcat日志所在的路径 //需要读取的catalina.log文件 全路径 查看异常 错误日志路径 String logFile = "/home/pan/tomcat/logs/catalina."+now+".log"; //获取tomcat一日的访问总数日志路径 和 哪个时间点有请求 String logFile1 = "/home/pan/tomcat/logs/localhost_access_log.“+now+”.txt"; //关键字 String key = "Exception"; //查看tomcat异常命令 String cmd = "cat " +logFile+" | grep "+key+" -A 1"; //查看一日访问/请求命令 String cmd1 = "cat "+logFile1+"|awk '{aaa[$1]++;} END{for(i in aaa) { printf(\"%s\\t%s\\n\", aaa[i], i); }}' | sort -bn"; //哪个时间点有请求 String cmd2 = "cat "+logFile1+"|cut -d : -f 2-3 |sort|uniq -c"; Session session = conn.openSession(); /执行shell命令 session.execCommand(cmd1); //处理获取的shell命令的输出信息 InputStream stdout = session.getStdout(); InputStreamReader inputStreamReader = new InputStreamReader(stdout); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String s = bufferedReader.readLine(); String lastLineLog = null; System.out.println("----------以下是获取日志的全部信息--------------"); while (s!=null){ if (s!=null){ lastLineLog = s; System.out.println(s); } s = bufferedReader.readLine(); } System.out.println("----------以上是获取日志的全部信息--------------"); //最后关闭session资源 if (session != null){ session.close(); } return lastLineLog; }至于每日访客量,请求总数,哪个时间点有请求就把拿到的数据解析就完事了。shell命令已经写好在上面了 。