fix - 日志上传

This commit is contained in:
2026-06-03 22:48:08 +08:00
parent c60545fbd6
commit ebfcc2a1b4
5 changed files with 74 additions and 40 deletions

View File

@@ -1,51 +1,39 @@
package com.sv.intergration;
import com.jcraft.jsch.*;
import com.sv.netty.config.NettyConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
public class LogService {
private final Logger logger = LoggerFactory.getLogger(LogService.class);
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public void uploadLog() {
public Map<String, String> readLogContent() {
Map<String, String> logContent = new HashMap<>();
try {
JSch jsch = new JSch();
String serverIp = System.getProperty(NettyConstant.VENUE_SERVER_IP);
Session session = jsch.getSession("root", serverIp, 22);
session.setPassword(System.getProperty(NettyConstant.VENUE_CHECK_PWD));
session.setConfig("StrictHostKeyChecking", "no");
session.connect(3000);
ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
String deviceSn = System.getProperty(NettyConstant.VENUE_CLIENT_SN);
logContent.put("deviceSn", deviceSn);
File file1 = new File("/home/venue/logs/common-default.log");
File file2 = new File("/home/venue/logs/common-error.log");
if (file1.exists() || file2.exists()) {
sftp.connect();
if (file1.exists()){
sftp.put(new FileInputStream(file1), "/home/uploadlog/comm-" + System.getProperty(NettyConstant.VENUE_CLIENT_SN) + "-" + sdf.format(new Date()) + "-client.log");
}
if (file2.exists()) {
sftp.put(new FileInputStream(file2), "/home/uploadlog/error-" + System.getProperty(NettyConstant.VENUE_CLIENT_SN) + "-" + sdf.format(new Date()) + "-client.log");
}
logger.info("upload logs success");
if (file1.exists()) {
logContent.put("commLog", new String(Files.readAllBytes(Paths.get(file1.getPath()))));
}
if (sftp != null) {
sftp.disconnect();
if (file2.exists()) {
logContent.put("errorLog", new String(Files.readAllBytes(Paths.get(file2.getPath()))));
}
if (session != null) {
session.disconnect();
}
}catch (Exception e){
logger.error("upload log fail",e);
logger.info("read log content success");
} catch (Exception e) {
logger.error("read log content fail", e);
}
return logContent;
}
}