【问题标题】:Is there a way to view my web-apps tomcat logs in a browser real time?有没有办法在浏览器中实时查看我的 web-apps tomcat 日志?
【发布时间】:2013-08-24 13:11:18
【问题描述】:

我正在使用 log4j 来记录我的数据。我希望能够在浏览器中与我的网络应用程序一起实时查看日志文件。有像 Chainsaw 这样的独立工具非常好,但它们不能用于在浏览器中实时查看日志的目的。

谁能帮我解决这个问题?

【问题讨论】:

  • 您可以使用一些使用超时的 javascript 创建一个页面,并通过 AJAX 调用一个 servlet(它将读取并返回日志)并在 div/textarea 中显示内容。更进化的 servlet 将只返回日志中的新内容
  • 你有什么解决办法吗?请告诉我我也想要。

标签: java tomcat logging log4j


【解决方案1】:

您可以使用psi-probe。它具有实时访问日志、更改日志级别、下载等的内置功能...

【讨论】:

    【解决方案2】:

    一个简单的例子是:

    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 style="width: 100%; padding: 0; margin: 0">
      <input type="button" id="tog" onclick="toggleLogs()" value="Start" />
      <div id="log" style="width: 100%; padding: 0; margin: 0"></div>
     </body>
    </html>
    

    【讨论】:

    • 感谢您的回复。实际上,我需要一个 GUI 实用程序,而不是您建议的。像logdigger 这样的东西。我不想自己创建一个html。我需要一个实用程序来为我做这件事。
    • @ProgrammingPanda 请发布您最终集成的解决方案..寻找相同的..
    猜你喜欢
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多