Skip to content

Commit 539c2b7

Browse files
committed
新增日志切面
1 parent c46c209 commit 539c2b7

4 files changed

Lines changed: 124 additions & 9 deletions

File tree

‎spring-boot-logback/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
<groupId>org.springframework.boot</groupId>
4040
<artifactId>spring-boot-autoconfigure</artifactId>
4141
</dependency>
42+
<dependency>
43+
<groupId>com.alibaba</groupId>
44+
<artifactId>fastjson</artifactId>
45+
<version>1.2.31</version>
46+
</dependency>
4247
</dependencies>
4348

4449
<build>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package io.ymq.logback.config;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
import com.alibaba.fastjson.serializer.SerializerFeature;
5+
import io.ymq.logback.config.annotation.*;
6+
import org.aspectj.lang.ProceedingJoinPoint;
7+
import org.aspectj.lang.annotation.Around;
8+
import org.aspectj.lang.annotation.Aspect;
9+
import org.aspectj.lang.annotation.Pointcut;
10+
import org.aspectj.lang.reflect.MethodSignature;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
import org.springframework.stereotype.Component;
14+
15+
import java.lang.reflect.Method;
16+
17+
18+
/**
19+
* Created by junbaor on 2017/5/12.
20+
*/
21+
@Aspect
22+
@Component
23+
public class LogAspect {
24+
25+
private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
26+
private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
27+
private static final String STRING_START = "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n";
28+
private static final String STRING_END = "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n";
29+
30+
@Pointcut("execution(* io.ymq.logback..*(..))")
31+
public void serviceLog() {
32+
}
33+
34+
@Around("serviceLog()")
35+
public Object around(ProceedingJoinPoint joinPoint) {
36+
try {
37+
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
38+
Method method = signature.getMethod();
39+
Class<?> targetClass = method.getDeclaringClass();
40+
41+
StringBuffer classAndMethod = new StringBuffer();
42+
43+
Log classAnnotation = targetClass.getAnnotation(Log.class);
44+
Log methodAnnotation = method.getAnnotation(Log.class);
45+
46+
if (classAnnotation != null) {
47+
if (classAnnotation.ignore()) {
48+
return joinPoint.proceed();
49+
}
50+
classAndMethod.append(classAnnotation.value()).append("-");
51+
}
52+
53+
if (methodAnnotation != null) {
54+
if (methodAnnotation.ignore()) {
55+
return joinPoint.proceed();
56+
}
57+
classAndMethod.append(methodAnnotation.value());
58+
}
59+
60+
String target = targetClass.getName() + "#" + method.getName();
61+
String params = JSONObject.toJSONStringWithDateFormat(joinPoint.getArgs(), dateFormat, SerializerFeature.WriteMapNullValue);
62+
63+
log.info(STRING_START + "{} 开始调用--> {} 参数:{}", classAndMethod.toString(), target, params);
64+
65+
long start = System.currentTimeMillis();
66+
Object result = joinPoint.proceed();
67+
long timeConsuming = System.currentTimeMillis() - start;
68+
69+
log.info("\n{} 调用结束<-- {} 返回值:{} 耗时:{}ms" + STRING_END, classAndMethod.toString(), target, JSONObject.toJSONStringWithDateFormat(result, dateFormat, SerializerFeature.WriteMapNullValue), timeConsuming);
70+
return result;
71+
} catch (Throwable throwable) {
72+
log.error(throwable.getMessage(), throwable);
73+
}
74+
return null;
75+
}
76+
77+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.ymq.logback.config.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Created by junbaor on 2017/5/12.
10+
*/
11+
@Target({ElementType.TYPE, ElementType.METHOD})
12+
@Retention(RetentionPolicy.RUNTIME)
13+
public @interface Log {
14+
15+
/**
16+
* 说明信息
17+
*
18+
* @return
19+
*/
20+
String value() default "";
21+
22+
/**
23+
* 是否忽略
24+
*
25+
* @return
26+
*/
27+
boolean ignore() default false;
28+
}
Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package io.ymq.logback.controller;
22

3+
import io.ymq.logback.config.annotation.Log;
34
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
5-
import org.springframework.web.bind.annotation.GetMapping;
6-
7-
import org.springframework.web.bind.annotation.RequestMapping;
8-
import org.springframework.web.bind.annotation.RestController;
6+
import org.springframework.web.bind.annotation.*;
97

108
import java.time.LocalDateTime;
119

@@ -16,17 +14,24 @@
1614
* @create 2017-10-30 14:20
1715
**/
1816
@RestController
19-
@RequestMapping("/")
17+
@RequestMapping(value = "/index")
2018
public class IndexController {
2119

2220
private static final Logger LOGGER = LoggerFactory.getLogger(IndexController.class);
2321

24-
@GetMapping(value = {""})
25-
public String index() {
22+
/**
23+
* http://127.0.0.1:8080/index/?content="我是测试内容"
24+
*
25+
* @param content
26+
* @return
27+
*/
28+
@Log("首页IndexController")
29+
@RequestMapping(value="", method= RequestMethod.GET)
30+
public String index(@RequestParam String content) {
2631
LocalDateTime localDateTime = LocalDateTime.now();
2732

28-
LOGGER.info("测试 logback:{}",localDateTime);
33+
LOGGER.info("测试 logback:{},content:{}", localDateTime, content);
2934

30-
return "测试 logback:" + localDateTime;
35+
return "测试 logback:" + localDateTime + ",content:" + content;
3136
}
3237
}

0 commit comments

Comments
 (0)