java 实现 tail -f 实时查看日志

文章目录

    • 实现方式
    • 效果图
    • 项目代码大体展示
      • contorller层
      • service层
      • modelAndView层
    • 项目源码下载地址

实现方式

1.参考开源项目finderWeb实现日志实时展示

2.前端实现方式采用的layui框架提交的ajax

效果图

选择目录、选择该目录下的文件、选择查看文件的编码{默认UTF-8}

java 实现 tail -f 实时查看日志_第1张图片

点击提交按钮,就可以实时查看该文件的内容

java 实现 tail -f 实时查看日志_第2张图片

项目代码大体展示

contorller层

package com.zkjw.cms.plugins.finderWeb;

import com.zkjw.cms.common.utils.HttpRequestUtil;
import com.zkjw.cms.plugins.finderWeb.model.Finder;
import com.zkjw.cms.plugins.finderWeb.service.FinderWebServce;
import com.zkjw.cms.plugins.finderWeb.service.LessServlet;
import com.zkjw.cms.plugins.finderWeb.template.TailTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @description tail -f  查看日志
 * @author liuhongrong
 * @since 2023/11/13 12:59
 * @desc
 */
@Controller
@RequestMapping("/finderWeb")
public class FinderWebController {


    /**
     * 如何在window下像linux下一样tail -f catalina.out查看tomcat日志
     * 原文链接:https://blog.csdn.net/bud407/article/details/84534416
     *
     * 大家都有在linux下使用tail -f catalina.out查看tomcat日志的习惯,
     * 在window下可以吗?当然可以下面就介绍一下如何操作。
     * 1、打开bin下面的startup.bat文件,把call "%EXECUTABLE%" start %CMD_LINE_ARGS%
     * 改为call "%EXECUTABLE%" run %CMD_LINE_ARGS% 。
     * 2、打开bin下面的catalina.bat文件,会发现共有4处%ACTION%,在后面分别加上
     * “>>%CATALINA_BASE%\logs\catalina.out”。
     * 重启tomcat,就会发现在logs文件夹下出现了catalina.out文件,把原来控制台的信息全写进去了。
     *
     * 如果中文乱码则添加{-Dfile.encoding="UTF-8"}
     * -Dcatalina.home="%CATALINA_HOME%" -Dfile.encoding="UTF-8"
     */

    FinderWebServce finderWebServce =new  FinderWebServce();

    /**
     * 查看实时日志
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value = "/lessGetTail",method = {RequestMethod.GET,RequestMethod.POST})
    public void getTail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        File file = Finder.getFile(request);
        finderWebServce.getTail(request, response, file);
    }

    /**
     * 加载实时日志界面,并获取容器日志目录
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value = "/finderTail",method = {RequestMethod.GET,RequestMethod.POST})
    public void tail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if(LessServlet.prepare(request, response)) {
            TailTemplate.execute(request, response);
        }
    }
}

service层

/**
 * @param request
 * @param response
 * @return boolean
 * @throws ServletException
 * @throws IOException
 */
public static boolean prepare(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String request_workspace = request.getParameter("workspace");
    String request_path = request.getParameter("path");
    String work ="";//日志工作空间
    String path ="";//文件名称
    File file_workspace = new File(request_workspace);
    if(file_workspace.exists() || file_workspace.isFile()) {
        work=request_workspace;
        path=request_path;
    }else{
        work = Path.getWebWork(request);
        path= Path.getWebPath(request,work);
    }
    String charset = request.getParameter("charset");
    String realPath = Finder.getRealPath(work, path);

    if(realPath == null) {
        FinderServlet.error(request, response, 404, work + "/" + path + " not exists.");
        return false;
    }
    if(charset == null || charset.trim().length() < 1) {
        charset = "utf-8";
    }
    File file = new File(realPath);
    if(!file.exists() || !file.isFile()) {
        FinderServlet.error(request, response, 404, realPath + " not exists.");
        return false;
    }
    String parent = Path.getRelativePath(work, file.getParent());
    String relativePath = Path.getRelativePath(work, realPath);
    request.setAttribute("workspace", request_workspace);
    request.setAttribute("work", work);
    request.setAttribute("path", relativePath);
    request.setAttribute("parent", parent);
    request.setAttribute("charset", charset);
    request.setAttribute("absolutePath", file.getCanonicalPath());
    return true;
}

modelAndView层

/**
 * @param request
 * @param response
 * @throws IOException
 * @throws ServletException
 */
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String path=request.getScheme()+"://"+request.getServerName()+":"+request.getLocalPort()+request.getContextPath();
    response.setContentType("text/html; charset=utf-8");
    PrintWriter out = response.getWriter();
    out.write("\r\n");
    out.write("\r\n\r\n\r\n");
    out.write("\r\n\r\n");
    out.write("\r\nWebTail\r\n);
    this.print(out, request.getAttribute("requestURI"));
    out.write(path+"/web/assets/libs/finder/css/less.css\"/>\r\n\r\n\r\n\r\n\r\n\r\n\r\n);
    this.print(out, request.getAttribute("contextPath"));
    out.write("\" workspace=\"");
    this.print(out, request.getAttribute("workspace"));
    out.write("\" parent=\"");
    this.print(out, request.getAttribute("parent"));
    out.write("\" path=\"");
    this.print(out, request.getAttribute("path"));
    out.write("\" charset=\"");
    this.print(out, request.getAttribute("charset"));
    out.write("\">\r\n\r\n
\r\n"
); out.write("
\r\n
\r\n"); out.write(" \r\n \r\n \r\n"); out.write(" \r\n"); out.write(" \r\n"); out.write(" \r\n"); out.write(" \r\n"); out.write(" \r\n 重载时间:\r\n"); out.write(" 自动滚动\r\n"); out.write("
\r\n
\r\n
\r\n"); out.write("
\r\n 查找内容: \r\n"); out.write(" \r\n"); out.write("
\r\n
\r\n \r\n"); out.write(" 正则表达式\r\n"); out.write("
\r\n
\r\n

提示:快捷键(Ctrl + B),再次按下可关闭。

\r\n"); out.write("

说明:正则相关文档请参考JavaScript正则表达式 。

\r\n"
); out.write("
\r\n
\r\n \r\n"); out.write("
\r\n
\r\n\r\n"
); out.flush(); }

项目源码下载地址

链接:https://pan.baidu.com/s/1LKEIes6R4IUQo-ZOnZLtJw
提取码:WSSB

你可能感兴趣的:(java,开发语言)