fix - 日志上传
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
package com.sv.intergration;
|
||||
|
||||
import com.sv.netty.config.NettyConstant;
|
||||
import com.sv.netty.utils.JsonUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
@@ -14,26 +18,51 @@ public class LogService {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(LogService.class);
|
||||
|
||||
public Map<String, String> readLogContent() {
|
||||
Map<String, String> logContent = new HashMap<>();
|
||||
public void uploadLogViaHttp() {
|
||||
try {
|
||||
String deviceSn = System.getProperty(NettyConstant.VENUE_CLIENT_SN);
|
||||
logContent.put("deviceSn", deviceSn);
|
||||
Map<String, String> logData = new HashMap<>();
|
||||
logData.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()) {
|
||||
logContent.put("commLog", new String(Files.readAllBytes(Paths.get(file1.getPath()))));
|
||||
logData.put("commLog", new String(Files.readAllBytes(Paths.get(file1.getPath()))));
|
||||
}
|
||||
if (file2.exists()) {
|
||||
logContent.put("errorLog", new String(Files.readAllBytes(Paths.get(file2.getPath()))));
|
||||
logData.put("errorLog", new String(Files.readAllBytes(Paths.get(file2.getPath()))));
|
||||
}
|
||||
logger.info("read log content success");
|
||||
|
||||
if (!file1.exists() && !file2.exists()) {
|
||||
logger.warn("no log files found");
|
||||
return;
|
||||
}
|
||||
|
||||
String json = JsonUtils.encode(logData);
|
||||
URL url = new URL("https://api.hongyutiyu.top/api/log/receive");
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
|
||||
conn.setDoOutput(true);
|
||||
conn.setConnectTimeout(10000);
|
||||
conn.setReadTimeout(30000);
|
||||
|
||||
try (OutputStream os = conn.getOutputStream()) {
|
||||
os.write(json.getBytes("utf-8"));
|
||||
os.flush();
|
||||
}
|
||||
|
||||
int code = conn.getResponseCode();
|
||||
if (code == 200) {
|
||||
logger.info("upload log success via http");
|
||||
} else {
|
||||
logger.error("upload log fail, http code: " + code);
|
||||
}
|
||||
conn.disconnect();
|
||||
} catch (Exception e) {
|
||||
logger.error("read log content fail", e);
|
||||
logger.error("upload log via http fail", e);
|
||||
}
|
||||
return logContent;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,16 +1,10 @@
|
||||
package com.sv.intergration;
|
||||
|
||||
import com.sv.intergration.impl.OldDoorService;
|
||||
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.util.Map;
|
||||
|
||||
/**
|
||||
* 消息服务
|
||||
* MessageService.java
|
||||
@@ -37,10 +31,9 @@ public class MessageService {
|
||||
/**
|
||||
* 解析并执行接受服务器消息
|
||||
*
|
||||
* @param ctx
|
||||
* @param message
|
||||
*/
|
||||
public void execute(ChannelHandlerContext ctx, VenueMessage message) {
|
||||
public void execute(VenueMessage message) {
|
||||
switch (message.getMessageType()) {
|
||||
case ENTER_DOOR:
|
||||
enterDoor();
|
||||
@@ -49,23 +42,16 @@ public class MessageService {
|
||||
outDoor();
|
||||
break;
|
||||
case LOG:
|
||||
sendLog(ctx);
|
||||
sendLog();
|
||||
break;
|
||||
default:
|
||||
logger.info( "default");
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
private void sendLog() {
|
||||
LogService logService = new LogService();
|
||||
logService.uploadLogViaHttp();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -58,7 +58,7 @@ public class ClientHandler extends SimpleChannelInboundHandler<String> {
|
||||
logger.error("收到无法解析的消息,忽略: " + msg);
|
||||
return;
|
||||
}
|
||||
MessageService.getInstance().execute(ctx, message);
|
||||
MessageService.getInstance().execute(message);
|
||||
} catch (Exception e) {
|
||||
logger.error("处理服务器消息异常,不影响连接: " + msg, e);
|
||||
}
|
||||
|
||||
@@ -30,7 +30,6 @@ public class ClientService {
|
||||
System.setProperty(NettyConstant.VENUE_SERVER_PORT,decode.getServerPort());
|
||||
System.setProperty(NettyConstant.VENUE_CLIENT_SN,decode.getClientSN());
|
||||
System.setProperty(NettyConstant.VENUE_CLIENT_VID,decode.getClientVid());
|
||||
System.setProperty(NettyConstant.VENUE_CHECK_PWD,decode.getClientPwd());
|
||||
}
|
||||
bufferedReader.close();
|
||||
fileReader.close();
|
||||
|
||||
Reference in New Issue
Block a user