728x90
개요
개념적인 내용은 다른 블로그를 참고 하시는게 좋을거 같습니다.
Mybatis에서 개발을 할 때 log4jdbc-log4j2-jdbc4.1 설정을 하여 쿼리문을 확인한다.
그러면
select * from temp where id = 1
이런식으로 출력이 된다.
하지만 JPA는 로그가 기본으로 설정 되어 있지만select * from temp where id = ? -> 그 뒤에 ? 관한 정보가 뜬다.
위와 같은 식으로 노출이 된다.
이런 방식은 local에서 개발 할 때 가독성이 현저히 떨어진다.
하나하나 대입 하거나 하는 정말 귀찮은 일이 발생한다.
그래서 이번 게시글은 p6spy-spring-boot-starter를 사용하여 Mybatis와 같이 노출 되게 설정 할려고 한다.
참고
- 작성자의 JPA Git의 들어와서 JPA에 공부한 내용을 확인해 주시면 감사하겠습니다.
- log4jdbc-log4j2-jdbc4.1 이걸로 설정 하는 사람도 있는거 같긴한데.. 중간중간 오류가 많이 뜬다( 추후 적용 방법을 찾으면 글을 올리겠습니다. )
- p6spy 공식 문서
설정!!
- Dependency 추가!!
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.8.0'
- application.properties 설정!
# 로그를 사용할지 말지 인데 개발 active에 따라 변경 하여 사용하면된다.
# default는 true 이다.
decorator.datasource.p6spy.enable-logging= true
- spy.properties 추가!
└─ src
├─ main
│ ├─ java
│ │ └─ com
│ │ └─ ...
│ └─ resources
│ └─ spy.properties // 이 위치에 추가한다.
# 작성!
# 필요한 내용은 참고 의 공식 문서에 가서 확인 하면좋다.
appender=com.p6spy.engine.spy.appender.StdoutLogger
# logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat # 기본 포맷을 사용할때
logMessageFormat= "MessageFormattingStrategy 상속 받은 파일 위치"
excludecategories=info,debug,result,resultset
여기 까지 작성 하더라도 화면에 출력이 된다. 하지만 Log Format 설정을 위해 다음 단계로 넘어간다!
- Config 추가!
@Configuration
public class P6spyConfig {
@PostConstruct
public void setLogMessageFormat() {
// P6spyFormat class는 생성 해야 한다!!
P6SpyOptions.getActiveInstance().setLogMessageFormat(P6spyFormat.class.getName());
}
}
- P6spyFormat.java 추가!
이름 아무거나 상관 없고 파일 위치도 어디든 상관 없다.
단. spy.properties에 현재 파일 위치를 작성해야 한다.
public class P6spyFormat implements MessageFormattingStrategy {
@Override
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared,
String sql, String url) {
sql = formatSql(category, sql);
return elapsed + "ms|" + sql;
}
private String formatSql(String category, String sql) {
if (sql == null || sql.trim().equals(""))
return sql;
if (Category.STATEMENT.getName().equals(category)) {
String tempSql = sql.trim().toLowerCase(Locale.ROOT);
if (tempSql.startsWith("create") || tempSql.startsWith("alter") || tempSql.startsWith("comment")) {
sql = FormatStyle.DDL.getFormatter().format(sql);
} else {
sql = FormatStyle.BASIC.getFormatter().format(sql);
}
}
return sql;
}
}
완성!!!!
위와 같이 코드를 적용 하면
3ms| select noticedto0_.id as id1_1_0_, noticedto0_.content as content2_1_0_, noticedto0_.reg_date as reg_date3_1_0_, noticedto0_.title as title4_1_0_ from notice noticedto0_ where noticedto0_.id=123 0ms|
와 같이 출력된다.
Format은 Custom 가능 하며 필요시 구글 검색이나 공식문서 시간이 괜찮다면 이것저것 테스트 하면서 본인에게 맞는 Log를 생성 하는 것이 좋다.
후기
다른 사람들이 사용한 글을 보면 무거워서인지, 느려져서 인지 실서버에서는 끄고 사용하란 글이 몇개 보았다.
이 부분은 각자 서버 사정 및 보안 환경에 맞게 설정 하면 될거 같다.
728x90
'BackEnd > Spring Boot' 카테고리의 다른 글
Bean Life Cycle (0) | 2023.04.16 |
---|---|
Redis 설정 (0) | 2023.04.16 |
채팅 만들기_2(chatting_room) (0) | 2023.04.16 |
채팅 만들기_1(basic) (0) | 2023.04.16 |
Request PathVariable Enum Converter (0) | 2023.04.16 |