【seagull1985】简单实现WEB页面查看LOG4J日志
本帖最后由 seagull1985 于 2016-8-8 11:14 编辑在自动化框架中,日志一般会有2个部分:一块是用例日志,用来记录用例每条用例的执行过程,这一部分我们直接存放在数据库,方便能通过每条用例定位到对应的日志
另一块是LOG4J的系统日志,用来记录框架的运行过程。而LOG4J是记录在日志文件中,每次看日志都要去服务器上或是下载下来看,这里我们做一个简单的实现,通过WEB端直接查看LOG4J的日志。
页面HTML log4j.jsp
<% response.setContentType("application/unknown;charset=gbk");
response.setHeader("Content-disposition","filename=" + (String) request.getAttribute("filename"));
out.println((String) request.getAttribute("data"));
%>
JAVA服务端处理
/**
* 页面日志展示
*
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping(value = "/down.do")
public String download(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception
{
String storeName = "APP.log";
String name = request.getParameter("startDate");
// String name = date;
if (!DateLib.today("yyyy-MM-dd").equals(name))
{
storeName = storeName + "." + name;
}
String contentType = "application/octet-stream";
//String ctxPath=getClass().getResource("/").getFile().toString();
String ctxPath = "D:\\log\\";
String downLoadPath = ctxPath + storeName;
File file = new File(downLoadPath);
if (!file.exists())
{
model.addAttribute("message", downLoadPath + "(系统找不到指定的文件。)");
model.addAttribute("url", "/testJobs/list.do");
return "success";
}
String data = download(request, response, storeName, contentType,ctxPath);
model.addAttribute("filename", storeName);
model.addAttribute("data", data);
return "log4j";
}
/**
* 获取服务端日志
*
* @param request
* @param response
* @param storeName
* @param contentType
* @param realName
* @throws Exception
*/
public static String download(HttpServletRequest request, HttpServletResponse response, String storeName,
String contentType,String ctxPath) throws Exception
{
response.setContentType("text/html;charset=gbk");
request.setCharacterEncoding("gbk");
BufferedReader bos = null;
//String ctxPath = "E:\\log\\";
String downLoadPath = ctxPath + storeName;
long fileLength = new File(downLoadPath).length();
response.setContentType(contentType);
// response.setHeader("Content-disposition", "attachment; filename="+new String(storeName.getBytes("utf-8"), "ISO8859-1"));
/* response.setHeader("Content-disposition", "attachment; filename="
+ new String(storeName.getBytes("gbk"), "ISO8859-1"));*/
response.setContentType("multipart/form-data");
response.setHeader("Content-Length", String.valueOf(fileLength));
String str = "";
InputStreamReader isr = new InputStreamReader(new FileInputStream(downLoadPath), "UTF-8"); //转成UTF-8后,页面不会乱码,注意!
bos = new BufferedReader(isr);
//bos = new BufferedReader(new FileReader(downLoadPath));
StringBuffer sb = new StringBuffer();
while ((str = bos.readLine()) != null)
{
sb.append(str).append("\n");
}
bos.close();
return sb.toString();
}
支持下版主的分享! 哈哈,支持楼主 学习了,收藏先
页:
[1]