refactor: 日志优化,根据IP解析的省市字段拆分
This commit is contained in:
@@ -1,13 +1,19 @@
|
|||||||
package com.youlai.system.common.util;
|
package com.youlai.system.common.util;
|
||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.lionsoul.ip2region.xdb.Searcher;
|
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.InetAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* IP工具类
|
* IP工具类
|
||||||
@@ -22,9 +28,31 @@ import java.net.UnknownHostException;
|
|||||||
* @since 2.10.0
|
* @since 2.10.0
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@Component
|
||||||
public class IPUtils {
|
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地址
|
* 获取IP地址
|
||||||
@@ -96,21 +124,16 @@ public class IPUtils {
|
|||||||
* @return 地理位置信息
|
* @return 地理位置信息
|
||||||
*/
|
*/
|
||||||
public static String getRegion(String ip) {
|
public static String getRegion(String ip) {
|
||||||
Searcher searcher = null;
|
if (searcher == null) {
|
||||||
|
log.error("Searcher is not initialized");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
searcher = Searcher.newWithFileOnly(DB_PATH);
|
|
||||||
return searcher.search(ip);
|
return searcher.search(ip);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("IpRegionUtil ERROR, {}", e.getMessage());
|
log.error("IpRegionUtil ERROR, {}", e.getMessage());
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
|
||||||
if (searcher != null) {
|
|
||||||
try {
|
|
||||||
searcher.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("IpRegionUtil close ERROR, {}", e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,11 +53,26 @@ public class SysLog implements Serializable {
|
|||||||
*/
|
*/
|
||||||
private String region;
|
private String region;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 省份
|
||||||
|
*/
|
||||||
|
private String province;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 城市
|
||||||
|
*/
|
||||||
|
private String city;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 浏览器
|
* 浏览器
|
||||||
*/
|
*/
|
||||||
private String browser;
|
private String browser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 浏览器版本
|
||||||
|
*/
|
||||||
|
private String browserVersion;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 终端系统
|
* 终端系统
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -52,12 +52,12 @@ public class LogAspect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TimeInterval timer = DateUtil.timer();
|
TimeInterval timer = DateUtil.timer();
|
||||||
|
// 执行方法
|
||||||
Object proceed = joinPoint.proceed();
|
Object proceed = joinPoint.proceed();
|
||||||
long executionTime = timer.interval();
|
long executionTime = timer.interval();
|
||||||
|
|
||||||
// 创建日志对象
|
// 创建日志记录
|
||||||
SysLog log = new SysLog();
|
SysLog log = new SysLog();
|
||||||
|
|
||||||
log.setModule(logAnnotation.module());
|
log.setModule(logAnnotation.module());
|
||||||
log.setContent(logAnnotation.value());
|
log.setContent(logAnnotation.value());
|
||||||
log.setRequestUri(requestURI);
|
log.setRequestUri(requestURI);
|
||||||
@@ -70,7 +70,14 @@ public class LogAspect {
|
|||||||
if (StrUtil.isNotBlank(ipAddr)) {
|
if (StrUtil.isNotBlank(ipAddr)) {
|
||||||
log.setIp(ipAddr);
|
log.setIp(ipAddr);
|
||||||
String region = IPUtils.getRegion(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);
|
log.setExecutionTime(executionTime);
|
||||||
// 方法名
|
// 方法名
|
||||||
@@ -81,8 +88,8 @@ public class LogAspect {
|
|||||||
// 系统信息
|
// 系统信息
|
||||||
log.setOs(userAgent.getOs().getName());
|
log.setOs(userAgent.getOs().getName());
|
||||||
// 浏览器信息
|
// 浏览器信息
|
||||||
String browserInfo = userAgent.getBrowser().getName() + " " + userAgent.getBrowser().getVersion(userAgentString);
|
log.setBrowser(userAgent.getBrowser().getName());
|
||||||
log.setBrowser(browserInfo);
|
log.setBrowserVersion(userAgent.getBrowser().getVersion(userAgentString));
|
||||||
// 保存日志到数据库
|
// 保存日志到数据库
|
||||||
logService.save(log);
|
logService.save(log);
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,18 @@
|
|||||||
<!-- 日志分页列表 -->
|
<!-- 日志分页列表 -->
|
||||||
<select id="listPagedLogs" resultType="com.youlai.system.model.vo.LogPageVO">
|
<select id="listPagedLogs" resultType="com.youlai.system.model.vo.LogPageVO">
|
||||||
SELECT
|
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
|
t2.nickname AS operator
|
||||||
FROM
|
FROM
|
||||||
sys_log t1
|
sys_log t1
|
||||||
|
|||||||
Reference in New Issue
Block a user