有没有办法在浏览器中实时查看我的Web应用程序tomcat日志?

有没有办法在浏览器中实时查看我的Web应用程序tomcat日志?,第1张

有没有办法在浏览器中实时查看我的Web应用程序tomcat日志

一个简单的例子是:

Servlet(根据需要更改日志文件的路径):

@WebServlet(name = "Log", urlPatterns = { "/log" }) public class LogServlet extends HttpServlet {   private static final long serialVersionUID = 7503953988166684851L;  public LogServlet() {     super();   }  @Override   protected void doGet(HttpServletRequest req, HttpServletResponse resp)       throws ServletException, IOException {     Path path = FileSystems.getDefault()          .getPath("/path/to/tomcat/logs", "catalina.out");     StringBuilder logContent = new StringBuilder();     logContent.append("<pre>");     try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);) {         String line = null;         while ((line = reader.readLine()) != null) {  logContent.append(line).append("<br/>");         }     } catch (IOException x) {         // Take care of that     }     logContent.append("</pre>");     resp.getWriter().print(logContent.toString());   }  @Override   public void init(ServletConfig servletConfig) throws ServletException {     super.init(servletConfig);   } }

HTML页面:

<!DOCTYPE html><html> <head>  <meta charset="UTF-8">  <title>Log viewer</title>  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>  <script>window.jQuery || document.write(unescape('%3Cscript src="http://jquery.com/jquery-wp-content/themes/jquery/js/jquery-1.10.2.min.js"%3E%3C/script%3E'))  </script>  <script type="text/javascript">   var logging = false;   function refreshLog() {    if (logging) {     $.get('/log', function(data) {      $('#log').html(data);     });    }    if (logging) {      setTimeout(function() {       refreshLog()      }, 5000);    }   }   function toggleLogs() {    if (logging) {     logging = false;     $("#tog").val("Start");    } else {     logging = true;     $("#tog").val("Stop");     refreshLog();    }   }  </script> </head> <body >  <input type="button" id="tog" onclick="toggleLogs()" value="Start" />  <div id="log" ></div> </body></html>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5442383.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存