2024-03-28
471
15 MIN READ
项目需求: 在项目开发过程中,日志信息是非常重要的,通常我们会将日志输出到服务器本地,在需要的时候手动从服务器上下载查看,这或多或少有点不方便,因此我考虑将服务器上的日志文件直接输出到网页上,通过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();
}
}
}