Compare commits

...

6 Commits

Author SHA1 Message Date
lifangliang
0724f442e6 save 2025-08-01 19:46:07 +08:00
lifangliang
9f2a6e56ff save 2025-08-01 13:52:40 +08:00
lifangliang
b55b4e87a6 打印机状态 2025-08-01 11:46:10 +08:00
lifangliang
8a43ac80cf 自定义日志管理 2025-07-31 18:47:37 +08:00
lifangliang
b3a3d7fad9 自定义日志管理 2025-07-31 17:27:31 +08:00
lifangliang
dd3f70c324 自定义日志管理 2025-07-31 14:24:36 +08:00
11 changed files with 434 additions and 77 deletions

View File

@ -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);
}
}

View File

@ -35,7 +35,10 @@ public class PrintController implements PrintService {
@Autowired
private PrintServerConfig config;
private final String rootPath = "pdfTemp";
@Autowired
private com.goeing.printserver.main.sse.PrinterClient printerClient;
private final String rootPath = System.getProperty("java.io.tmpdir") + File.separator + "goeingprint" + File.separator + "pdfTemp";
/**
* 获取所有可用打印机列表
@ -194,6 +197,65 @@ public class PrintController implements PrintService {
return settings;
}
/**
* 获取打印机连接状态
*
* @return 包含各种连接状态的详细信息
*/
@GetMapping("printers/status")
public Map<String, Object> getPrintersStatus() {
Map<String, Object> status = new HashMap<>();
// Java后端服务状态
Map<String, Object> backendStatus = new HashMap<>();
backendStatus.put("status", "connected");
backendStatus.put("uptime", getUptime());
backendStatus.put("timestamp", System.currentTimeMillis());
status.put("backend", backendStatus);
// WebSocket连接状态
Map<String, Object> websocketStatus = new HashMap<>();
boolean isWebSocketConnected = printerClient.isConnected();
websocketStatus.put("status", isWebSocketConnected ? "connected" : "disconnected");
websocketStatus.put("url", config.getWebsocketUrl());
websocketStatus.put("printerId", config.getPrinterId());
if (isWebSocketConnected) {
websocketStatus.put("connectionUrl", printerClient.getCurrentConnectionUrl());
}
websocketStatus.put("timestamp", System.currentTimeMillis());
status.put("websocket", websocketStatus);
// 本地打印机状态
Map<String, Object> printersStatus = new HashMap<>();
javax.print.PrintService[] printServices = PrinterJob.lookupPrintServices();
List<Map<String, Object>> printerList = Arrays.stream(printServices)
.map(service -> {
Map<String, Object> printer = new HashMap<>();
printer.put("name", service.getName());
printer.put("status", "available"); // 简化处理假设所有检测到的打印机都可用
printer.put("isDefault", service.getName().equals(config.getDefaultPrinter()));
return printer;
})
.sorted((a, b) -> ((String) a.get("name")).compareTo((String) b.get("name")))
.collect(Collectors.toList());
printersStatus.put("count", printerList.size());
printersStatus.put("printers", printerList);
printersStatus.put("defaultPrinter", config.getDefaultPrinter());
printersStatus.put("timestamp", System.currentTimeMillis());
status.put("localPrinters", printersStatus);
// 打印队列状态
Map<String, Object> queueStatus = new HashMap<>();
queueStatus.put("queueSize", printQueueService.getQueueSize());
queueStatus.put("maxQueueSize", printQueueService.getMaxQueueSize());
queueStatus.put("currentTask", printQueueService.getCurrentTaskInfo());
queueStatus.put("timestamp", System.currentTimeMillis());
status.put("queue", queueStatus);
return status;
}
/**
* 保存系统设置
*
@ -203,18 +265,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 +345,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 +394,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");

View File

@ -0,0 +1,32 @@
// LogbackConfig.java
package com.goeing.printserver.main.config;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.Logger;
import com.goeing.printserver.main.utils.MemoryLogAppender;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import jakarta.annotation.PostConstruct;
@Configuration
public class LogbackConfig {
@Autowired
private MemoryLogAppender memoryLogAppender;
@PostConstruct
public void registerAppender() {
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
// // 添加 appender root logger
Logger rootLogger = context.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
rootLogger.addAppender(memoryLogAppender);
memoryLogAppender.start();
// 可选也加到你的包 logger
// Logger appLogger = context.getLogger("com.goeing.printserver");
// appLogger.addAppender(memoryLogAppender);
}
}

View File

@ -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);
}

View File

@ -0,0 +1,74 @@
package com.goeing.printserver.main.controller;
import com.goeing.printserver.main.utils.MemoryLogAppender;
import com.goeing.printserver.main.utils.MemoryLogStorage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 日志控制器提供日志相关的 API 接口
*/
@RestController
@RequestMapping("/api/logs")
@Slf4j
public class LogController {
@Autowired
private MemoryLogStorage memoryLogStorage;
/**
* 获取系统日志
* @param limit 限制数量默认100
* @param level 日志级别过滤默认ALL
* @return 日志列表
*/
@GetMapping
public Map<String, Object> getLogs(
@RequestParam(defaultValue = "100") int limit,
@RequestParam(defaultValue = "ALL") String level) {
Map<String, Object> result = new HashMap<>();
try {
List<MemoryLogAppender.LogEntry> logs = memoryLogStorage.getLogs(limit, level);
result.put("success", true);
result.put("logs", logs);
result.put("total", logs.size());
} catch (Exception e) {
log.error("获取日志失败", e);
result.put("success", false);
result.put("message", "获取日志失败: " + e.getMessage());
result.put("logs", List.of());
result.put("total", 0);
}
return result;
}
/**
* 清空系统日志
* @return 操作结果
*/
@DeleteMapping
public Map<String, Object> clearLogs() {
Map<String, Object> result = new HashMap<>();
try {
memoryLogStorage.clearLogs();
result.put("success", true);
result.put("message", "日志已清空");
log.info("系统日志已被清空");
} catch (Exception e) {
log.error("清空日志失败", e);
result.put("success", false);
result.put("message", "清空日志失败: " + e.getMessage());
}
return result;
}
}

View File

@ -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;

View File

@ -0,0 +1,68 @@
package com.goeing.printserver.main.utils;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import cn.hutool.extra.spring.SpringUtil;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* 内存日志追加器用于缓存日志到内存中
*/
@Component
public class MemoryLogAppender extends AppenderBase<ILoggingEvent> {
@Autowired
private MemoryLogStorage memoryLogStorage; // Spring 注入全局唯一
private static final DateTimeFormatter FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
@Override
public void start() {
System.out.println("✅ MemoryLogAppender started with context: " + context);
super.start(); // 必须调用
}
@Override
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(),
event.getLoggerName(),
event.getFormattedMessage()
);
memoryLogStorage.addLog(logEntry); // 写入共享存储
}
/**
* 日志条目类
*/
@Getter
public static class LogEntry {
private final String timestamp;
private final String level;
private final String logger;
private final String message;
public LogEntry(String timestamp, String level, String logger, String message) {
this.timestamp = timestamp;
this.level = level;
this.logger = logger;
this.message = message;
}
}
}

View File

@ -0,0 +1,57 @@
package com.goeing.printserver.main.utils;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
@Component
public class MemoryLogStorage {
private final Queue<MemoryLogAppender.LogEntry> logs = new ConcurrentLinkedQueue<>();
private static final int MAX_LOGS = 2000;
/**
* 添加日志条目并控制最大数量
*/
public void addLog(MemoryLogAppender.LogEntry logEntry) {
logs.offer(logEntry);
// 保持最多 MAX_LOGS
while (logs.size() > MAX_LOGS) {
logs.poll();
}
}
/**
* 获取日志列表支持过滤和分页
*/
public List<MemoryLogAppender.LogEntry> getLogs(int limit, String level) {
List<MemoryLogAppender.LogEntry> result = new ArrayList<>();
for (MemoryLogAppender.LogEntry log : logs) {
if (level == null || "ALL".equals(level) || level.equals(log.getLevel())) {
result.add(log);
}
}
// 最新的在前
result.sort((a, b) -> b.getTimestamp().compareTo(a.getTimestamp()));
// 限制数量
if (limit > 0 && result.size() > limit) {
return result.subList(0, limit);
}
return result;
}
/**
* 清空所有日志
*/
public void clearLogs() {
logs.clear();
}
/**
* 获取当前日志数量用于监控
*/
public int size() {
return logs.size();
}
}

View File

@ -254,8 +254,8 @@ public class PdfPrinter {
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
// 设置纸张大小
String size = option.getSize() != null ? option.getSize() : "Letter";
aset.add(PAPER_SIZES.getOrDefault(size, MediaSizeName.NA_LETTER));
// String size = option.getSize() != null ? option.getSize() : "Letter";
// aset.add(PAPER_SIZES.getOrDefault(size, MediaSizeName.NA_LETTER));
// 设置颜色模式
setColorMode(aset, option.getColor());
@ -280,7 +280,7 @@ public class PdfPrinter {
* @return PDF页面方向
*/
private static Orientation getPdfOrientation(String orientationStr) {
if (orientationStr != null && "ORI_LANDSCAPE".equalsIgnoreCase(orientationStr)) {
if ("ORI_LANDSCAPE".equalsIgnoreCase(orientationStr)) {
return Orientation.LANDSCAPE;
}
return Orientation.PORTRAIT; // 默认为纵向
@ -293,7 +293,14 @@ public class PdfPrinter {
* @param color 颜色模式字符串
*/
private static void setColorMode(PrintRequestAttributeSet aset, String color) {
if (color != null && ("Full Color".equalsIgnoreCase(color) || "Cover Letter Color Only".equalsIgnoreCase(color))) {
if (color == null) {
aset.add(Chromaticity.MONOCHROME);
return;
}
String c = color.trim().toLowerCase();
if (c.equals("color") || c.equals("full color") || c.equals("cover letter color only")) {
aset.add(Chromaticity.COLOR);
} else {
aset.add(Chromaticity.MONOCHROME);

View File

@ -10,7 +10,6 @@ spring.main.allow-circular-references=true
# 在macOS系统上如果遇到HeadlessException可以设置为true强制使用无头模式
# 或者在启动时添加JVM参数-Djava.awt.headless=true
app.force.headless=false
# 日志配置
# 设置打印机状态面板和设置面板的日志级别为WARN减少日志输出

View 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">-->
<!-- &lt;!&ndash; 内存追加器不需要额外配置 &ndash;&gt;-->
<!-- </appender>-->
<!-- 文件输出 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${java.io.tmpdir}/goeingprint/logs/application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${java.io.tmpdir}/goeingprint/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>