Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- try - with - resources
- Java Graphql
- mTLS
- 상호 인증
- tomcat jndi
- 개방 폐쇄 원칙
- Reading HttpServletRequest Multiple Times
- 데이터 압축
- Unchecked Exception
- WildFly
- Tomcat DBCP
- Graphql Client
- Request Body 여러 번 사용
- Srping MVC
- Socket is closed
- java
- mapstruct
- Open Close Principal
- Jndi DataSource
- NoUniqueBeanDefinitionException
- graphql
- Java Rest
- requestheaderdto
- Checked Exception
- HandlerMethodArgumentResolver
- 이중정렬
- Java Singleton
- Sub Bytes
- 바이트 절삭
- AfterMapping
Archives
- Today
- Total
Developer Sang Guy
[Spring] Aop, Logback 활용 본문
Aop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
@Component
@Aspect
public class MyAspect {
private final Logger logger = LoggerFactory.getLogger("tLogger");
@Before("execution(* com.example.demo.service.MyService.*(..))")
public void before(JoinPoint jp) throws Exception {
MethodSignature methodSignature = (MethodSignature) jp.getSignature();
Method method = methodSignature.getMethod();
logger.info("Call Before : " + method.getName());
}
@After("execution(* com.example.demo.service.MyService.*(..))")
public void after(JoinPoint jp) throws Exception {
MethodSignature methodSignature = (MethodSignature) jp.getSignature();
Method method = methodSignature.getMethod();
logger.info("Call After : " + method.getName());
}
@AfterThrowing(pointcut = "execution(* com.example.demo.service.MyService.*(..))", throwing = "e")
public void afterThrowing(JoinPoint jp, Exception e) throws Exception {
MethodSignature methodSignature = (MethodSignature) jp.getSignature();
Method method = methodSignature.getMethod();
logger.info("Call AfterThrowing : " + method.getName() + " / " + e);
}
@Around("execution(* com.example.demo.service.MyService.*(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
logger.info("Call Around Target : " + pjp.getTarget());
logger.info("Call Around Parameter : " + Arrays.toString(pjp.getArgs()));
Object object = null;
try {
object = pjp.proceed();
} catch (Exception e) {
logger.error("Call Around Exception : " + e);
}
long end = System.currentTimeMillis();
logger.info("Call Around Time : " + (end - start));
return object;
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
|
@Service
public class MyService {
public int sum(int a, int b) throws Exception {
return a + b;
}
public void parseInteger(String str) throws Exception {
Integer.parseInt(str);
}
}
|
cs |
1
2
3
4
5
6
7
8
|
@PostMapping("/aspect")
public void aspect(@RequestBody String body) throws Exception {
TestVO vo = new ObjectMapper().readValue(body, TestVO.class);
myService.sum(vo.getValue().getNum1().intValue(), vo.getValue().getNum2().intValue());
myService.parseInteger("String");
logger.info("MyController Finish.");
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
INFO 22-04-28 14:44:06[http-nio-8080-exec-2] [tLogger:53] - Call Around Target : com.example.demo.service.MyService@1091d063
INFO 22-04-28 14:44:06[http-nio-8080-exec-2] [tLogger:54] - Call Around Parameter : [10, 7]
INFO 22-04-28 14:44:06[http-nio-8080-exec-2] [tLogger:29] - Call Before : sum
INFO 22-04-28 14:44:06[http-nio-8080-exec-2] [tLogger:37] - Call After : sum
INFO 22-04-28 14:44:06[http-nio-8080-exec-2] [tLogger:67] - Call Around Time : 11
INFO 22-04-28 14:44:06[http-nio-8080-exec-2] [tLogger:53] - Call Around Target : com.example.demo.service.MyService@1091d063
INFO 22-04-28 14:44:06[http-nio-8080-exec-2] [tLogger:54] - Call Around Parameter : [String]
INFO 22-04-28 14:44:06[http-nio-8080-exec-2] [tLogger:29] - Call Before : parseInteger
INFO 22-04-28 14:44:06[http-nio-8080-exec-2] [tLogger:45] - Call AfterThrowing : parseInteger / java.lang.NumberFormatException: For input string: "String"
INFO 22-04-28 14:44:06[http-nio-8080-exec-2] [tLogger:37] - Call After : parseInteger
ERROR 22-04-28 14:44:06[http-nio-8080-exec-2] [tLogger:63] - Call Around Exception : java.lang.NumberFormatException: For input string: "String"
INFO 22-04-28 14:44:06[http-nio-8080-exec-2] [tLogger:67] - Call Around Time : 0
INFO 22-04-28 14:44:06[http-nio-8080-exec-2] [tLogger:31] - MyController Finish.
|
cs |
Logback
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_PATTERN" value="%-5level %d{yy-MM-dd HH:mm:ss}[%thread] [%logger{0}:%line] - %msg%n"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 파일경로 설정 -->
<file>C:/Users/user/Documents/로그/test.log</file>
<!-- 출력패턴 설정 -->
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${LOG_PATTERN}</pattern>
</encoder>
<!-- Rolling 정책 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- .gz,.zip 등을 넣으면 자동 일자별 로그파일 압축 -->
<fileNamePattern>C:/Users/user/Documents/로그/test.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- <fileNamePattern>C:/Users/user/Documents/로그/test.%d{yyyy-MM-dd}_%i.log</fileNamePattern> -->
<!-- <timeBasedFileNamingAndTriggeringPolicy -->
<!-- class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> -->
<!-- 파일당 최고 용량 kb, mb, gb -->
<!-- <maxFileSize>10MB</maxFileSize> -->
<!-- </timeBasedFileNamingAndTriggeringPolicy> -->
<!-- 일자별 로그파일 최대 보관주기(~일), 해당 설정일 이상된 파일은 자동으로 제거 -->
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="CONSOLE" />
</root>
<logger name="tLogger" level="info" additivity="false">
<appender-ref ref="FILE" />
</logger>
</configuration>
|
cs |
'Spring' 카테고리의 다른 글
HttpServletRequest Body 여러 번 읽기 (0) | 2022.12.28 |
---|---|
[Spring] RequestMapping 기능 (produces, consumes ) (0) | 2022.10.07 |
[Spring Boot] DB 연결 (0) | 2022.09.28 |
스프링 WebMvcConfigurer (0) | 2021.08.08 |
Spring @Autowired를 사용한 스프링 빈 주입 (0) | 2021.05.31 |
Comments