通用日志记录

This commit is contained in:
zhh 2018-04-18 17:25:18 +08:00
parent 8dde877a9f
commit 2d4dc7e0c1
11 changed files with 171 additions and 28 deletions

View File

@ -31,6 +31,7 @@ crud操作demo | ✔
集成监控功能 | ✔
包结构调整 | ✔
SpringSecurity登录改为Restful形式 |
SpringAOP通用日志处理 |
### 功能完善

View File

@ -51,6 +51,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>

View File

@ -0,0 +1,48 @@
package com.macro.mall.component;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
/**
* 统一日志处理切面
*/
@Aspect
@Component
public class WebLogAspect {
private static final Logger LOGGER = LoggerFactory.getLogger(WebLogAspect.class);
private ThreadLocal<Long> startTime = new ThreadLocal<>();
@Pointcut("execution(public * com.macro.mall.controller.*.*(..))")
public void webLog() {
}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
startTime.set(System.currentTimeMillis());
//获取当前请求对象
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
//记录请求信息(可以记录到数据库中)
String classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
String args = Arrays.toString(joinPoint.getArgs());
LOGGER.info("HTTP URL:{}; HTTP_METHOD:{}; IP:{}; CLASS_METHOD:{}; ARGS:{}",
request.getRequestURL(), request.getMethod(), request.getRemoteAddr(), classMethod, args);
}
@AfterReturning(value = "webLog()", returning = "ret")
public void doAfterReturning(Object ret) throws Throwable {
long costTime = System.currentTimeMillis() - startTime.get();
LOGGER.info("RESPONSE:{}; COST_TIME:{}ms", ret, costTime);
}
}

View File

@ -25,8 +25,6 @@ public class PmsBrandController {
@Autowired
private PmsBrandService brandService;
private static final Logger LOGGER = LoggerFactory.getLogger(PmsBrandController.class);
@ApiOperation(value = "获取全部品牌列表")
@RequestMapping(value = "/listAll", method = RequestMethod.GET)
@ResponseBody
@ -45,10 +43,8 @@ public class PmsBrandController {
int count = brandService.createBrand(pmsBrand);
if (count == 1) {
commonResult = new CommonResult().success(pmsBrand);
LOGGER.debug("createBrand success:{}", pmsBrand);
} else {
commonResult = new CommonResult().failed();
LOGGER.debug("createBrand failed:{}", pmsBrand);
}
return commonResult;
}
@ -64,10 +60,8 @@ public class PmsBrandController {
int count = brandService.updateBrand(id, pmsBrandParam);
if (count == 1) {
commonResult = new CommonResult().success(pmsBrandParam);
LOGGER.debug("updateBrand success:{}", pmsBrandParam);
} else {
commonResult = new CommonResult().failed();
LOGGER.debug("updateBrand failed:{}", pmsBrandParam);
}
return commonResult;
}
@ -78,10 +72,8 @@ public class PmsBrandController {
public Object deleteBrand(@PathVariable("id") Long id) {
int count = brandService.deleteBrand(id);
if (count == 1) {
LOGGER.debug("deleteBrand success:id={}", id);
return new CommonResult().success(null);
} else {
LOGGER.debug("deleteBrand failed:id={}", id);
return new CommonResult().failed();
}
}
@ -108,10 +100,8 @@ public class PmsBrandController {
public Object deleteBrandBatch(@RequestParam("ids") List<Long> ids) {
int count = brandService.deleteBrand(ids);
if (count > 0) {
LOGGER.debug("deleteBrandBatch success:ids={}", ids);
return new CommonResult().success(count);
} else {
LOGGER.debug("deleteBrandBatch failed:ids={}", ids);
return new CommonResult().failed();
}
}
@ -123,10 +113,8 @@ public class PmsBrandController {
@RequestParam("showStatus") Integer showStatus) {
int count = brandService.updateShowStatus(ids, showStatus);
if (count > 0) {
LOGGER.debug("updateShowStatus success:ids={}", ids);
return new CommonResult().success(count);
} else {
LOGGER.debug("updateShowStatus failed:ids={}", ids);
return new CommonResult().failed();
}
}
@ -138,10 +126,8 @@ public class PmsBrandController {
@RequestParam("factoryStatus") Integer factoryStatus) {
int count = brandService.updateFactoryStatus(ids, factoryStatus);
if (count > 0) {
LOGGER.debug("updateFactoryStatus success:{}", ids);
return new CommonResult().success(count);
} else {
LOGGER.debug("updateFactoryStatus failed:{}", ids);
return new CommonResult().failed();
}
}

View File

@ -18,7 +18,6 @@ import java.util.List;
@Controller
@RequestMapping("/productAttribute/category")
public class PmsProductAttributeCategoryController {
private static final Logger LOGGER = LoggerFactory.getLogger(PmsProductAttributeCategoryController.class);
@Autowired
private PmsProductAttributeCategoryService productAttributeCategoryService;
@ -28,10 +27,8 @@ public class PmsProductAttributeCategoryController {
public Object create(@RequestParam String name) {
int count = productAttributeCategoryService.create(name);
if (count > 0) {
LOGGER.debug("create success name:{}", name);
return new CommonResult().success(count);
} else {
LOGGER.debug("create failed name:{}", name);
return new CommonResult().failed();
}
}
@ -42,10 +39,8 @@ public class PmsProductAttributeCategoryController {
public Object update(@PathVariable Long id, @RequestParam String name) {
int count = productAttributeCategoryService.update(id, name);
if (count > 0) {
LOGGER.debug("update success id:{}", id);
return new CommonResult().success(count);
} else {
LOGGER.debug("update failed id:{}", id);
return new CommonResult().failed();
}
}
@ -56,10 +51,8 @@ public class PmsProductAttributeCategoryController {
public Object delete(@PathVariable Long id) {
int count = productAttributeCategoryService.delete(id);
if (count > 0) {
LOGGER.debug("delete success name:{}", id);
return new CommonResult().success(count);
} else {
LOGGER.debug("delete failed name:{}", id);
return new CommonResult().failed();
}
}

View File

@ -0,0 +1,19 @@
package com.macro.mall.controller;
import com.macro.mall.service.PmsProductAttributeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 商品属性管理Controller
*/
@Controller
@RequestMapping("/productAttribute")
public class PmsProductAttributeController {
private static final Logger LOGGER = LoggerFactory.getLogger(PmsProductAttributeController.class);
@Autowired
private PmsProductAttributeService productAttributeService;
}

View File

@ -21,7 +21,6 @@ import java.util.List;
@Controller
@RequestMapping("/productCategory")
public class PmsProductCategoryController {
private static final Logger LOGGER = LoggerFactory.getLogger(PmsProductCategoryController.class);
@Autowired
private PmsProductCategoryService productCategoryService;
@ -34,10 +33,8 @@ public class PmsProductCategoryController {
}
int count = productCategoryService.create(pmsProductCategoryParam);
if (count > 0) {
LOGGER.debug("create success {}", pmsProductCategoryParam);
return new CommonResult().success(count);
} else {
LOGGER.debug("create failed {}", pmsProductCategoryParam);
return new CommonResult().failed();
}
}
@ -54,10 +51,8 @@ public class PmsProductCategoryController {
}
int count = productCategoryService.update(id, pmsProductCategoryParam);
if (count > 0) {
LOGGER.debug("update success {}", pmsProductCategoryParam);
return new CommonResult().success(count);
} else {
LOGGER.debug("update failed {}", pmsProductCategoryParam);
return new CommonResult().failed();
}
}
@ -78,10 +73,8 @@ public class PmsProductCategoryController {
public Object delete(@PathVariable Long id) {
int count = productCategoryService.delete(id);
if (count > 0) {
LOGGER.debug("delete success id:{}", id);
return new CommonResult().success(count);
} else {
LOGGER.debug("delete failed id:{}", id);
return new CommonResult().failed();
}
}

View File

@ -1,6 +1,7 @@
package com.macro.mall.dto;
import com.github.pagehelper.PageInfo;
import com.macro.mall.util.JsonUtils;
import org.springframework.validation.BindingResult;
import java.util.HashMap;
@ -76,6 +77,11 @@ public class CommonResult {
return this;
}
@Override
public String toString() {
return JsonUtils.objectToJson(this);
}
public int getCode() {
return code;
}

View File

@ -0,0 +1,7 @@
package com.macro.mall.service;
/**
* 商品属性Service
*/
public interface PmsProductAttributeService {
}

View File

@ -0,0 +1,15 @@
package com.macro.mall.service.impl;
import com.macro.mall.mapper.PmsProductAttributeMapper;
import com.macro.mall.service.PmsProductAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 商品属性Service实现类
*/
@Service
public class PmsProductAttributeServiceImpl implements PmsProductAttributeService{
@Autowired
private PmsProductAttributeMapper productAttributeMapper;
}

View File

@ -0,0 +1,71 @@
package com.macro.mall.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
/**
* 淘淘商城自定义响应结构
*/
public class JsonUtils {
// 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* 将对象转换成json字符串
* <p>Title: pojoToJson</p>
* <p>Description: </p>
* @param data
* @return
*/
public static String objectToJson(Object data) {
try {
String string = MAPPER.writeValueAsString(data);
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
/**
* 将json结果集转化为对象
*
* @param jsonData json数据
* @param beanType 对象中的object类型
* @return
*/
public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
try {
T t = MAPPER.readValue(jsonData, beanType);
return t;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将json数据转换成pojo对象list
* <p>Title: jsonToList</p>
* <p>Description: </p>
* @param jsonData
* @param beanType
* @return
*/
public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) {
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
try {
List<T> list = MAPPER.readValue(jsonData, javaType);
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}