实现了平常数据存mysql。日志数据存MongoDB的功能
2018.5.16更新 添加了按日期时间段的查询,更正了title查询,修改了返回值为时间格式
一、日志的bean类
package com.johnfnash.learn.mongodb.entity;
import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.index.CompoundIndexes;
import org.springframework.data.mongodb.core.mapping.Document;
import java.text.SimpleDateFormat;
import java.util.Date;
@Document(collection = "LoggerInfo")
@CompoundIndexes(
@CompoundIndex(name = "title", def = "{ title : 1 }")
)
public class LoggerInfo {
private static final long serialVersionUID = 4495390935587105239L;
private String title;
private Date createDate;
private String createDateStr;
private Object[] arguments;
private String targetName;
private String methodName;
private Object result;
private String ip;
public LoggerInfo() {
}
public LoggerInfo(String title, Date createDate, Object[] arguments, Object result, String targetName, String methodName, String ip) {
this.title = title;
this.createDate = createDate;
this.arguments = arguments;
this.targetName = targetName;
this.methodName = methodName;
this.result = result;
this.ip = ip;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getCreateDate() {
return new Date();
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public String getCreateDateStr() {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(this.createDate);
}
public void setCreateDateStr(String createDateStr) {
this.createDateStr = createDateStr;
}
public Object[] getArguments() {
return arguments;
}
public void setArguments(Object[] arguments) {
this.arguments = arguments;
}
public String getTargetName() {
return targetName;
}
public void setTargetName(String targetName) {
this.targetName = targetName;
}
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
}
二、service层
package com.johnfnash.learn.mongodb.service;
import com.johnfnash.learn.mongodb.entity.LoggerInfo;
import com.johnfnash.learn.mongodb.mapper.LoggerInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional(readOnly = true)
public class LoggerInfoService {
@Autowired
private LoggerInfoRepository infoRepository;
@Transactional(readOnly = false)
public void addLoggerInfo(LoggerInfo loggerInfo) {
infoRepository.addLoggerInfo(loggerInfo);
}
public List<LoggerInfo> getLoggerInfoListByTitle(String title) {
return infoRepository.getLoggerInfo(title);
}
public List<LoggerInfo> getLoggerInfoListByDate(String startDate, String endDate) {
return infoRepository.getLoggerBetweenTime(startDate, endDate);
}
}
三、mapper层
package com.johnfnash.learn.mongodb.mapper;
import com.johnfnash.learn.mongodb.entity.LoggerInfo;
import org.springframework.beans.factory.annotation.Autowired;
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.Repository;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Repository
public class LoggerInfoRepository {
@Autowired
private MongoTemplate mongoTemplate;
public List<LoggerInfo> getLoggerInfo(String title) {
Query query = new Query();
Criteria criteria = Criteria.where("title").is(title);
query.addCriteria(criteria);
query.with(new Sort(Sort.Direction.DESC, "createDate"));
return mongoTemplate.find(query, LoggerInfo.class);
}
public List<LoggerInfo> getLoggerBetweenTime(String startDate, String endDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date sdate = null, edate = null;
try {
sdate = sdf.parse(startDate);
edate = sdf.parse(endDate);
} catch (ParseException e) {
System.out.println("时间转换错误======");
e.printStackTrace();
}
Query query = new Query();
Criteria criteria = new Criteria();
if (sdate != null && edate != null) {
criteria = Criteria.where("createDate").gte(sdate).lte(edate);
}
query.addCriteria(criteria);
return mongoTemplate.find(query, LoggerInfo.class);
}
public void addLoggerInfo(LoggerInfo loggerInfo) {
mongoTemplate.insert(loggerInfo);
}
}
四、aop切面编程类
package com.johnfnash.learn.mongodb.config;
import com.johnfnash.learn.mongodb.entity.LoggerInfo;
import com.johnfnash.learn.mongodb.service.LoggerInfoService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import java.util.Date;
@Aspect
@Configuration
public class LoggerInfoAspect {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private LoggerInfoService loggerInfoService;
@Pointcut("execution(* com.johnfnash.learn.mongodb.service.*.get*(..))")
public void executeService() {
}
@Around("executeService()")
public Object doAround(ProceedingJoinPoint joinPoint) throws ClassNotFoundException {
Object result = null; // 返回值
LoggerInfo loggerInfo;
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
System.out.println("方法名称:"+methodName);
Object[] args = joinPoint.getArgs();
try {
result = joinPoint.proceed();
loggerInfo = new LoggerInfo("正确日志", new Date(), args, result, targetName, methodName, "");
loggerInfoService.addLoggerInfo(loggerInfo);
} catch (Throwable e) {
String message = e.getMessage();
loggerInfo = new LoggerInfo("错误日志", new Date(), args, message, targetName, methodName, "");
loggerInfoService.addLoggerInfo(loggerInfo);
logger.error("规则切面发生异常", e);
}
return result;
}
}
五、yml文件
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017
spring.data.mongodb.database=admin
六、测试类
package com.johnfnash.learn.mongodb.controller;
import com.johnfnash.learn.mongodb.entity.LoggerInfo;
import com.johnfnash.learn.mongodb.service.LoggerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/logger")
public class LoggerController {
@Autowired
private LoggerInfoService loggerInfoService;
@GetMapping("/listByTitle")
public List<LoggerInfo> getLoggerInfo(@RequestParam("title") String title){
List<LoggerInfo> loggerInfos = loggerInfoService.getLoggerInfoListByTitle(title);
return loggerInfos;
}
@GetMapping("/listByDate")
public List<LoggerInfo> getInfoByDate(@RequestParam("startDate") String startDate , @RequestParam("endDate") String endDate){
List<LoggerInfo> loggerInfos = loggerInfoService.getLoggerInfoListByDate(startDate, endDate);
return loggerInfos;
}
}
七、pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.johnfnash.learn</groupId>
<artifactId>mongodb-aspect</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mongodb</name>
<description>Mongodb project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
本文介绍了一种使用AOP切面编程结合MongoDB进行日志存储的方法,包括实体类、Service层、Mapper层、AOP切面编程类、YML配置、测试类及Maven配置,实现了按日期查询和标题查询的日志检索功能。
2046

被折叠的 条评论
为什么被折叠?



