代码修改
This commit is contained in:
@@ -1,33 +0,0 @@
|
||||
package org.nl.modules.log;
|
||||
|
||||
import org.slf4j.Marker;
|
||||
import org.slf4j.MarkerFactory;
|
||||
|
||||
public enum LogMarkerTypeEnum {
|
||||
DEFAULT("default", "默认日志"),
|
||||
REQUEST("request", "请求日志"),
|
||||
QUARTZ("quartz", "定时器日志"),
|
||||
DEVICE_EXECUTE("device_execute", "设备执行日志"),
|
||||
AGV_TASK_STATUS("agv_task_status", "agv任务状态日志"),
|
||||
AGV_FEEDBACK_SERVLET("agv_feedback_servlet", "反馈AGV请求"),
|
||||
AGV_LEAVE_SERVLET("agv_leave_servlet", "AGV请求离开"),
|
||||
ACS_TO_ERP("acs_to_erp", "ACS请求ERP"),
|
||||
ACS_TO_WMS("acs_to_wms", "ACS请求WMS"),
|
||||
AUTO_CREATE_INST("auto_create_inst", "自动创建指令"),
|
||||
NDC_SOCKET_CONNECTION_AUTORUN("ndc_socket_connection_autorun", "NDC自动连接"),
|
||||
XZ_AGV_TASK_STATUS("xz_agv_task_status", "仙知AGV指令状态"),
|
||||
WMS_TO_ACS("wms_to_acs", "WMS下发ACS");
|
||||
// 成员变量
|
||||
private String name;
|
||||
private String code;
|
||||
|
||||
// 构造方法
|
||||
private LogMarkerTypeEnum(String code, String name) {
|
||||
this.code = code;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static Marker getMarker(LogMarkerTypeEnum logType) {
|
||||
return MarkerFactory.getMarker(logType.code);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package org.nl.modules.log;
|
||||
|
||||
import org.slf4j.Marker;
|
||||
import org.slf4j.MarkerFactory;
|
||||
|
||||
public class LogMarkerUtil {
|
||||
private static final Marker MARKER = MarkerFactory.getMarker("test_marker");
|
||||
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package org.nl.modules.log;
|
||||
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import org.bson.Document;
|
||||
import org.slf4j.Marker;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
/**
|
||||
* https://www.cnblogs.com/lzghyh/p/14913230.html
|
||||
* https://juejin.cn/post/6844903488896385037
|
||||
* https://cloud.tencent.com/developer/article/1384035
|
||||
* https://www.freesion.com/article/229560377/
|
||||
*/
|
||||
public class MongoDBAppender extends MongoDBAppenderBase<ILoggingEvent> {
|
||||
public MongoDBAppender() {
|
||||
super("loggingEvents");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Document toMongoDocument(ILoggingEvent eventObject) {
|
||||
|
||||
final Document doc = new Document();
|
||||
doc.append("_id",IdUtil.simpleUUID());
|
||||
|
||||
doc.append("date", DateUtil.now());
|
||||
doc.append("source", source);
|
||||
try {
|
||||
doc.append("ip", InetAddress.getLocalHost().getHostAddress());
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Marker marker = eventObject.getMarker();
|
||||
if (!ObjectUtil.isEmpty(marker)) {
|
||||
doc.append("marker", marker.getName());
|
||||
}else {
|
||||
doc.append("marker", "default");
|
||||
}
|
||||
|
||||
doc.append("level", eventObject.getLevel().toString());
|
||||
doc.append("logger", eventObject.getLoggerName());
|
||||
doc.append("thread", eventObject.getThreadName());
|
||||
doc.append("message", eventObject.getFormattedMessage());
|
||||
if (eventObject.getMDCPropertyMap() != null && !eventObject.getMDCPropertyMap().isEmpty())
|
||||
doc.append("mdc", eventObject.getMDCPropertyMap());
|
||||
// ...
|
||||
return doc;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
package org.nl.modules.log;
|
||||
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.UnsynchronizedAppenderBase;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.MongoClientOptions;
|
||||
import com.mongodb.MongoCredential;
|
||||
import com.mongodb.ServerAddress;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import lombok.Data;
|
||||
import org.bson.Document;
|
||||
import org.slf4j.Marker;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* MongoDBAppender适配类
|
||||
*/
|
||||
@Data
|
||||
public abstract class MongoDBAppenderBase<E> extends UnsynchronizedAppenderBase<E> {
|
||||
private MongoClient mongo;
|
||||
MongoDatabase db;
|
||||
private MongoCollection<Document> eventsCollection;
|
||||
|
||||
private String host = "192.168.81.251"; // 地址
|
||||
private int port = 27017; // 端口号
|
||||
private String dbName = "db"; // 库名
|
||||
private String collectionName; // 集合名
|
||||
private String username; // 用户名
|
||||
private String password; // 密码
|
||||
protected String source;
|
||||
|
||||
private int connectionsPerHost = 10; // 空闲线程池中最大链接数
|
||||
private int threadsAllowedToBlockForConnectionMultiplier = 5; //一个线程等待链接可用的最大等待毫秒数
|
||||
private int maxWaitTime = 1000 * 60 * 2; // 最长等待时间
|
||||
private int connectTimeout;
|
||||
private int socketTimeout;
|
||||
private int wtimeout;
|
||||
|
||||
MongoDBAppenderBase(String collectionName) {
|
||||
this.collectionName = collectionName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
try {
|
||||
connectToMongoDB();
|
||||
super.start();
|
||||
} catch (UnknownHostException e) {
|
||||
addError("Error connecting to MongoDB server: " + host + ":" + port,
|
||||
e);
|
||||
}
|
||||
}
|
||||
|
||||
private void connectToMongoDB() throws UnknownHostException {
|
||||
// 用户名 数据库 密码
|
||||
if (username != null && password != null) {
|
||||
|
||||
MongoCredential credential = MongoCredential.createCredential(
|
||||
username, dbName, password.toCharArray());
|
||||
ServerAddress serverAddress = new ServerAddress(host, port);
|
||||
mongo = new MongoClient(serverAddress, Collections.singletonList(credential), buildOptions());
|
||||
} else {
|
||||
mongo = new MongoClient(new ServerAddress(host, port), buildOptions());
|
||||
}
|
||||
|
||||
db = mongo.getDatabase(dbName);
|
||||
eventsCollection = db.getCollection(collectionName);
|
||||
|
||||
}
|
||||
|
||||
private MongoClientOptions buildOptions() {
|
||||
final MongoClientOptions.Builder options = new MongoClientOptions.Builder();
|
||||
options.connectionsPerHost(connectionsPerHost);
|
||||
options.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier);
|
||||
options.maxWaitTime(maxWaitTime);
|
||||
options.connectTimeout(connectTimeout);
|
||||
options.socketTimeout(socketTimeout);
|
||||
options.maxWaitTime(wtimeout);
|
||||
return options.build();
|
||||
}
|
||||
|
||||
protected abstract Document toMongoDocument(E event);
|
||||
|
||||
@Override
|
||||
protected void append(E eventObject) {
|
||||
if (eventObject instanceof ILoggingEvent) {
|
||||
ILoggingEvent evt = (ILoggingEvent) eventObject;
|
||||
Marker marker = evt.getMarker();
|
||||
if (ObjectUtil.isEmpty(marker))
|
||||
marker = LogMarkerTypeEnum.getMarker(LogMarkerTypeEnum.DEFAULT);
|
||||
eventsCollection = db.getCollection(marker.getName());
|
||||
|
||||
}
|
||||
|
||||
eventsCollection.insertOne(toMongoDocument(eventObject));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
if (mongo != null)
|
||||
mongo.close();
|
||||
super.stop();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
|
||||
package org.nl.modules.log.rest;
|
||||
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.annotation.Log;
|
||||
import org.nl.modules.log.service.RootLogService;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author loujf
|
||||
* @date 2021-04-02
|
||||
**/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Api(tags = "系统日志")
|
||||
@RequestMapping("/api/rootLog")
|
||||
@Slf4j
|
||||
public class RootLogController {
|
||||
|
||||
private final RootLogService rootLogService;
|
||||
|
||||
@GetMapping
|
||||
@Log("查询日志")
|
||||
@ApiOperation("查询日志")
|
||||
//@PreAuthorize("@el.check('log:list')")
|
||||
public ResponseEntity<Object> query(@RequestParam Map whereJson, Pageable page) throws ParseException {
|
||||
return new ResponseEntity<>(rootLogService.queryAll(whereJson, page), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping("/deviceLog")
|
||||
@Log("查询设备日志")
|
||||
@ApiOperation("查询设备日志")
|
||||
//@PreAuthorize("@el.check('log:list')")
|
||||
public ResponseEntity<Object> queryDeviceLog(@RequestParam Map whereJson, Pageable page) throws ParseException {
|
||||
return new ResponseEntity<>(rootLogService.queryDeviceLog(whereJson, page), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping( "/error")
|
||||
@Log("查询异常详情")
|
||||
@ApiOperation("查询异常详情")
|
||||
public ResponseEntity<Object> findError(@RequestBody Map whereJson) {
|
||||
return new ResponseEntity<>(rootLogService.findError(whereJson), HttpStatus.OK);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package org.nl.modules.log.service;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 系统日志查询
|
||||
*/
|
||||
public interface RootLogService {
|
||||
/**
|
||||
* 查询数据分页
|
||||
*
|
||||
* @param whereJson 条件
|
||||
* @param page 分页参数
|
||||
* @return Map<String, Object>
|
||||
*/
|
||||
Object queryAll(Map whereJson, Pageable page) throws ParseException;
|
||||
|
||||
/**
|
||||
* 查询设备日志
|
||||
*
|
||||
* @param whereJson 条件
|
||||
* @param page 分页参数
|
||||
* @return Map<String, Object>
|
||||
*/
|
||||
Map<String,Object> queryDeviceLog(Map whereJson, Pageable page) throws ParseException;
|
||||
|
||||
|
||||
/**
|
||||
* 查询异常详情
|
||||
*
|
||||
* @param whereJson
|
||||
* @return Log
|
||||
*/
|
||||
String findError(Map whereJson);
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
package org.nl.modules.log.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.acs.device.service.DeviceService;
|
||||
import org.nl.modules.log.service.RootLogService;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author loujf
|
||||
* @description 服务实现
|
||||
* @date 2022-04-02
|
||||
**/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class RootLogServiceImpl implements RootLogService {
|
||||
|
||||
private final MongoTemplate mongoTemplate;
|
||||
|
||||
private final DeviceService deviceService;
|
||||
|
||||
@Override
|
||||
public Map<String, Object> queryAll(Map whereJson, Pageable page){
|
||||
String log_type = (String) whereJson.get("log_type");
|
||||
String log_level = (String) whereJson.get("log_level");
|
||||
String begin_time = (String) whereJson.get("begin_time");
|
||||
String end_time = (String) whereJson.get("end_time");
|
||||
|
||||
Query query = new Query().with(Sort.by(Sort.Order.desc("date")));
|
||||
|
||||
if (StrUtil.isEmpty(log_type)) {
|
||||
log_type = "default";
|
||||
}
|
||||
if (StrUtil.isNotEmpty(log_level)){
|
||||
query.addCriteria(Criteria.where("level").is(log_level));
|
||||
}
|
||||
if (StrUtil.isNotEmpty(begin_time) && StrUtil.isNotEmpty(end_time)) {
|
||||
query.addCriteria(Criteria.where("date")
|
||||
.gte(begin_time)
|
||||
.lte(end_time));
|
||||
}
|
||||
|
||||
//根据条件得到的总条数
|
||||
long totalSize = mongoTemplate.count(query, Map.class, log_type);
|
||||
|
||||
//处理分页
|
||||
query.skip(page.getPageNumber()).limit(page.getPageSize());
|
||||
List<Map> list = mongoTemplate.find(query,Map.class, log_type);
|
||||
//封装前端分页查询结果
|
||||
JSONObject result = new JSONObject();
|
||||
result.put("content", list);
|
||||
result.put("totalElements", totalSize);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> queryDeviceLog(Map whereJson, Pageable page){
|
||||
String device_code = (String) whereJson.get("device_code");
|
||||
String begin_time = (String) whereJson.get("begin_time");
|
||||
String end_time = (String) whereJson.get("end_time");
|
||||
|
||||
Query query = new Query().with(Sort.by(Sort.Order.desc("create_time")));
|
||||
|
||||
if (StrUtil.isEmpty(device_code)) {
|
||||
JSONArray array = deviceService.selectList();
|
||||
JSONObject json = array.getJSONObject(0);
|
||||
device_code = json.getString("device_code");
|
||||
}
|
||||
if (StrUtil.isNotEmpty(begin_time) && StrUtil.isNotEmpty(end_time)) {
|
||||
query.addCriteria(Criteria.where("create_time")
|
||||
.gte(begin_time)
|
||||
.lte(end_time));
|
||||
}
|
||||
|
||||
//根据条件得到的总条数
|
||||
long totalSize = mongoTemplate.count(query, Map.class, device_code);
|
||||
|
||||
//处理分页
|
||||
query.skip(page.getPageNumber()).limit(page.getPageSize());
|
||||
List<Map> list = mongoTemplate.find(query,Map.class, device_code);
|
||||
//封装前端分页查询结果
|
||||
JSONObject result = new JSONObject();
|
||||
result.put("content", list);
|
||||
result.put("totalElements", totalSize);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String findError(Map map) {
|
||||
String message = (String) map.get("message");
|
||||
// String marker = (String) map.get("marker");
|
||||
// Query query = Query.query(Criteria.where("_id").is(id)).with(Sort.by("date"));
|
||||
// JSONObject list = mongoTemplate.findOne(query,JSONObject.class,marker);
|
||||
|
||||
return message;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user