-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: sql로그를 예쁘게 보기위해 p6spy 도입 (#23)
- Loading branch information
Showing
5 changed files
with
113 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package cotato.bookitlist.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class P6SpyConfig { | ||
|
||
@Bean | ||
public P6SpyEventListener p6SpyCustomEventListener() { | ||
return new P6SpyEventListener(); | ||
} | ||
|
||
@Bean | ||
public P6SpyFormatter p6SpyCustomFormatter() { | ||
return new P6SpyFormatter(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/cotato/bookitlist/config/P6SpyEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package cotato.bookitlist.config; | ||
|
||
import com.p6spy.engine.common.ConnectionInformation; | ||
import com.p6spy.engine.event.JdbcEventListener; | ||
import com.p6spy.engine.spy.P6SpyOptions; | ||
|
||
import java.sql.SQLException; | ||
|
||
public class P6SpyEventListener extends JdbcEventListener { | ||
|
||
@Override | ||
public void onAfterGetConnection(ConnectionInformation connectionInformation, SQLException e) { | ||
P6SpyOptions.getActiveInstance().setLogMessageFormat(P6SpyFormatter.class.getName()); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/cotato/bookitlist/config/P6SpyFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package cotato.bookitlist.config; | ||
|
||
import com.p6spy.engine.logging.Category; | ||
import com.p6spy.engine.spy.appender.MessageFormattingStrategy; | ||
import org.hibernate.engine.jdbc.internal.FormatStyle; | ||
|
||
import java.util.Locale; | ||
|
||
public class P6SpyFormatter implements MessageFormattingStrategy { | ||
|
||
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"; | ||
|
||
@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 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters