优化log机制

This commit is contained in:
weidong 2024-11-07 21:28:39 +08:00
parent 7c9a5b0d26
commit f9f9575e59
12 changed files with 56 additions and 34 deletions

View File

@ -114,15 +114,7 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version><!-- 使用最新版本 -->
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>

View File

@ -1,5 +1,6 @@
package com.imdroid.sideslope.service;
import com.imdroid.common.util.ThreadManager;
import com.imdroid.secapi.dto.GnssSingleData;
import com.imdroid.secapi.dto.GnssSingleDataMapper;
import org.slf4j.Logger;
@ -32,7 +33,6 @@ public class GnssSingleBufferServiceImpl implements GnssSingleBufferService {
buffer.add(data);
if (buffer.size() >= BUFFER_SIZE) {
// 溢出时直接保存
// 此处暂定但很有必要引入一个线程来保存避免阻塞主线程
flush();
}
}
@ -48,12 +48,19 @@ public class GnssSingleBufferServiceImpl implements GnssSingleBufferService {
try {
List<GnssSingleData> batchList = new ArrayList<>(buffer);
buffer.clear();
// 由于每秒写数据库操作频繁这里采用批量保存的方式
saveBatch(batchList);
// 数据库操作异步执行不阻塞主线程
ThreadManager.getSingleThreadPool("gnss-single-save").submit(() -> {
try {
saveBatch(batchList);
logger.debug("批量插入{}条数据成功", batchList.size());
} catch (Exception e) {
logger.error("批量插入数据失败", e);
}
});
} catch (Exception e) {
logger.error("线程创建失败", e);
throw e;
}
}

View File

@ -34,3 +34,5 @@ mybatis-plus.configuration.map-underscore-to-camel-case=false
xfz.server.host = 171.106.48.63
xfz.server.port = 52000
xfz.server.data.send = false
warning.log.directory=./log

View File

@ -20,7 +20,7 @@
</encoder>
</appender>
<root level="debug">
<root level="info">
<appender-ref ref="STDOUT" />
</root>
<logger name="org.springframework" level="warn" />

View File

@ -155,7 +155,7 @@
</exclude>
</excludes>
</configuration>
</plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>11</source><target>11</target></configuration></plugin>
</plugin>
</plugins>
</build>

View File

@ -12,6 +12,8 @@ import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.Duration;
import java.time.LocalDateTime;
@ -80,6 +82,11 @@ public class GnssSingleDataController extends BasicController implements CommonE
return this.pageList(session, page, limit, searchParams);
}
@RequestMapping("/gnss/data/single/export")
@ResponseBody
public void exportData(HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
this.export(session, request, response);
}
@Override
public Class<GnssSingleData> getEntityClass() {

View File

@ -46,3 +46,5 @@ aliyun.oss.accessKey = LTAI4G6hmpX7h9hQvUnwKWxj
aliyun.oss.accessSecret = GHVzHKLLor8i5ZR1qyeoi4KMf3mjHb
aliyun.oss.bucket = imdroid-device-management
aliyun.oss.publicReadUrl = https://imdroid-device-management.oss-cn-shanghai.aliyuncs.com
warning.log.directory=./log

View File

@ -67,6 +67,7 @@ CREATE TABLE IF NOT EXISTS `gnssdevices` (
`appver` varchar(16) DEFAULT NULL COMMENT '固件版本',
`imei` varchar(16) DEFAULT NULL,
`model` smallint DEFAULT 0,
`loggingmode` smallint DEFAULT 0 COMMENT '日志模式: 0-精简模式(仅D3F0和D3F2), 1-完整模式',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
@ -320,3 +321,17 @@ CREATE TABLE IF NOT EXISTS `ApiKey` (
`tenantname` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `gnssdevicesinglerecords` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`deviceid` varchar(64) NOT NULL COMMENT '设备ID',
`createtime` datetime(3) NOT NULL COMMENT '创建时间',
`model` smallint NOT NULL COMMENT '设备型号: 0-F9P(G505), 1-博通(G510)',
`x` double NOT NULL COMMENT 'GGA时是latitude, ECEF时是x',
`y` double NOT NULL COMMENT 'GGA时是longitude, ECEF时是y',
`z` double NOT NULL COMMENT 'GGA时是altitude, ECEF时是z',
`status` int NOT NULL DEFAULT '0' COMMENT 'GGA: 0初始化,1单点定位,2码差分,3无效PPS,4固定解,5浮点解,6估算,7人工固定,8模拟,9WAAS差分; ECEF: 0无B562,1浮点解,2固定解',
PRIMARY KEY (`id`),
KEY `idx_deviceid_createtime` (`deviceid`,`createtime`),
KEY `idx_createtime` (`createtime`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='GNSS单次解算记录表';

View File

@ -149,7 +149,7 @@
// 监听导出操作
form.on('submit(data-export-btn)', function (data) {
var result = $('#searchFrm').serialize();
var u = "/gnss/data/calc/export?" + result;
var u = "/gnss/data/single/export?" + result;
window.open(u, "_blank");
return false;
});

View File

@ -8,6 +8,7 @@ import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class WarningLogExecutor {
@ -32,14 +33,15 @@ public class WarningLogExecutor {
initializeWarningFile(warningFile, keyword, warningTypeName);
}
// 找到日志文件
List<Path> logFiles = Files.list(Paths.get(logDirectory))
try (Stream<Path> logFilesStream = Files.list(Paths.get(logDirectory))) {
List<Path> logFiles = logFilesStream
.filter(Files::isRegularFile)
.filter(path -> path.getFileName().toString().matches("sideslopertcm(\\.\\d{4}-\\d{2}-\\d{2}\\.\\d+)?\\.log"))
.collect(Collectors.toList());
for (Path logFile : logFiles) {
executor.submit(() -> processFile(logFile, keyword, warningTypeName, warningFile));
executor.submit(() -> processFile(logFile, keyword, warningFile));
}
}
} catch (IOException e) {
e.printStackTrace();
@ -67,7 +69,7 @@ public class WarningLogExecutor {
}
}
private void processFile(Path logFile, String keyword, String warningTypeName, Path warningFile) {
private void processFile(Path logFile, String keyword, Path warningFile) {
try (BufferedReader reader = Files.newBufferedReader(logFile);
BufferedWriter writer = Files.newBufferedWriter(warningFile,
StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {

View File

@ -100,14 +100,7 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>

View File

@ -20,3 +20,5 @@ app.format.time = HH:mm:ss
app.format.datetime = yyyy-MM-dd HH:mm:ss
mybatis-plus.configuration.map-underscore-to-camel-case=false
netty.test.port = 9917