refactor: 日志优化,根据IP解析的省市字段拆分

This commit is contained in:
Ray.Hao
2024-06-28 12:58:01 +08:00
parent 6b5d9cb4a0
commit 94d4998e43
4 changed files with 74 additions and 18 deletions

View File

@@ -1,13 +1,19 @@
package com.youlai.system.common.util;
import cn.hutool.core.util.StrUtil;
import jakarta.annotation.PostConstruct;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
/**
* IP工具类
@@ -22,9 +28,31 @@ import java.net.UnknownHostException;
* @since 2.10.0
*/
@Slf4j
@Component
public class IPUtils {
private static final String DB_PATH = "src/main/resources/data/ip2region.xdb";
private static final String DB_PATH = "/data/ip2region.xdb";
private static Searcher searcher;
@PostConstruct
public void init() {
try {
// 从类路径加载资源文件
InputStream inputStream = getClass().getResourceAsStream(DB_PATH);
if (inputStream == null) {
throw new FileNotFoundException("Resource not found: " + DB_PATH);
}
// 将资源文件复制到临时文件
Path tempDbPath = Files.createTempFile("ip2region", ".xdb");
Files.copy(inputStream, tempDbPath, StandardCopyOption.REPLACE_EXISTING);
// 使用临时文件初始化 Searcher 对象
searcher = Searcher.newWithFileOnly(tempDbPath.toString());
} catch (Exception e) {
log.error("IpRegionUtil initialization ERROR, {}", e.getMessage());
}
}
/**
* 获取IP地址
@@ -96,21 +124,16 @@ public class IPUtils {
* @return 地理位置信息
*/
public static String getRegion(String ip) {
Searcher searcher = null;
if (searcher == null) {
log.error("Searcher is not initialized");
return null;
}
try {
searcher = Searcher.newWithFileOnly(DB_PATH);
return searcher.search(ip);
} catch (Exception e) {
log.error("IpRegionUtil ERROR, {}", e.getMessage());
return null;
} finally {
if (searcher != null) {
try {
searcher.close();
} catch (IOException e) {
log.error("IpRegionUtil close ERROR, {}", e.getMessage());
}
}
}
}
}

View File

@@ -53,11 +53,26 @@ public class SysLog implements Serializable {
*/
private String region;
/**
* 省份
*/
private String province;
/**
* 城市
*/
private String city;
/**
* 浏览器
*/
private String browser;
/**
* 浏览器版本
*/
private String browserVersion;
/**
* 终端系统
*/

View File

@@ -52,12 +52,12 @@ public class LogAspect {
}
TimeInterval timer = DateUtil.timer();
// 执行方法
Object proceed = joinPoint.proceed();
long executionTime = timer.interval();
// 创建日志对象
// 创建日志记录
SysLog log = new SysLog();
log.setModule(logAnnotation.module());
log.setContent(logAnnotation.value());
log.setRequestUri(requestURI);
@@ -70,7 +70,14 @@ public class LogAspect {
if (StrUtil.isNotBlank(ipAddr)) {
log.setIp(ipAddr);
String region = IPUtils.getRegion(ipAddr);
log.setRegion(region);
// 中国|0|四川省|成都市|电信 解析省和市
if (StrUtil.isNotBlank(region)) {
String[] regionArray = region.split("\\|");
if (regionArray.length > 2) {
log.setProvince(regionArray[2]);
log.setRegion(regionArray[3]);
}
}
}
log.setExecutionTime(executionTime);
// 方法名
@@ -81,8 +88,8 @@ public class LogAspect {
// 系统信息
log.setOs(userAgent.getOs().getName());
// 浏览器信息
String browserInfo = userAgent.getBrowser().getName() + " " + userAgent.getBrowser().getVersion(userAgentString);
log.setBrowser(browserInfo);
log.setBrowser(userAgent.getBrowser().getName());
log.setBrowserVersion(userAgent.getBrowser().getVersion(userAgentString));
// 保存日志到数据库
logService.save(log);

View File

@@ -7,7 +7,18 @@
<!-- 日志分页列表 -->
<select id="listPagedLogs" resultType="com.youlai.system.model.vo.LogPageVO">
SELECT
t1.*,
id,
module,
content,
request_uri,
method,
ip,
concat(province, city) AS region,
execution_time,
browser,
browser_version,
os,
create_time,
t2.nickname AS operator
FROM
sys_log t1