From 5d86c30b83b1c454197690d1008e19f14c86d283 Mon Sep 17 00:00:00 2001 From: "Jonguk. Lim" Date: Wed, 31 Jul 2024 17:36:37 +0900 Subject: [PATCH] =?UTF-8?q?config:=20hibernate=20SQL=20log=20disable=20=20?= =?UTF-8?q?=20=20=20=20=20=20=20p6spy=20log=20=EC=9E=AC=EC=A0=95=EC=9D=98?= =?UTF-8?q?=20=20=20=20=20=20=20=20=20pom.xml=20=EC=84=A4=EC=A0=95=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 41 ++++ pom.xml | 213 ++++++++++-------- .../logging/P6spySqlFormatConfiguration.java | 122 +++++----- src/main/resources/application.yml | 46 ++-- src/main/resources/config/conf-log.yml | 29 +-- 5 files changed, 256 insertions(+), 195 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c5c24c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,41 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### h2db ### +*.mv.db +*exchange*.xml +pack.xml +*.pid + +logs/ diff --git a/pom.xml b/pom.xml index 243215f..4cbf588 100644 --- a/pom.xml +++ b/pom.xml @@ -37,100 +37,7 @@ WEB-INF/classes/**/ens/modules/example/**, - - - local - - true - - - local - - ${package.include.base} - WEB-INF/classes/**/ens/batch/**, - WEB-INF/classes/**/ens/modules/**, - - WEB-INF/classes/application-local*.yml, - - - - - - - prod-iup - - prod-iup - - ${package.include.base} - WEB-INF/classes/**/ens/modules/**, - - - - WEB-INF/classes/**/ens/biz/iup/**, - WEB-INF/classes/application-prod-iup.yml, - WEB-INF/classes/mybatis-mapper/**, - - - ${package.exclude.base} - WEB-INF/classes/**/ens/batch/**/EnsScheduler.class, - WEB-INF/classes/**/ens/biz/iup/_legacy/**, - WEB-INF/classes/**/ens/biz/iup/alimtalk/**, - - - - - prod-traffic-cc - - prod-traffic - - ${package.include.base} - WEB-INF/classes/**/ens/batch/**/EnsKkoMydocScheduler.class, - WEB-INF/classes/**/ens/modules/common/**, - WEB-INF/classes/**/ens/modules/kkomydoc/**, - WEB-INF/classes/**/ens/biz/traffic/chuncheon/**, - WEB-INF/classes/application-prod-traffic.yml, - - - ${package.exclude.base} - WEB-INF/classes/**/ens/modules/common/ctgy/intgrnnoti/**, - - - - - - src/main/resources - - mybatis-mapper/** - - - - - - - prod-traffic-cheonan - - prod-traffic-cheonan - - ${package.include.base} - WEB-INF/classes/**/ens/batch/**/EnsIntgrnScheduler.class, - WEB-INF/classes/**/ens/batch/**/EnsKkoMydocScheduler.class, - WEB-INF/classes/**/ens/batch/**/EnsKtGibisScheduler.class, - WEB-INF/classes/**/ens/modules/**, - - - - - WEB-INF/classes/**/ens/biz/traffic/cheonan/**, - WEB-INF/classes/application-prod-traffic-cheonan.yml, - WEB-INF/classes/mybatis-mapper/**, - - - ${package.exclude.base} - - ojdbc6 - - - + org.springframework.boot @@ -379,6 +286,7 @@ + com.mysema.maven apt-maven-plugin @@ -390,14 +298,31 @@ process - target/generated-sources/java - + ${project.build.directory}/generated-sources/java com.querydsl.apt.jpa.JPAAnnotationProcessor - + + org.codehaus.mojo + build-helper-maven-plugin + + + add-source + generate-sources + + add-source + + + + ${project.build.directory}/generated-sources/java + + + + + + org.apache.maven.plugins @@ -448,4 +373,98 @@ + + + local + + true + + + local + + ${package.include.base} + WEB-INF/classes/**/ens/batch/**, + WEB-INF/classes/**/ens/modules/**, + + WEB-INF/classes/application-local*.yml, + + + + + + + prod-iup + + prod-iup + + ${package.include.base} + WEB-INF/classes/**/ens/modules/**, + + + + WEB-INF/classes/**/ens/biz/iup/**, + WEB-INF/classes/application-prod-iup.yml, + WEB-INF/classes/mybatis-mapper/**, + + + ${package.exclude.base} + WEB-INF/classes/**/ens/batch/**/EnsScheduler.class, + WEB-INF/classes/**/ens/biz/iup/_legacy/**, + WEB-INF/classes/**/ens/biz/iup/alimtalk/**, + + + + + prod-traffic-cc + + prod-traffic + + ${package.include.base} + WEB-INF/classes/**/ens/batch/**/EnsKkoMydocScheduler.class, + WEB-INF/classes/**/ens/modules/common/**, + WEB-INF/classes/**/ens/modules/kkomydoc/**, + WEB-INF/classes/**/ens/biz/traffic/chuncheon/**, + WEB-INF/classes/application-prod-traffic.yml, + + + ${package.exclude.base} + WEB-INF/classes/**/ens/modules/common/ctgy/intgrnnoti/**, + + + + + + src/main/resources + + mybatis-mapper/** + + + + + + + prod-traffic-cheonan + + prod-traffic-cheonan + + ${package.include.base} + WEB-INF/classes/**/ens/batch/**/EnsIntgrnScheduler.class, + WEB-INF/classes/**/ens/batch/**/EnsKkoMydocScheduler.class, + WEB-INF/classes/**/ens/batch/**/EnsKtGibisScheduler.class, + WEB-INF/classes/**/ens/modules/**, + + + + + WEB-INF/classes/**/ens/biz/traffic/cheonan/**, + WEB-INF/classes/application-prod-traffic-cheonan.yml, + WEB-INF/classes/mybatis-mapper/**, + + + ${package.exclude.base} + + ojdbc6 + + + diff --git a/src/main/java/cokr/xit/ens/core/config/logging/P6spySqlFormatConfiguration.java b/src/main/java/cokr/xit/ens/core/config/logging/P6spySqlFormatConfiguration.java index 6056fd3..79feb06 100644 --- a/src/main/java/cokr/xit/ens/core/config/logging/P6spySqlFormatConfiguration.java +++ b/src/main/java/cokr/xit/ens/core/config/logging/P6spySqlFormatConfiguration.java @@ -1,72 +1,72 @@ package cokr.xit.ens.core.config.logging; +import java.util.Locale; + +import org.hibernate.engine.jdbc.internal.FormatStyle; + import com.p6spy.engine.logging.Category; import com.p6spy.engine.spy.appender.MessageFormattingStrategy; -import lombok.extern.slf4j.Slf4j; -import org.hibernate.engine.jdbc.internal.FormatStyle; -import java.util.Locale; -import java.util.Objects; -import java.util.Stack; +import lombok.extern.slf4j.Slf4j; @Slf4j public class P6spySqlFormatConfiguration implements MessageFormattingStrategy { - @Override - public String formatMessage(final int connectionId, final String now, final long elapsed, final String category, final String prepared, final String sql, final String url) { - Stack callStack = new Stack<>(); - StackTraceElement[] stackTrace = new Throwable().getStackTrace(); - - for (StackTraceElement stackTraceElement : stackTrace) { - String trace = stackTraceElement.toString(); - if (trace.startsWith("io.p6spy") && !trace.contains("P6spyPrettySqlFormatter")) { - callStack.push(trace); - } - } - - StringBuilder callStackBuilder = new StringBuilder(); - int order = 1; - while(callStack.size() != 0) { - callStackBuilder.append("\n\t\t").append(order++).append(". ").append(callStack.pop()); - } + private static final String NEW_LINE = "\n"; + private static final String TAP = "\t"; + private static final String CREATE = "create"; + private static final String ALTER = "alter"; + private static final String DROP = "drop"; + private static final String COMMENT = "comment"; - String message = new StringBuilder().append("\n\n\tConnection ID: ").append(connectionId) - .append("\n\tExecution Time: ").append(elapsed).append(" ms\n") - .append("\n\tCall Stack (number 1 is entry point): ").append(callStackBuilder).append("\n") - .toString(); - - return sqlFormat(sql, category, message); + @Override + public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) { + if (sql.trim().isEmpty()) { + return formatByCommand(category); + } return formatBySql(sql, category) + getAdditionalMessages(elapsed); } - - private String sqlFormat(String sql, String category, String message) { - if(sql.trim().isEmpty()) { - return ""; - } - - String sqlComment = null; - if(Objects.equals(Category.STATEMENT.getName(), category)) { - sql = sql.trim().toLowerCase(Locale.ROOT); - if(sql.startsWith("/*") && sql.contains("*/")){ - sqlComment = sql.substring(0, sql.indexOf("*/")+2); - sql = sql.substring(sqlComment.length()).trim(); - } - if(sql.startsWith("create") || sql.startsWith("alter") || sql.startsWith("comment") || sql.startsWith("drop")) { - sql = FormatStyle.DDL - .getFormatter() - .format(sql); - } - else { - sql = FormatStyle.BASIC - .getFormatter() - .format(sql); - } - } - - return new StringBuilder().append("\n") - .append("----------------------------------------------------------------------------------------------------") - .append(sqlComment == null ? "" : "\n" + sqlComment) - .append(sql) - .append(message) - .append("----------------------------------------------------------------------------------------------------") - .toString(); + private static String formatByCommand(String category) { + return NEW_LINE + + "Execute Command : " + + NEW_LINE + + NEW_LINE + + TAP + + category + + NEW_LINE + + NEW_LINE + + "----------------------------------------------------------------------------------------------------"; + } + private String formatBySql(String sql, String category) { + if (isStatementDDL(sql, category)) { + return NEW_LINE + + "Execute DDL : " + + NEW_LINE + + FormatStyle.DDL + .getFormatter() + .format(sql); + } return NEW_LINE + + "Execute DML : " + + NEW_LINE + + FormatStyle.BASIC + .getFormatter() + .format(sql); + } + private String getAdditionalMessages(long elapsed) { + return NEW_LINE + + NEW_LINE + + String.format("Execution Time: %s ms", elapsed) + + NEW_LINE + + "----------------------------------------------------------------------------------------------------"; + } + private boolean isStatementDDL(String sql, String category) { + return isStatement(category) && isDDL(sql.trim().toLowerCase(Locale.ROOT)); + } + private boolean isStatement(String category) { + return Category.STATEMENT.getName().equals(category); + } + private boolean isDDL(String lowerSql) { + return lowerSql.startsWith(CREATE) + || lowerSql.startsWith(ALTER) + || lowerSql.startsWith(DROP) + || lowerSql.startsWith(COMMENT); } -} \ No newline at end of file +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index bc58ebe..35a28e2 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,5 +1,5 @@ server: - + shutdown: graceful servlet: context-path: error: @@ -14,19 +14,19 @@ server: spring: profiles: -# active: local + # active: local active: '@spring.profiles.active@' -# include: -# prodDB -# group: -# local: -# - localDB -# prod: -# - prodDB + # include: + # prodDB + # group: + # local: + # - localDB + # prod: + # - prodDB config: import: classpath:config/conf-app.yml, classpath:config/conf-contract.yml, classpath:config/conf-log.yml, classpath:config/conf-mybatis.yml, classpath:config/conf-policy.yml, classpath:config/conf-slack.yml # main: -# allow-bean-definition-overriding: true + # allow-bean-definition-overriding: true pid: file: mdpost.pid @@ -49,7 +49,7 @@ spring: connection-test-query: SELECT 1 minimum-idle: 10 maximum-pool-size: 50 -# transaction-isolation: TRANSACTION_READ_UNCOMMITTED + # transaction-isolation: TRANSACTION_READ_UNCOMMITTED transaction-isolation: TRANSACTION_READ_COMMITTED pool-name: pool-xitworld data-source-properties: @@ -58,23 +58,23 @@ spring: jpa: database-platform: org.hibernate.dialect.MariaDB103Dialect - show-sql: true -# generate-ddl: true #TODO: 필요 시 주석해제. generate-ddl를 true 설정 시 ddl-auto의 설정값과 무관하게 DDL 스크립트 실행 + # show-sql: true + # generate-ddl: true #TODO: 필요 시 주석해제. generate-ddl를 true 설정 시 ddl-auto의 설정값과 무관하게 DDL 스크립트 실행 hibernate: ddl-auto: none naming: -# implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy + # implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy properties: hibernate: - format_sql: true - use_sql_comments: true + # format_sql: true + # use_sql_comments: true order_inserts: true order_updates: true jdbc: batch_size: 200 -# default_batch_fetch_size: ${chunkSize:100} -# dialect: org.hibernate.dialect.MySQL57InnoDBDialect + # default_batch_fetch_size: ${chunkSize:100} + # dialect: org.hibernate.dialect.MySQL57InnoDBDialect cache: @@ -82,11 +82,11 @@ spring: redis: host: ${server.host:localhost} port: 16379 -# password: -# database: -# timeout: -# sentinel: -# master: + # password: + # database: + # timeout: + # sentinel: + # master: data: redis: repositories: diff --git a/src/main/resources/config/conf-log.yml b/src/main/resources/config/conf-log.yml index 293da3e..fb5841b 100644 --- a/src/main/resources/config/conf-log.yml +++ b/src/main/resources/config/conf-log.yml @@ -4,8 +4,8 @@ decorator: enable-logging: true #p6spy logging 활성화 여부 multiline: true logging: slf4j -# custom-appender-class: com.p6spy.engine.spy.appender.Slf4JLogger -# log-format: cokr.xit.ens.core.config.support.P6spySqlFormatConfiguration + # custom-appender-class: com.p6spy.engine.spy.appender.Slf4JLogger + # log-format: cokr.xit.ens.core.config.support.P6spySqlFormatConfiguration tracing: include-parameter-values: true @@ -16,17 +16,18 @@ logging: level: root: info '[org.hibernate.type.descriptor.sql]': off #jpa "?" 부분(binding 파라미터) 출력 -# '[org.springframework.orm.jpa]': debug -# '[org.springframework.transaction]': debug -# '[org.springframework.transaction.interceptor]': trace -# '[org.springframework.jdbc.core]': off -# '[org.springframework.orm]': off -# '[org.springframework.orm.hibernate5]': off -# '[com.p6spy.engine.logging]': off -# '[com.p6spy]': off -# '[org.hibernate]': off -# '[org.hibernate.sql]': off -# '[org.hibernate.jpa]': off + # '[org.springframework.orm.jpa]': debug + # '[org.springframework.transaction]': debug + # '[org.springframework.transaction.interceptor]': trace + # '[org.springframework.jdbc.core]': off + # '[org.springframework.orm]': off + # '[org.springframework.orm.hibernate5]': off + # '[com.p6spy.engine.logging]': off + # '[com.p6spy]': off + # '[org.hibernate]': off + # '[org.hibernate.sql]': off + # '[org.hibernate.jpa]': off + '[org.mariadb.jdbc.internal.logging]': WARN exception-conversion-word: '%wEx' pattern: console: '%d{yyyy-MM-dd HH:mm:ss.SSS} %clr(${LOG_LEVEL_PATTERN:-%5p}){green} %clr([%18thread]){magenta} %clr(%-40.40logger{39}%line){cyan} %clr(: %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}){faint}' @@ -41,4 +42,4 @@ logging: # '[org.springframework]': debug # '[org.hibernate]': info # slack: -# webhook-uri: https://hooks.slack.com/services/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 \ No newline at end of file +# webhook-uri: https://hooks.slack.com/services/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