优化面板日志
This commit is contained in:
parent
b0b6c6dfad
commit
34890faee6
@ -8,6 +8,7 @@ import com.goeing.printserver.main.domain.request.PrintRequest;
|
||||
import com.goeing.printserver.main.service.PrintQueueService;
|
||||
import com.goeing.printserver.main.service.PrintService;
|
||||
import com.goeing.printserver.main.utils.PdfPrinter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@ -23,6 +24,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
@Slf4j
|
||||
public class PrintController implements PrintService {
|
||||
|
||||
@Autowired
|
||||
@ -93,7 +95,7 @@ public class PrintController implements PrintService {
|
||||
@PostMapping("print")
|
||||
public String print(@RequestBody PrintRequest request) {
|
||||
// 记录请求信息
|
||||
System.out.println("Received print request: " + JSONUtil.toJsonPrettyStr(request));
|
||||
log.info("收到打印请求: {}", JSONUtil.toJsonPrettyStr(request));
|
||||
|
||||
// 参数验证
|
||||
if (request == null) {
|
||||
@ -111,7 +113,7 @@ public class PrintController implements PrintService {
|
||||
throw new IllegalArgumentException("Printer name cannot be null or empty, and no default printer is configured");
|
||||
}
|
||||
request.setPrinterName(defaultPrinter);
|
||||
System.out.println("Using default printer: " + defaultPrinter);
|
||||
log.info("使用默认打印机: {}", defaultPrinter);
|
||||
}
|
||||
|
||||
// 验证打印机是否存在
|
||||
@ -135,7 +137,7 @@ public class PrintController implements PrintService {
|
||||
|
||||
try {
|
||||
// 下载文件
|
||||
System.out.println("Downloading file from: " + fileUrl);
|
||||
log.info("正在从以下地址下载文件: {}", fileUrl);
|
||||
HttpUtil.downloadFile(fileUrl, filePath);
|
||||
|
||||
if (!pdfFile.exists() || pdfFile.length() == 0) {
|
||||
@ -143,12 +145,12 @@ public class PrintController implements PrintService {
|
||||
}
|
||||
|
||||
// 打印文件
|
||||
System.out.println("Printing file to printer: " + request.getPrinterName());
|
||||
log.info("正在将文件发送到打印机: {}", request.getPrinterName());
|
||||
PdfPrinter.print(filePath, request.getPrinterName(), request.getPrintOption());
|
||||
|
||||
return "success";
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error during print process: " + e.getMessage());
|
||||
log.error("打印过程中发生错误: {}", e.getMessage(), e);
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("Print failed: " + e.getMessage(), e);
|
||||
} finally {
|
||||
@ -156,9 +158,9 @@ public class PrintController implements PrintService {
|
||||
if (pdfFile.exists()) {
|
||||
try {
|
||||
pdfFile.delete();
|
||||
System.out.println("Temporary file deleted: " + filePath);
|
||||
log.debug("临时文件已删除: {}", filePath);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to delete temporary file: " + filePath);
|
||||
log.warn("删除临时文件失败: {}", filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,14 +25,32 @@ public class SwingLogAppender extends AppenderBase<ILoggingEvent> {
|
||||
// 转换日志级别
|
||||
SystemLogPanel.LogLevel level = convertLogLevel(event.getLevel());
|
||||
|
||||
// 格式化日志消息
|
||||
String message = String.format("[%s] %s",
|
||||
event.getLoggerName(),
|
||||
// 格式化日志消息,包含时间戳
|
||||
String timestamp = java.time.LocalDateTime.ofInstant(
|
||||
java.time.Instant.ofEpochMilli(event.getTimeStamp()),
|
||||
java.time.ZoneId.systemDefault()
|
||||
).format(java.time.format.DateTimeFormatter.ofPattern("HH:mm:ss.SSS"));
|
||||
|
||||
String loggerName = event.getLoggerName();
|
||||
// 简化logger名称,只显示最后两个包名
|
||||
String[] parts = loggerName.split("\\.");
|
||||
if (parts.length > 2) {
|
||||
loggerName = parts[parts.length - 2] + "." + parts[parts.length - 1];
|
||||
}
|
||||
|
||||
String message = String.format("%s [%s] %s",
|
||||
timestamp,
|
||||
loggerName,
|
||||
event.getFormattedMessage());
|
||||
|
||||
// 如果有异常信息,添加到消息中
|
||||
if (event.getThrowableProxy() != null) {
|
||||
message += "\n" + event.getThrowableProxy().getMessage();
|
||||
message += "\n 异常: " + event.getThrowableProxy().getMessage();
|
||||
// 添加异常堆栈的前几行
|
||||
if (event.getThrowableProxy().getStackTraceElementProxyArray() != null &&
|
||||
event.getThrowableProxy().getStackTraceElementProxyArray().length > 0) {
|
||||
message += "\n 位置: " + event.getThrowableProxy().getStackTraceElementProxyArray()[0].toString();
|
||||
}
|
||||
}
|
||||
|
||||
// 添加到日志面板
|
||||
|
||||
@ -201,9 +201,8 @@ public class SystemLogPanel extends JPanel implements LocaleChangeListener {
|
||||
}
|
||||
}
|
||||
|
||||
// 格式化日志消息
|
||||
String formattedMessage = String.format("[%s] [%s] %s\n",
|
||||
entry.timestamp.format(dateFormatter),
|
||||
// 格式化日志消息(SwingLogAppender已经包含时间戳,这里只添加级别)
|
||||
String formattedMessage = String.format("[%s] %s\n",
|
||||
entry.level.name(),
|
||||
entry.message);
|
||||
|
||||
|
||||
@ -23,6 +23,7 @@ public class MessageUtils {
|
||||
defaultMessageSource.setBasenames("messages");
|
||||
defaultMessageSource.setDefaultEncoding("UTF-8");
|
||||
messageSource = defaultMessageSource;
|
||||
// 静态初始化时无法使用日志,使用System.out
|
||||
System.out.println("MessageUtils static initialization with default messageSource");
|
||||
}
|
||||
|
||||
@ -33,6 +34,7 @@ public class MessageUtils {
|
||||
public void init() {
|
||||
// Spring容器启动后,使用注入的MessageSource替换默认的
|
||||
messageSource = autowiredMessageSource;
|
||||
// 初始化时的调试信息,使用System.out
|
||||
System.out.println("MessageUtils initialized with autowired messageSource: " + (messageSource != null));
|
||||
}
|
||||
|
||||
@ -68,6 +70,7 @@ public class MessageUtils {
|
||||
public static String getMessage(String code, Object[] args, String defaultMessage) {
|
||||
// 添加空检查,如果messageSource为null,返回默认消息或代码
|
||||
if (messageSource == null) {
|
||||
// MessageSource为null时的警告,使用System.err避免循环依赖
|
||||
System.err.println("Warning: MessageSource is null when getting message for code: " + code);
|
||||
return defaultMessage.isEmpty() ? code : defaultMessage;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user