原创

实现将本地日志文件输出到页面上

将本地日志文件输出到页面上

项目需求: 在项目开发过程中,日志信息是非常重要的,通常我们会将日志输出到服务器本地,在需要的时候手动从服务器上下载查看,这或多或少有点不方便,因此我考虑将服务器上的日志文件直接输出到网页上,通过url+servlet直接访问日志信息。

代码如下

public class LogOutput {
    public static void main(String[] args) throws IOException {
        String inputPath = "C://logs/LoggingDemo.txt";
        String outputPath = "C://logs/LoggingDemo.html";
        convertLogTxt2Html(inputPath, outputPath);
    }

    public static void convertLogTxt2Html(String inputPath, String outputPath) {
        if (inputPath == null || "".equals(inputPath)) {
            throw new RuntimeException("Convert Log File Failed: input file path is empty!");
        }

        if (outputPath == null || "".equals(outputPath)) {
            throw new RuntimeException("Convert Log File Failed: output file path is empty!");
        }

        BufferedReader reader;
        try {
            reader = new BufferedReader(new FileReader(inputPath));
            BufferedWriter writer = new BufferedWriter(new FileWriter(outputPath));

            String htmlHead = "<!doctype html><html lang=\\\"en\\\"><head></head><body><div>";
            String htmlTail = "</div></head>";

            writer.write(htmlHead + "<br>" + "\n");
            String line;
            while ((line = reader.readLine()) != null) {
                writer.write(line + "<br>" + "\n");
            }
            writer.write(htmlTail + "<br>");

            writer.flush();
            writer.close();
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Java
  • 作者:CoCo(联系作者)
  • 发表时间:2024-03-28 18:56:09
  • 更新时间:2024-03-28 22:54:36
  • 版权声明 © 原创不易,转载请注明出处
  • 留言