Blame view

src/main/java/daeucna/config/p6spy/P6SpyFomatter.java 1.48 KB
2034b5b1   함상기   Init Version 2024...
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
  package daeucna.config.p6spy;

  

  import java.util.Locale;

  

  import org.hibernate.engine.jdbc.internal.FormatStyle;

  import org.springframework.context.annotation.Configuration;

  

  import com.p6spy.engine.logging.Category;

  import com.p6spy.engine.spy.P6SpyOptions;

  import com.p6spy.engine.spy.appender.MessageFormattingStrategy;

  

  import jakarta.annotation.PostConstruct;

  

  @Configuration

  public class P6SpyFomatter implements MessageFormattingStrategy {

  

      @PostConstruct

      public void setLogMessageFormat() {

          P6SpyOptions.getActiveInstance().setLogMessageFormat(this.getClass().getName());

      }

  

      @Override

      public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {

          sql = formatSql(category, sql);

          return String.format("[%s] | %d ms | %s", category, elapsed, formatSql(category, sql));

      }

  

      private String formatSql(String category, String sql) {

          if (sql != null && !sql.trim().isEmpty() && Category.STATEMENT.getName().equals(category)) {

              String trimmedSQL = sql.trim().toLowerCase(Locale.ROOT);

              if (trimmedSQL.startsWith("create") || trimmedSQL.startsWith("alter") || trimmedSQL.startsWith("comment")) {

                  sql = FormatStyle.DDL.getFormatter().format(sql);

              } else {

                  sql = FormatStyle.BASIC.getFormatter().format(sql);

              }

              return sql;

          }

          return sql;

      }

  }