fix - 日志上传
This commit is contained in:
@@ -12,7 +12,11 @@ import io.netty.handler.timeout.IdleStateEvent;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -81,6 +85,9 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
|
||||
}
|
||||
}
|
||||
break;
|
||||
case LOG_DATA:
|
||||
handleLogData(message);
|
||||
break;
|
||||
default:
|
||||
logger.info( "default");
|
||||
}
|
||||
@@ -140,6 +147,34 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
|
||||
}
|
||||
}
|
||||
|
||||
private void handleLogData(VenueMessage message) {
|
||||
try {
|
||||
String json = message.getMessage();
|
||||
Map<String, String> logData = JsonUtils.decode(json, Map.class);
|
||||
String deviceSn = logData.get("deviceSn");
|
||||
String commLog = logData.get("commLog");
|
||||
String errorLog = logData.get("errorLog");
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
String dateStr = sdf.format(new Date());
|
||||
|
||||
if (commLog != null) {
|
||||
String fileName = "/home/uploadlog/comm-" + deviceSn + "-" + dateStr + "-client.log";
|
||||
try (FileOutputStream fos = new FileOutputStream(fileName)) {
|
||||
fos.write(commLog.getBytes());
|
||||
}
|
||||
}
|
||||
if (errorLog != null) {
|
||||
String fileName = "/home/uploadlog/error-" + deviceSn + "-" + dateStr + "-client.log";
|
||||
try (FileOutputStream fos = new FileOutputStream(fileName)) {
|
||||
fos.write(errorLog.getBytes());
|
||||
}
|
||||
}
|
||||
logger.info("log data saved for device: " + deviceSn);
|
||||
} catch (Exception e) {
|
||||
logger.error("handle log data fail", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* IdleStateHandler 如果几秒之后没有读操作,那么就会触发这个方法
|
||||
*/
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
package com.sv.intergration;
|
||||
|
||||
import com.sv.intergration.impl.OldDoorService;
|
||||
import com.sv.netty.config.NettyConstant;
|
||||
import com.sv.netty.config.MessageType;
|
||||
import com.sv.netty.config.VenueMessage;
|
||||
import com.sv.netty.utils.EncodeMsg;
|
||||
import com.sv.netty.utils.JsonUtils;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 消息服务
|
||||
@@ -35,9 +37,10 @@ public class MessageService {
|
||||
/**
|
||||
* 解析并执行接受服务器消息
|
||||
*
|
||||
* @param ctx
|
||||
* @param message
|
||||
*/
|
||||
public void execute(VenueMessage message) {
|
||||
public void execute(ChannelHandlerContext ctx, VenueMessage message) {
|
||||
switch (message.getMessageType()) {
|
||||
case ENTER_DOOR:
|
||||
enterDoor();
|
||||
@@ -46,16 +49,23 @@ public class MessageService {
|
||||
outDoor();
|
||||
break;
|
||||
case LOG:
|
||||
sendLog();
|
||||
sendLog(ctx);
|
||||
break;
|
||||
default:
|
||||
logger.info( "default");
|
||||
}
|
||||
}
|
||||
|
||||
private void sendLog() {
|
||||
LogService logService = new LogService();
|
||||
logService.uploadLog();
|
||||
private void sendLog(ChannelHandlerContext ctx) {
|
||||
try {
|
||||
LogService logService = new LogService();
|
||||
Map<String, String> logContent = logService.readLogContent();
|
||||
VenueMessage response = new VenueMessage(MessageType.LOG_DATA, JsonUtils.encode(logContent));
|
||||
ctx.channel().writeAndFlush(EncodeMsg.INSTANCE.encode(response));
|
||||
logger.info("send log data success");
|
||||
} catch (Exception e) {
|
||||
logger.error("send log data fail", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -58,7 +58,7 @@ public class ClientHandler extends SimpleChannelInboundHandler<String> {
|
||||
logger.error("收到无法解析的消息,忽略: " + msg);
|
||||
return;
|
||||
}
|
||||
MessageService.getInstance().execute(message);
|
||||
MessageService.getInstance().execute(ctx, message);
|
||||
} catch (Exception e) {
|
||||
logger.error("处理服务器消息异常,不影响连接: " + msg, e);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,8 @@ public enum MessageType {
|
||||
SCAN_CODE("扫码"),
|
||||
ENTER_DOOR("进门"),
|
||||
OUT_DOOR("出门"),
|
||||
LOG("日志");
|
||||
LOG("日志"),
|
||||
LOG_DATA("日志数据");
|
||||
|
||||
private String message;
|
||||
MessageType(String message) {
|
||||
|
||||
Reference in New Issue
Block a user