51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1922|回复: 3
打印 上一主题 下一主题

[资料] 【seagull1985】简单实现WEB页面查看LOG4J日志

[复制链接]
  • TA的每日心情
    奋斗
    2018-8-27 15:56
  • 签到天数: 322 天

    连续签到: 1 天

    [LV.8]测试军长

    跳转到指定楼层
    1#
    发表于 2016-8-8 11:11:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 seagull1985 于 2016-8-8 11:14 编辑

    在自动化框架中,日志一般会有2个部分:一块是用例日志,用来记录用例每条用例的执行过程,这一部分我们直接存放在数据库,方便能通过每条用例定位到对应的日志

    另一块是LOG4J的系统日志,用来记录框架的运行过程。而LOG4J是记录在日志文件中,每次看日志都要去服务器上或是下载下来看,这里我们做一个简单的实现,通过WEB端直接查看LOG4J的日志。

    页面HTML      log4j.jsp
    1. <%        response.setContentType("application/unknown;charset=gbk");
    2.         response.setHeader("Content-disposition","filename=" + (String) request.getAttribute("filename"));
    3.         out.println((String) request.getAttribute("data"));
    4. %>
    复制代码


    JAVA服务端处理
    1. /**
    2.          * 页面日志展示
    3.          *
    4.          * @param request
    5.          * @param response
    6.          * @return
    7.          * @throws Exception
    8.          */
    9.         @RequestMapping(value = "/down.do")
    10.         public String download(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception
    11.         {
    12.                 String storeName = "APP.log";
    13.                 String name = request.getParameter("startDate");
    14.                 // String name = date;
    15.                 if (!DateLib.today("yyyy-MM-dd").equals(name))
    16.                 {
    17.                         storeName = storeName + "." + name;
    18.                 }
    19.                 String contentType = "application/octet-stream";
    20.                 //String ctxPath=getClass().getResource("/").getFile().toString();
    21.                 String ctxPath = "D:\\log\\";
    22.                 String downLoadPath = ctxPath + storeName;
    23.                 File file = new File(downLoadPath);
    24.                 if (!file.exists())
    25.                 {
    26.                         model.addAttribute("message", downLoadPath + "(系统找不到指定的文件。)");
    27.                         model.addAttribute("url", "/testJobs/list.do");
    28.                         return "success";
    29.                 }
    30.                 String data = download(request, response, storeName, contentType,ctxPath);
    31.                 model.addAttribute("filename", storeName);
    32.                 model.addAttribute("data", data);
    33.                 return "log4j";
    34.         }

    35.         /**
    36.          * 获取服务端日志
    37.          *
    38.          * @param request
    39.          * @param response
    40.          * @param storeName
    41.          * @param contentType
    42.          * @param realName
    43.          * @throws Exception
    44.          */
    45.         public static String download(HttpServletRequest request, HttpServletResponse response, String storeName,
    46.                 String contentType,String ctxPath) throws Exception
    47.         {
    48.                 response.setContentType("text/html;charset=gbk");
    49.                 request.setCharacterEncoding("gbk");
    50.                 BufferedReader bos = null;

    51.                 //String ctxPath = "E:\\log\\";
    52.                 String downLoadPath = ctxPath + storeName;

    53.                 long fileLength = new File(downLoadPath).length();

    54.                 response.setContentType(contentType);
    55.         //        response.setHeader("Content-disposition", "attachment; filename="+new String(storeName.getBytes("utf-8"), "ISO8859-1"));
    56.         /*        response.setHeader("Content-disposition", "attachment; filename="
    57.                         + new String(storeName.getBytes("gbk"), "ISO8859-1"));*/
    58.                 response.setContentType("multipart/form-data");
    59.                 response.setHeader("Content-Length", String.valueOf(fileLength));
    60.                 String str = "";
    61.                 InputStreamReader isr = new InputStreamReader(new FileInputStream(downLoadPath), "UTF-8");   //转成UTF-8后,页面不会乱码,注意!
    62.                 bos = new BufferedReader(isr);
    63.                 //bos = new BufferedReader(new FileReader(downLoadPath));
    64.                 StringBuffer sb = new StringBuffer();
    65.                 while ((str = bos.readLine()) != null)
    66.                 {
    67.                         sb.append(str).append("\n");
    68.                 }
    69.                 bos.close();
    70.                 return sb.toString();
    71.         }
    复制代码


    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    无聊
    12 小时前
  • 签到天数: 939 天

    连续签到: 1 天

    [LV.10]测试总司令

    2#
    发表于 2016-8-8 11:58:59 | 只看该作者
    支持下版主的分享!
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    3#
    发表于 2016-8-17 17:46:18 | 只看该作者
    哈哈,支持楼主
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    4#
    发表于 2016-8-23 15:53:08 | 只看该作者
    学习了,收藏先
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-28 21:48 , Processed in 0.069971 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表