自定义日志管理
This commit is contained in:
parent
dd3f70c324
commit
b3a3d7fad9
@ -10,39 +10,8 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GoeingPrintServerApplication {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GoeingPrintServerApplication.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 检查是否在macOS系统上运行
|
||||
String osName = System.getProperty("os.name").toLowerCase();
|
||||
boolean isMacOS = osName.contains("mac");
|
||||
|
||||
// 检查是否已经设置了java.awt.headless系统属性
|
||||
String headlessProperty = System.getProperty("java.awt.headless");
|
||||
|
||||
// 如果是macOS并且没有明确设置headless属性,可能需要特殊处理
|
||||
if (isMacOS && headlessProperty == null) {
|
||||
log.info("在macOS系统上运行,检查是否需要启用无头模式");
|
||||
|
||||
// 检查是否支持图形界面
|
||||
if (GraphicsEnvironment.isHeadless()) {
|
||||
log.warn("检测到系统不支持图形界面,自动启用无头模式");
|
||||
System.setProperty("java.awt.headless", "true");
|
||||
System.setProperty("app.headless.mode", "true");
|
||||
}
|
||||
}
|
||||
|
||||
ConfigurableApplicationContext context = SpringApplication.run(GoeingPrintServerApplication.class, args);
|
||||
|
||||
// 从配置中读取是否强制使用无头模式
|
||||
Environment env = context.getEnvironment();
|
||||
boolean forceHeadless = Boolean.parseBoolean(env.getProperty("app.force.headless", "false"));
|
||||
|
||||
if (forceHeadless) {
|
||||
log.info("根据配置强制启用无头模式");
|
||||
System.setProperty("java.awt.headless", "true");
|
||||
System.setProperty("app.headless.mode", "true");
|
||||
}
|
||||
SpringApplication.run(GoeingPrintServerApplication.class, args);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -35,6 +35,9 @@ public class PrintController implements PrintService {
|
||||
@Autowired
|
||||
private PrintServerConfig config;
|
||||
|
||||
@Autowired
|
||||
private com.goeing.printserver.main.sse.PrinterClient printerClient;
|
||||
|
||||
private final String rootPath = "pdfTemp";
|
||||
|
||||
/**
|
||||
@ -203,18 +206,76 @@ public class PrintController implements PrintService {
|
||||
@PostMapping("settings")
|
||||
public Map<String, String> saveSystemSettings(@RequestBody Map<String, Object> settings) {
|
||||
try {
|
||||
boolean needReconnect = false;
|
||||
|
||||
// 更新最大队列大小
|
||||
if (settings.containsKey("maxQueueSize")) {
|
||||
int maxQueueSize = (Integer) settings.get("maxQueueSize");
|
||||
printQueueService.setMaxQueueSize(maxQueueSize);
|
||||
config.setMaxQueueSize(maxQueueSize);
|
||||
}
|
||||
|
||||
// 更新默认打印机
|
||||
if (settings.containsKey("defaultPrinter")) {
|
||||
String defaultPrinter = (String) settings.get("defaultPrinter");
|
||||
config.setDefaultPrinter(defaultPrinter);
|
||||
}
|
||||
|
||||
// 更新通知设置
|
||||
if (settings.containsKey("enableNotifications")) {
|
||||
boolean enableNotifications = (Boolean) settings.get("enableNotifications");
|
||||
config.setEnableNotifications(enableNotifications);
|
||||
}
|
||||
|
||||
// 更新自动启动设置
|
||||
if (settings.containsKey("autoStart")) {
|
||||
boolean autoStart = (Boolean) settings.get("autoStart");
|
||||
config.setAutoStart(autoStart);
|
||||
}
|
||||
|
||||
// 更新WebSocket URL(需要重连)
|
||||
if (settings.containsKey("websocketUrl")) {
|
||||
String websocketUrl = (String) settings.get("websocketUrl");
|
||||
if (!websocketUrl.equals(config.getWebsocketUrl())) {
|
||||
config.setWebsocketUrl(websocketUrl);
|
||||
needReconnect = true;
|
||||
}
|
||||
}
|
||||
|
||||
// 更新打印机ID(需要重连)
|
||||
if (settings.containsKey("printerId")) {
|
||||
String printerId = (String) settings.get("printerId");
|
||||
if (!printerId.equals(config.getPrinterId())) {
|
||||
config.setPrinterId(printerId);
|
||||
needReconnect = true;
|
||||
}
|
||||
}
|
||||
|
||||
// 更新API Key(需要重连)
|
||||
if (settings.containsKey("apiKey")) {
|
||||
String apiKey = (String) settings.get("apiKey");
|
||||
if (!apiKey.equals(config.getApiKey())) {
|
||||
config.setApiKey(apiKey);
|
||||
needReconnect = true;
|
||||
}
|
||||
}
|
||||
|
||||
// 保存配置到文件
|
||||
config.saveConfig();
|
||||
|
||||
// 如果WebSocket相关配置发生变化,触发重连
|
||||
if (needReconnect) {
|
||||
log.info("WebSocket配置已更改,正在重新连接...");
|
||||
printerClient.reconnect();
|
||||
} else {
|
||||
log.info("WebSocket配置未更改,无需重连");
|
||||
}
|
||||
|
||||
// 这里可以添加其他设置的保存逻辑
|
||||
log.info("系统设置已保存: {}", settings);
|
||||
|
||||
Map<String, String> result = new HashMap<>();
|
||||
result.put("status", "success");
|
||||
result.put("message", "设置保存成功");
|
||||
result.put("message", "设置保存成功" + (needReconnect ? ",WebSocket正在重新连接" : ""));
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error("保存系统设置失败", e);
|
||||
@ -225,37 +286,7 @@ public class PrintController implements PrintService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统日志
|
||||
*
|
||||
* @return 系统日志列表
|
||||
*/
|
||||
@GetMapping("logs")
|
||||
public List<Map<String, Object>> getSystemLogs() {
|
||||
// 这里返回模拟的日志数据,实际项目中可以集成日志框架
|
||||
List<Map<String, Object>> logs = new ArrayList<>();
|
||||
|
||||
Map<String, Object> log1 = new HashMap<>();
|
||||
log1.put("level", "info");
|
||||
log1.put("time", LocalDateTime.now().minusHours(1).toString());
|
||||
log1.put("message", "打印服务启动成功");
|
||||
logs.add(log1);
|
||||
|
||||
Map<String, Object> log2 = new HashMap<>();
|
||||
log2.put("level", "info");
|
||||
log2.put("time", LocalDateTime.now().minusMinutes(30).toString());
|
||||
log2.put("message", "连接到打印机: " + (config.getDefaultPrinter() != null ? config.getDefaultPrinter() : "默认打印机"));
|
||||
logs.add(log2);
|
||||
|
||||
Map<String, Object> log3 = new HashMap<>();
|
||||
log3.put("level", "info");
|
||||
log3.put("time", LocalDateTime.now().minusMinutes(10).toString());
|
||||
log3.put("message", "当前队列大小: " + printQueueService.getQueueSize());
|
||||
logs.add(log3);
|
||||
|
||||
return logs;
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("print")
|
||||
public String print(@RequestBody PrintRequest request) {
|
||||
// 记录请求信息
|
||||
@ -304,7 +335,7 @@ public class PrintController implements PrintService {
|
||||
log.info("正在从以下地址下载文件: {}", fileUrl);
|
||||
HttpUtil.downloadFile(fileUrl, filePath);
|
||||
|
||||
log.info("文件下载地址为:",filePath);
|
||||
log.info("文件下载地址为:{}",filePath);
|
||||
|
||||
if (!pdfFile.exists() || pdfFile.length() == 0) {
|
||||
throw new RuntimeException("Downloaded file is empty or does not exist");
|
||||
|
||||
@ -78,7 +78,7 @@ public class PrintServerConfig {
|
||||
printerId = properties.getProperty("printerId", DEFAULT_PRINTER_ID);
|
||||
apiKey = properties.getProperty("apiKey", DEFAULT_API_KEY);
|
||||
|
||||
log.info("配置已加载: {}", configFile.getAbsolutePath());
|
||||
log.info("配置已加载: {}, WebSocket URL: {}, PrinterId: {}", configFile.getAbsolutePath(), websocketUrl, printerId);
|
||||
} catch (IOException e) {
|
||||
log.error("加载配置文件失败", e);
|
||||
// 使用默认值
|
||||
@ -101,7 +101,10 @@ public class PrintServerConfig {
|
||||
*/
|
||||
public void saveConfig() {
|
||||
try {
|
||||
// 更新属性
|
||||
// 确保目录存在
|
||||
configFile.getParentFile().mkdirs();
|
||||
|
||||
// 设置配置值
|
||||
properties.setProperty("defaultPrinter", defaultPrinter);
|
||||
properties.setProperty("maxQueueSize", String.valueOf(maxQueueSize));
|
||||
properties.setProperty("enableNotifications", String.valueOf(enableNotifications));
|
||||
@ -113,9 +116,10 @@ public class PrintServerConfig {
|
||||
|
||||
// 保存到文件
|
||||
try (FileOutputStream fos = new FileOutputStream(configFile)) {
|
||||
properties.store(fos, "Goeing Print Server Configuration");
|
||||
log.info("配置已保存: {}", configFile.getAbsolutePath());
|
||||
properties.store(fos, "Print Server Configuration");
|
||||
}
|
||||
|
||||
log.info("配置已保存: {}, WebSocket URL: {}, PrinterId: {}", configFile.getAbsolutePath(), websocketUrl, printerId);
|
||||
} catch (IOException e) {
|
||||
log.error("保存配置文件失败", e);
|
||||
}
|
||||
|
||||
@ -151,6 +151,9 @@ public class PrinterClient implements ApplicationRunner {
|
||||
String printerId = config.getPrinterId();
|
||||
String apiKey = config.getApiKey();
|
||||
|
||||
// 添加调试日志
|
||||
log.info("当前配置 - WebSocket URL: {}, PrinterId: {}, ApiKey: {}", serverUri, printerId, apiKey);
|
||||
|
||||
String tempUrl = serverUri+"?printerId="+printerId+"&apiKey="+apiKey;
|
||||
|
||||
isConnecting = true;
|
||||
|
||||
@ -32,6 +32,13 @@ public class MemoryLogAppender extends AppenderBase<ILoggingEvent> {
|
||||
protected void append(ILoggingEvent event) {
|
||||
if (!isStarted()) return;
|
||||
|
||||
// 排除日志接口的请求日志,避免循环记录
|
||||
String message = event.getFormattedMessage();
|
||||
if (message != null) {
|
||||
if (message.contains("/api/logs")||message.contains("LogController")) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
LogEntry logEntry = new LogEntry(
|
||||
LocalDateTime.now().format(FORMATTER),
|
||||
event.getLevel().toString(),
|
||||
|
||||
54
src/main/resources/logback-spring.xml
Normal file
54
src/main/resources/logback-spring.xml
Normal file
@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<!-- 控制台输出 -->
|
||||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- 内存日志追加器 -->
|
||||
<!-- <appender name="MEMORY" class="com.goeing.printserver.main.utils.MemoryLogAppender">-->
|
||||
<!-- <!– 内存追加器不需要额外配置 –>-->
|
||||
<!-- </appender>-->
|
||||
|
||||
<!-- 文件输出 -->
|
||||
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>logs/application.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>logs/application.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
|
||||
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
|
||||
<maxFileSize>10MB</maxFileSize>
|
||||
</timeBasedFileNamingAndTriggeringPolicy>
|
||||
<maxHistory>30</maxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- 根日志级别 -->
|
||||
<root level="INFO">
|
||||
<appender-ref ref="CONSOLE" />
|
||||
<!-- <appender-ref ref="MEMORY" />-->
|
||||
<appender-ref ref="FILE" />
|
||||
</root>
|
||||
|
||||
<!-- 特定包的日志级别 -->
|
||||
<logger name="com.goeing.printserver" level="DEBUG" additivity="false">
|
||||
<appender-ref ref="CONSOLE" />
|
||||
<!-- <appender-ref ref="MEMORY" />-->
|
||||
<appender-ref ref="FILE" />
|
||||
</logger>
|
||||
|
||||
<!-- Spring Boot 相关日志 -->
|
||||
<logger name="org.springframework" level="INFO" />
|
||||
<logger name="org.springframework.web" level="DEBUG" />
|
||||
|
||||
<!-- Hibernate 相关日志 -->
|
||||
<logger name="org.hibernate" level="WARN" />
|
||||
|
||||
<!-- 网络相关日志 -->
|
||||
<logger name="org.apache.http" level="WARN" />
|
||||
<logger name="httpclient" level="WARN" />
|
||||
</configuration>
|
||||
Loading…
Reference in New Issue
Block a user