๐Ÿ”ฅ์šฐ๋‹นํƒ•ํƒ• ๊ฐœ๋ฐœ๊ธฐ - ๋กœ๊ทธ

jiholeeยท2022๋…„ 5์›” 4์ผ
0

Java Spring

๋ชฉ๋ก ๋ณด๊ธฐ
3/5
post-thumbnail

์˜จ๊ฐ– ์˜ˆ์ƒ์„ ๋›ฐ์–ด๋„˜๋Š” ์„œ๋น„์Šค ์žฅ์• ์— ๋น ๋ฅด๊ฒŒ ๋Œ€๋น„ํ•˜๊ธฐ ์œ„ํ•ด Log๋ฅผ ์ž˜ ๋‚จ๊ฒจ์•ผ ํ•œ๋‹ค. ์—ด์‹ฌํžˆ (์•ˆ์ •์ ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š”) ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ํ…Œ์ŠคํŠธํ•ด๋„ ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์€ ํ•ญ์ƒ ์žˆ๋‹ค. ์žฅ์• ๊ฐ€ ๋‚œ ๊ณณ์„ ํŒŒ์•…ํ•˜๊ณ  ๋น ๋ฅด๊ฒŒ ๋Œ€์‘ํ•ด์„œ ํ”ผํ•ด๋ฅผ ์ตœ์†Œํ™”ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ํ”„๋กœ๊ทธ๋žจ ์ƒํƒœ๋ฅผ ๋ชจ๋‹ˆํ„ฐ๋งํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋ณด๋ฅผ ๊ธฐ๋กํ•˜๋Š” ๊ฒƒ์„ ๋กœ๊น…์ด๋ผ๊ณ  ํ•œ๋‹ค. ๋‚˜๋Š” slf4j ๋กœ๊น… ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ชจ๋“  ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๊ณ  ๋๋‚  ๋•Œ ๋กœ๊ทธ๋ฅผ ์ฐ์–ด ๋ณด๊ณ ์ž AOP ๊ฐœ๋…์„ ๊ณต๋ถ€ํ–ˆ๋‹ค.

์กฐ์ธ ํฌ์ธํŠธ(Join point)

์กฐ์ธ ํฌ์ธํŠธ๋Š” ์ถ”์ƒ์ ์ธ ๊ฐœ๋…์ด๋‹ค. AOP๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์ง€์ ์ด๋ผ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ์Šคํ”„๋ง AOP๋Š” ํ”„๋ก์‹œ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์กฐ์ธ ํฌ์ธํŠธ๋Š” ํ•ญ์ƒ ๋ฉ”์†Œ๋“œ ์‹คํ–‰ ์ง€์ ์œผ๋กœ ์ œํ•œ๋œ๋‹ค.

ํฌ์ธํŠธ์ปท (Pointcut)

์กฐ์ธ ํฌ์ธํŠธ ์ค‘์—์„œ ์–ด๋“œ๋ฐ”์ด์Šค๊ฐ€ ์ ์šฉ๋  ์œ„์น˜๋ฅผ ์„ ๋ณ„ํ•˜๋Š” ๊ธฐ๋Šฅ

์–ด๋“œ๋ฐ”์ด์Šค(Advice)

ํŠน์ • ์กฐ์ธ ํฌ์ธํŠธ์—์„œ Aspect์— ์˜ํ•ด ์ทจํ•ด์ง€๋Š” ์กฐ์น˜, Around(์ฃผ๋ณ€), Before(์ „), After(ํ›„)์™€ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ์–ด๋“œ๋ฐ”์ด์Šค๊ฐ€ ์žˆ์Œ

package hello.mentoring.aop;

import org.aspectj.lang.annotation.Pointcut;

public class Pointcuts {

    @Pointcut("execution(* hello.mentoring..*.*(..))")
    public void log() {}

    @Pointcut("execution(* hello.mentoring.controller.*.*(..))")
    public void controller() {}

    @Pointcut("execution(* hello.mentoring.service.*.*(..))")
    public void service() {}

    @Pointcut("execution(* hello.mentoring.repository.*.*(..))")
    public void repository() {}
}

์ฒ˜์Œ์—๋Š” ์„œ๋น„์Šค ํŒจํ‚ค์ง€์—๋งŒ ๋กœ๊ทธ๋ฅผ ๊ฑธ๊ณ  ์‹ถ์—ˆ๋Š”๋ฐ, ์ปจํŠธ๋กค๋Ÿฌ, ์„œ๋น„์Šค, ๋ ˆํฌ์ง€ํ† ๋ฆฌ ๋ชจ๋‘ ๋กœ๊ทธ๊ฐ€ ํ•„์š”ํ•  ๊ฒƒ ๊ฐ™์•„์„œ ๋‹ค ๋งŒ๋“ค์—ˆ๋‹ค.

์–ด๋“œ๋ฐ”์ด์Šค ์ข…๋ฅ˜

  • @Around : ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ „ํ›„์— ์ˆ˜ํ–‰, ๊ฐ€์žฅ ๊ฐ•๋ ฅํ•œ ์–ด๋“œ๋ฐ”์ด์Šค, ์กฐ์ธ ํฌ์ธํŠธ ์‹คํ–‰ ์—ฌ๋ถ€ ์„ ํƒ, ๋ฐ˜ํ™˜ ๊ฐ’ ๋ณ€ํ™˜, ์˜ˆ์™ธ ๋ณ€ํ™˜ ๋“ฑ์ด ๊ฐ€๋Šฅ
  • @Before : ์กฐ์ธ ํฌ์ธํŠธ ์‹คํ–‰ ์ด์ „์— ์‹คํ–‰
  • @AfterReturning : ์กฐ์ธ ํฌ์ธํŠธ๊ฐ€ ์ •์ƒ ์™„๋ฃŒํ›„ ์‹คํ–‰
  • @AfterThrowing : ๋ฉ”์„œ๋“œ๊ฐ€ ์˜ˆ์™ธ๋ฅผ ๋˜์ง€๋Š” ๊ฒฝ์šฐ ์‹คํ–‰
  • @After : ์กฐ์ธ ํฌ์ธํŠธ๊ฐ€ ์ •์ƒ ๋˜๋Š” ์˜ˆ์™ธ์— ๊ด€๊ณ„์—†์ด ์‹คํ–‰(finally)

package hello.mentoring.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.io.*;
import java.lang.reflect.Method;
import java.util.Date;

@Slf4j
@Aspect
@Component
public class LogAop {

    @Before("hello.mentoring.aop.Pointcuts.log()")
    public void before(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

		// ์กฐ์–ธ๋˜๋Š” ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•œ ์„ค๋ช…
        log.info("[before] {}", joinPoint.getSignature());

        Object[] args = joinPoint.getArgs();
        for (Object arg: args) {
            log.info("parameter value = {}", arg);
        }
    }

    @AfterReturning(value="hello.mentoring.aop.Pointcuts.log()", returning = "result")
    public void doRetutn(JoinPoint joinPoint, Object result) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        log.info("[return] {}", joinPoint.getSignature());

        Object[] args = joinPoint.getArgs();
        for (Object arg: args) {
            log.info("parameter value = {}", arg);
        }
    }

    @AfterThrowing(value="hello.mentoring.aop.Pointcuts.log()", throwing = "ex")
    public void doThrowing(JoinPoint joinPoint, Exception ex) {
        log.info("[ex] {} message={}", joinPoint.getSignature(), ex.getMessage());
    }



}

๋กœ๊ทธํŒŒ์ผ์„ ์ƒ์„ฑํ•  ๊ฒฝ๋กœ๋‚˜ ํŒŒ์ผ ์ด๋ฆ„์„ properties์— ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

#logging.file.path=/Users/jiho/Documents/Spring/file/
logging.file.name=log

0๊ฐœ์˜ ๋Œ“๊ธ€