Commit 5240b5de by 赵剑炜

添加ESlog,尚未完成

parent 831b1535
...@@ -138,5 +138,11 @@ ...@@ -138,5 +138,11 @@
<version>3.5.2</version> <version>3.5.2</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>com.junmp.v2</groupId>
<artifactId>v2-log-db</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>
package com.junmp.jyzb.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.junmp.v2.db.api.entity.BaseEntity;
import org.springframework.data.elasticsearch.annotations.Document;
@TableName("sys_log")
@Document(indexName = "sys_log", shards = 1, replicas = 1)
public class EsLog extends BaseEntity {
@TableId(
value = "log_id",
type = IdType.ASSIGN_ID
)
private Long logId;
@TableField("log_name")
private String logName;
@TableField("log_content")
private String logContent;
@TableField("app_name")
private String appName;
@TableField("log_type")
private Integer logType;
@TableField("request_url")
private String requestUrl;
@TableField("request_params")
private String requestParams;
@TableField("request_result")
private String requestResult;
@TableField("server_ip")
private String serverIp;
@TableField("client_ip")
private String clientIp;
@TableField("user_id")
private Long userId;
@TableField("http_method")
private String httpMethod;
@TableField("client_browser")
private String clientBrowser;
@TableField("client_os")
private String clientOs;
public EsLog() {
}
public Long getLogId() {
return this.logId;
}
public String getLogName() {
return this.logName;
}
public String getLogContent() {
return this.logContent;
}
public String getAppName() {
return this.appName;
}
public Integer getLogType() {
return this.logType;
}
public String getRequestUrl() {
return this.requestUrl;
}
public String getRequestParams() {
return this.requestParams;
}
public String getRequestResult() {
return this.requestResult;
}
public String getServerIp() {
return this.serverIp;
}
public String getClientIp() {
return this.clientIp;
}
public Long getUserId() {
return this.userId;
}
public String getHttpMethod() {
return this.httpMethod;
}
public String getClientBrowser() {
return this.clientBrowser;
}
public String getClientOs() {
return this.clientOs;
}
public void setLogId(final Long logId) {
this.logId = logId;
}
public void setLogName(final String logName) {
this.logName = logName;
}
public void setLogContent(final String logContent) {
this.logContent = logContent;
}
public void setAppName(final String appName) {
this.appName = appName;
}
public void setLogType(final Integer logType) {
this.logType = logType;
}
public void setRequestUrl(final String requestUrl) {
this.requestUrl = requestUrl;
}
public void setRequestParams(final String requestParams) {
this.requestParams = requestParams;
}
public void setRequestResult(final String requestResult) {
this.requestResult = requestResult;
}
public void setServerIp(final String serverIp) {
this.serverIp = serverIp;
}
public void setClientIp(final String clientIp) {
this.clientIp = clientIp;
}
public void setUserId(final Long userId) {
this.userId = userId;
}
public void setHttpMethod(final String httpMethod) {
this.httpMethod = httpMethod;
}
public void setClientBrowser(final String clientBrowser) {
this.clientBrowser = clientBrowser;
}
public void setClientOs(final String clientOs) {
this.clientOs = clientOs;
}
public String toString() {
return "SysLog(logId=" + this.getLogId() + ", logName=" + this.getLogName() + ", logContent=" + this.getLogContent() + ", appName=" + this.getAppName() + ", logType=" + this.getLogType() + ", requestUrl=" + this.getRequestUrl() + ", requestParams=" + this.getRequestParams() + ", requestResult=" + this.getRequestResult() + ", serverIp=" + this.getServerIp() + ", clientIp=" + this.getClientIp() + ", userId=" + this.getUserId() + ", httpMethod=" + this.getHttpMethod() + ", clientBrowser=" + this.getClientBrowser() + ", clientOs=" + this.getClientOs() + ")";
}
}
\ No newline at end of file
package com.junmp.jyzb.logManager;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.junmp.jyzb.logManager.service.EsLogService;
import com.junmp.v2.log.api.LogRecordApi;
import com.junmp.v2.log.api.bean.dto.LogRecordDto;
import com.junmp.v2.log.api.thread.LogManagerPool;
import com.junmp.v2.log.db.entity.SysLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
public class ESRecordManager implements LogRecordApi {
private static final Logger log = LoggerFactory.getLogger(com.junmp.v2.log.db.DbLogRecordManager.class);
private final EsLogService esService;
private final LogManagerPool logManagerPool;
private final LogRefreshManager logRefreshManager;
public ESRecordManager(LogManagerPool logManagerPool, EsLogService esService) {
this.logManagerPool = logManagerPool;
this.esService = esService;
this.logRefreshManager = new ESRecordManager.LogRefreshManager();
this.logRefreshManager.start();
}
public void add(LogRecordDto logRecord) {
if (null != logRecord) {
this.addBatch(CollectionUtil.list(false, new LogRecordDto[]{logRecord}));
}
}
public void addAsync(final LogRecordDto logRecord) {
this.logManagerPool.executeLog(new TimerTask() {
public void run() {
ESRecordManager.this.logRefreshManager.putLog(logRecord);
}
});
}
public void addBatch(List<LogRecordDto> logRecords) {
if (!ObjectUtil.isEmpty(logRecords)) {
List<SysLog> sysLogList = (List)logRecords.stream().map((dto) -> {
SysLog sysLog = new SysLog();
BeanUtil.copyProperties(dto, sysLog, new String[0]);
if (StrUtil.isEmpty(sysLog.getLogName())) {
sysLog.setLogName("API接口日志记录");
}
if (StrUtil.isEmpty(sysLog.getAppName())) {
sysLog.setAppName("none-app-name");
}
return sysLog;
}).collect(Collectors.toList());
if (ObjectUtil.isNotEmpty(sysLogList)) {
// this.esService.saveBatch(sysLogList);
}
}
}
class LogRefreshManager extends Thread {
private final long sleepTime;
private final int maxCount;
private final AtomicLong refreshMark = new AtomicLong();
public AtomicInteger count = new AtomicInteger(0);
private final Queue<LogRecordDto> queue = new ConcurrentLinkedQueue();
public LogRefreshManager() {
this.sleepTime = 3000L;
this.maxCount = 300;
}
public LogRefreshManager(long sleepTime) {
this.sleepTime = sleepTime;
this.maxCount = 300;
}
public LogRefreshManager(int maxCount) {
this.sleepTime = 3000L;
this.maxCount = maxCount;
}
public LogRefreshManager(long sleepTime, int maxCount) {
this.sleepTime = sleepTime;
this.maxCount = maxCount;
}
public void putLog(LogRecordDto record) {
int queueDataCount = this.count.get();
if (queueDataCount == 0) {
this.refreshMark.getAndSet(System.currentTimeMillis());
}
if (queueDataCount < this.maxCount * 2) {
this.queue.offer(record);
this.count.incrementAndGet();
}
}
private void refresh() {
this.refreshMark.getAndSet(System.currentTimeMillis());
int num = this.count.getAndSet(0);
List<LogRecordDto> cacheAll = new ArrayList(num);
for(int i = 0; i < num; ++i) {
LogRecordDto item = (LogRecordDto)this.queue.poll();
if (null == item) {
break;
}
cacheAll.add(item);
}
// if (null != cacheAll && cacheAll.size() > 0) {
// this.addBatch(cacheAll);
// }
}
private void timing() {
long currentTimeMillis = System.currentTimeMillis();
if (this.refreshMark.get() + this.sleepTime <= currentTimeMillis && this.count.get() > 0) {
this.refresh();
}
}
private void listener() {
if (this.count.get() >= this.maxCount) {
this.refresh();
}
}
public void run() {
try {
while(true) {
this.listener();
this.timing();
TimeUnit.MILLISECONDS.sleep(10L);
}
} catch (InterruptedException var2) {
if (ESRecordManager.log.isDebugEnabled()) {
var2.printStackTrace();
}
ESRecordManager.log.error(var2.getMessage());
}
}
}
}
\ No newline at end of file
package com.junmp.jyzb.logManager;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.junmp.v2.db.api.page.PageResult;
import com.junmp.v2.log.api.LogManagerApi;
import com.junmp.v2.log.api.bean.dto.LogRecordDto;
import com.junmp.v2.log.api.bean.req.LogRequest;
import com.junmp.v2.log.db.entity.SysLog;
import com.junmp.v2.log.db.service.SysLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.*;
public class EsLogManager implements LogManagerApi {
private static final Logger log = LoggerFactory.getLogger(EsLogManager.class);
@Resource
private SysLogService sysLogService;
public EsLogManager() {
}
public List<LogRecordDto> findList(LogRequest request) {
List<SysLog> sysLogList = this.sysLogService.findList(request);
List<LogRecordDto> dtoList = CollUtil.newArrayList(new LogRecordDto[0]);
BeanUtil.copyProperties(sysLogList, dtoList, new String[0]);
return dtoList;
}
public PageResult<LogRecordDto> findPage(LogRequest request) {
return this.sysLogService.getLogPage(request);
}
public void del(LogRequest request) {
this.sysLogService.del(request);
}
public void clearLog(LogRequest request) {
if (ObjectUtil.isNotNull(request) && ObjectUtil.isNotNull(request.getLogType())) {
this.sysLogService.clearLogByDate(request);
}
}
public LogRecordDto detail(LogRequest request) {
SysLog detail = this.sysLogService.detail(request);
LogRecordDto dto = new LogRecordDto();
BeanUtil.copyProperties(detail, dto, new String[0]);
return dto;
}
}
package com.junmp.jyzb.logManager.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.junmp.jyzb.api.bean.dto.ESTypeDto;
import com.junmp.jyzb.api.bean.dto.EquipmentTreeDto;
import com.junmp.jyzb.api.bean.dto.EquipmentTypeDto;
import com.junmp.jyzb.api.bean.query.QueryEquipmentTypeReq;
import com.junmp.jyzb.api.bean.req.UpdateEquipmentTypeReq;
import com.junmp.jyzb.entity.EquipmentType;
import com.junmp.jyzb.entity.EsLog;
import com.junmp.jyzb.utils.ResponseResult;
import java.io.IOException;
import java.util.List;
public interface EsLogService extends IService<EsLog> {
Object addEs();
}
\ No newline at end of file
package com.junmp.jyzb.logManager.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.junmp.jyzb.entity.EquipmentType;
import com.junmp.jyzb.entity.EsLog;
import com.junmp.jyzb.logManager.service.EsLogService;
import com.junmp.jyzb.mapper.EquipmentTypeMapper;
import com.junmp.jyzb.mapper.EsLogMapper;
import com.junmp.jyzb.service.EquipmentTypeService;
import org.springframework.stereotype.Service;
@Service
public class EsLogServiceImpl extends ServiceImpl<EsLogMapper, EsLog> implements EsLogService {
@Override
public Object addEs() {
return null;
}
}
package com.junmp.jyzb.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.junmp.jyzb.entity.EquipmentType;
import com.junmp.jyzb.entity.EsLog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface EsLogMapper extends BaseMapper<EsLog> {
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论