diff --git a/eladmin-logging/src/main/java/me/zhengjie/repository/LogRepository.java b/eladmin-logging/src/main/java/me/zhengjie/repository/LogRepository.java index 03bee453..b838165f 100644 --- a/eladmin-logging/src/main/java/me/zhengjie/repository/LogRepository.java +++ b/eladmin-logging/src/main/java/me/zhengjie/repository/LogRepository.java @@ -3,9 +3,12 @@ package me.zhengjie.repository; import me.zhengjie.domain.Log; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import javax.transaction.Transactional; + /** * @author Zheng Jie * @date 2018-11-24 @@ -21,4 +24,13 @@ public interface LogRepository extends JpaRepository, JpaSpecification */ @Query(value = "select count(*) FROM (select request_ip FROM log where create_time between ?1 and ?2 GROUP BY request_ip) as s",nativeQuery = true) Long findIp(String date1, String date2); + + /** + * 根据日志类型删除信息 + * @param logType + */ + @Query(nativeQuery = true,value = "delete from log where log_type = ?1") + @Modifying + @Transactional + void deleteByLogType(String logType); } diff --git a/eladmin-logging/src/main/java/me/zhengjie/rest/LogController.java b/eladmin-logging/src/main/java/me/zhengjie/rest/LogController.java index e7bd4443..6e2dc6a4 100644 --- a/eladmin-logging/src/main/java/me/zhengjie/rest/LogController.java +++ b/eladmin-logging/src/main/java/me/zhengjie/rest/LogController.java @@ -10,10 +10,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @@ -80,4 +77,21 @@ public class LogController { public ResponseEntity getErrorLogs(@PathVariable Long id){ return new ResponseEntity<>(logService.findByErrDetail(id), HttpStatus.OK); } + @DeleteMapping(value = "/del/error") + @Log("删除所有ERROR日志") + @ApiOperation("删除所有ERROR日志") + @PreAuthorize("@el.check()") + public ResponseEntity delAllByError(){ + logService.delAllByError(); + return new ResponseEntity(HttpStatus.OK); + } + + @DeleteMapping(value = "/del/info") + @Log("删除所有INFO日志") + @ApiOperation("删除所有INFO日志") + @PreAuthorize("@el.check()") + public ResponseEntity delAllByInfo(){ + logService.delAllByInfo(); + return new ResponseEntity(HttpStatus.OK); + } } diff --git a/eladmin-logging/src/main/java/me/zhengjie/service/LogService.java b/eladmin-logging/src/main/java/me/zhengjie/service/LogService.java index 6fbcad75..fb1a0549 100644 --- a/eladmin-logging/src/main/java/me/zhengjie/service/LogService.java +++ b/eladmin-logging/src/main/java/me/zhengjie/service/LogService.java @@ -64,4 +64,14 @@ public interface LogService { * @throws IOException / */ void download(List logs, HttpServletResponse response) throws IOException; + + /** + * 删除所有错误日志 + */ + void delAllByError(); + + /** + * 删除所有INFO日志 + */ + void delAllByInfo(); } diff --git a/eladmin-logging/src/main/java/me/zhengjie/service/impl/LogServiceImpl.java b/eladmin-logging/src/main/java/me/zhengjie/service/impl/LogServiceImpl.java index 56f7a6d1..8c2022d2 100644 --- a/eladmin-logging/src/main/java/me/zhengjie/service/impl/LogServiceImpl.java +++ b/eladmin-logging/src/main/java/me/zhengjie/service/impl/LogServiceImpl.java @@ -137,4 +137,16 @@ public class LogServiceImpl implements LogService { } FileUtil.downloadExcel(list, response); } + + @Override + @Transactional(rollbackFor = Exception.class) + public void delAllByError() { + logRepository.deleteByLogType("ERROR"); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void delAllByInfo() { + logRepository.deleteByLogType("INFO"); + } }