From 9456850daac74b98fa644ed41a7ef63e74b2309b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=9C=EA=B7=9C=EB=B9=88?= Date: Tue, 4 Oct 2022 19:27:11 +0900 Subject: [PATCH] =?UTF-8?q?=EB=A1=9C=EA=B7=B8=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 + build.gradle | 1 + .../aop/logger/LoggerAspect.java | 51 ++++++++++++ src/main/resources/application-dev.properties | 2 +- src/main/resources/application.yml | 6 +- src/main/resources/log4jdbc.log4j2.properties | 4 + src/main/resources/logback-spring.xml | 79 +++++++++++++++++++ 7 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/workduo/configuration/aop/logger/LoggerAspect.java create mode 100644 src/main/resources/log4jdbc.log4j2.properties create mode 100644 src/main/resources/logback-spring.xml diff --git a/.gitignore b/.gitignore index 296a191..155c905 100644 --- a/.gitignore +++ b/.gitignore @@ -45,3 +45,7 @@ src/jacoco/ ._* .Spotlight-V100 .Trashes + +### log ### +*.logs +*.log \ No newline at end of file diff --git a/build.gradle b/build.gradle index 13f7cf6..acef9f6 100644 --- a/build.gradle +++ b/build.gradle @@ -44,6 +44,7 @@ dependencies { implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE' implementation "com.querydsl:querydsl-jpa:${queryDslVersion}" implementation "com.querydsl:querydsl-apt:${queryDslVersion}" + implementation 'org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.h2database:h2' runtimeOnly 'mysql:mysql-connector-java' diff --git a/src/main/java/com/workduo/configuration/aop/logger/LoggerAspect.java b/src/main/java/com/workduo/configuration/aop/logger/LoggerAspect.java new file mode 100644 index 0000000..af0960c --- /dev/null +++ b/src/main/java/com/workduo/configuration/aop/logger/LoggerAspect.java @@ -0,0 +1,51 @@ +package com.workduo.configuration.aop.logger; + +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +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.time.LocalDateTime; +import java.util.HashMap; +import java.util.Map; + +@Slf4j +@Aspect +@Component +public class LoggerAspect { + + @Around("execution(* com.workduo..*Controller.*(..))") + public Object methodLogger(ProceedingJoinPoint pjp) throws Throwable { + + try { + Object result = pjp.proceed(); + + HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); + + String controllerName = pjp.getSignature().getDeclaringType().getSimpleName(); + String methodName = pjp.getSignature().getName(); + + Map params = new HashMap<>(); + + try { + params.put("controller", controllerName); + params.put("method", methodName); + params.put("log_time", LocalDateTime.now()); + params.put("request_uri", request.getRequestURI()); + params.put("http_method", request.getMethod()); + } catch (Exception e) { + log.error("LoggerAspect error", e); + } + + log.info("params : {}", params); + + return result; + } catch (Throwable throwable) { + throw throwable; + } + } +} diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index 13e43a9..5b7908f 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -1,4 +1,4 @@ -DB_HOST: jdbc:mysql://localhost:3306/workduo?autoReconnect=true&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true +DB_HOST: jdbc:log4jdbc:mysql://localhost:3306/workduo?autoReconnect=true&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true DB_NAME: workduo1 DB_PASSWORD: workduo1! REDIS_PORT: 6379 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 577cd7e..6409db8 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -5,10 +5,11 @@ spring: active: dev datasource: - driver-class-name: com.mysql.cj.jdbc.Driver +# driver-class-name: com.mysql.cj.jdbc.Driver url: ${DB_HOST} username: ${DB_NAME} password: ${DB_PASSWORD} + driver-class-name: net.sf.log4jdbc.sql.jdbcapi.DriverSpy redis: port: ${REDIS_PORT} @@ -47,8 +48,9 @@ spring: # password: # driver-class-name: org.h2.Driver logging: + config: classpath:logback-spring.xml level: - org.hibernate.SQL: debug +# org.hibernate.SQL: debug com.amazonaws.util.EC2MetadataUtils: ERROR diff --git a/src/main/resources/log4jdbc.log4j2.properties b/src/main/resources/log4jdbc.log4j2.properties new file mode 100644 index 0000000..842abb8 --- /dev/null +++ b/src/main/resources/log4jdbc.log4j2.properties @@ -0,0 +1,4 @@ +log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator +log4jdbc.dump.sql.maxlinelength=0 +log4jdbc.auto.load.popular.drivers=false +log4jdbc.drivers=com.mysql.cj.jdbc.Driver \ No newline at end of file diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..00c1f8a --- /dev/null +++ b/src/main/resources/logback-spring.xml @@ -0,0 +1,79 @@ + + + + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %highlight(%-5level) %magenta(%-4relative) --- [ %thread{10} ] %cyan(%logger{20}) : %msg%n + + + + + ${LOGS_NORMAL_PATH}/log_file.log + + [%d{yyyy-MM-dd HH:mm:ss}:%-3relative][%thread] %-5level %logger{35} - %msg%n + UTF-8 + + + ${LOGS_NORMAL_PATH}/%d{yyyy-MM-dd}_%i.log + + 10MB + + 7 + + + INFO + ACCEPT + DENY + + + + + ${LOGS_ERROR_PATH}/error_file.log + + [%d{yyyy-MM-dd HH:mm:ss}:%-3relative][%thread] %-5level %logger{35} - %msg%n + UTF-8 + + + ${LOGS_ERROR_PATH}/%d{yyyy-MM-dd}_error.log + 30 + + + ERROR + ACCEPT + DENY + + + + + + 1 + 0 + false + false + + + + + 1 + 0 + false + false + + + + + + + + + + + + + + + + \ No newline at end of file