From 33cdb7fea1f6f272218fe85e34639668372fde8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E4=BC=9F=E5=8D=8E?= Date: Thu, 5 Dec 2019 10:16:09 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=96=B0=E5=A2=9E=E6=97=A5=E5=BF=97=E6=B8=85?= =?UTF-8?q?=E7=A9=BA=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../me/zhengjie/repository/LogRepository.java | 12 ++++++++++ .../java/me/zhengjie/rest/LogController.java | 22 +++++++++++++++---- .../java/me/zhengjie/service/LogService.java | 10 +++++++++ .../zhengjie/service/impl/LogServiceImpl.java | 12 ++++++++++ 4 files changed, 52 insertions(+), 4 deletions(-) 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"); + } }