@GetMapping(value = "/logs", produces = MediaType.TEXT_HTML_VALUE + ";charset=UTF-8")
public String logs() throws IOException {
List<OrderWrapper> list = getLogFileNameList();
if (CollectionUtils.isEmpty(list)) {
return "no logs";
}
if (list.size() > 500) {
return "log file size is more then 500.";
}
StringBuilder sb = new StringBuilder();
sb.append("<table align='center' border='1'>");
sb.append("<thead>");
sb.append("<tr>");
sb.append("<th>").append("日志文件名").append("</th>");
sb.append("<th>").append("大小").append("</th>");
sb.append("<th>").append("下载").append("</th>");
// sb.append("<th>").append("查看").append("</th>");
// sb.append("<th>").append("记录开始时间").append("</th>");
sb.append("</tr>");
sb.append("</thead>");
for (OrderWrapper wrapper : list) {
String name = wrapper.getName();
File file = new File(infoConfigLoggerPath + CommonConstant.SPRIT + name);
sb.append("<tr>");
sb.append("<td>").append(name).append("</td>");
sb.append("<td>").append(file.length()).append("byte</td>");
sb.append("<td>").append("<a href='logs/download/").append(name).append("'>Download</a>").append("</td>");
// sb.append("<td>").append("<a href='logs/view/").append(name).append("/-1'>View</a>").append("</td>");
// String start = null;
// try (FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr)) {
// String line = br.readLine();
// if (line != null && line.length() > 23) {
// start = line.substring(0, 23);
// }
// }
// sb.append("<td>").append(start).append("</td>");
sb.append("</tr>");
}
sb.append("</table>");
return sb.toString();
}
private List<OrderWrapper> getLogFileNameList() {
List<OrderWrapper> list = new ArrayList<>();
File dir = new File(infoConfigLoggerPath);
if (dir.exists()) {
File[] files = dir.listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
if (file.isFile()) {
list.add(new OrderWrapper(file.getName(), OrderWrapperSort.desc));
}
}
}
}
Collections.sort(list);
return list;
}
@GetMapping(value = "/logs/view/{name}/{lines}", produces = MediaType.TEXT_HTML_VALUE + ";charset=UTF-8")
public String logsView(@PathVariable("name") String name, @PathVariable("lines") Integer lines) throws IOException {
File file = new File(infoConfigLoggerPath + File.separator + name);
double max = 1024 * 1024 * 5.1;
if (file.length() >= max) {
return "The log file is too large, please download it.";
}
List<String> list = new ArrayList<>();
try (FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr)) {
String line;
while ((line = br.readLine()) != null) {
list.add(line);
}
StringBuilder builder = new StringBuilder();
builder.append("<PRE>");
if (lines > 0) {
int start = Math.max(list.size() - lines, 0);
int end = list.size();
for (int i = start; i < end; i++) {
builder.append(list.get(i));
builder.append("\r\n");
}
} else {
if (file.length() > max) {
return "The log file is too large, please download it.";
}
for (String row : list) {
builder.append(row);
builder.append("\r\n");
}
}
builder.append("</PRE>");
return builder.toString();
}
}
@GetMapping(value = "/logs/download/{name}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public void logsDownload(HttpServletResponse response, @PathVariable("name") String name) throws IOException {
String pathname = infoConfigLoggerPath + CommonConstant.SPRIT + name;
File file = new File(pathname);
download(response, file);
}
public void download(@NotNull HttpServletResponse response, @NotNull File file) throws IOException {
String name = URLEncoder.encode(file.getName(), CommonConstant.ENCODING);
response.setCharacterEncoding(CommonConstant.ENCODING);
response.setHeader("Content-disposition", "attachment; filename=\"" + name + "\"");
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
Path source = Paths.get(file.getPath());
Files.copy(source, response.getOutputStream());
}
public void downloadFile(HttpServletResponse response, File file) {
try (InputStream is = new FileInputStream(file);
BufferedReader in = new BufferedReader(new InputStreamReader(is));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream()))) {
String name = URLEncoder.encode(file.getName(), CommonConstant.ENCODING);
response.setCharacterEncoding(CommonConstant.ENCODING);
response.setHeader("Content-disposition", "attachment; filename=\"" + name + "\"");
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
String line;
while ((line = in.readLine()) != null) {
out.append(line);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
private List<OrderWrapper> getLogFileNameList() {
List<OrderWrapper> list = new ArrayList<>();
File dir = new File(infoConfigLoggerPath);
if (dir.exists()) {
File[] files = dir.listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
if (file.isFile()) {
list.add(new OrderWrapper(file.getName(), OrderWrapperSort.desc));
}
}
}
}
Collections.sort(list);
return list;
}