사용자 도구

사이트 도구


java:hibernate:log

문서의 이전 판입니다!


Hibernate Log 남기기

Hibernate는 Log4j 옵션 설정을 통해 SQL 실행 로그 등을 세세히 남기도록 조정할 수 있다.

  • hibernate 프라퍼티중 hibernate.show_sql=true 로 설정하면 로거용 로그와 hibernate가 System.out 으로 찍는 로그 두개씩 찍힌다. 따라서 로거를 사용할 때는 hibernate.show_sql=false 로 설정해야 한다.
  • hibernate.format_sql=true 로 하면 Log4j 로그를 찍을때 알아보기 편한 상태로 줄바꿈 해서 출력해준다.
  • org.hibernate.type 로그에는 한가지 문제가 있다, 클래스가 로딩되는 순간 Log level을 저장해 놓고, 그 이후로는 그 저장된 값을 사용한다는 것이다. 이 때문에 처음에는 Log4J 설정을 DEBUG 이상의 레벨로 실행하다가 프로그램 실행중에 동적으로 TRACE로 바꾼다고 해서 JDBC 파라미터가 로그로 남지는 않는다. 따라서 항상, 이 로그를 남기려면 Hibernate의 클래스들이 로딩되기 전에 Logger의 설정이 TRACE로 되어 있어야 한다.
  • statistics를 남기기 위해 hibernate.generate_statistics=true 로 지정하고 org.hibernate.stat Logger를 DEBUG로 지정한다.

logback

  • logback.groovy
    logger('org.hibernate.SQL', DEBUG) // SQL 로그
    logger('org.hibernate.type', TRACE) // 파라미터와 결과 로그
    logger('org.hibernate.stat', DEBUG) // statistics
    logger('org.hibernate.type.BasicTypeRegistry', WARN) // 과도한 로그 제외
  • logback.xml
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <!-- .... -->
        <logger name="org.hibernate.SQL" level="DEBUG" />
        <logger name="org.hibernate.tool.hbm2ddl" level="DEBUG"/>
        <logger name="org.hibernate.type" level="TRACE" />
        <logger name="org.hibernate.stat" level="DEBUG" />
        <logger name="org.hibernate.type.BasicTypeRegistry" level="WARN" />
    </configuration>

Log4j

log4j.logger.org.hibernate=info
#log4j.logger.org.hibernate=debug
# http://blog.naver.com/jdkim528/140027638234 에서 가져옴. -> 쿼리 파라미터는 잘못돼 있음
 
 
### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug
 
### log just the SQL
log4j.logger.org.hibernate.SQL=debug
 
### log JDBC bind parameters ###
log4j.logger.org.hibernate.type=TRACE   # 혹은 과거버전 log4j에서는 DEBUG
### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=info
 
### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug
 
### log cache activity ###
log4j.logger.org.hibernate.cache=info
 
### log transaction activity
#log4j.logger.org.hibernate.transaction=debug
 
### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug
 
### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace

디버깅시 가장 중요한 SQL로그와 파라미터/결과 로그는 log4j.logger.org.hibernate.SQL=debuglog4j.logger.org.hibernate.type=TRACE 이다.

Hibernate Log Categories

  • org.hibernate.SQL Log all SQL DML statements as they are executed
  • org.hibernate.type Log all JDBC parameters
  • org.hibernate.type.BasicTypeRegistry는 너무 많은 로그를 찍으므로 WARN 정도로 레벨을 올릴 것.
  • org.hibernate.tool.hbm2ddl Log all SQL DDL statements as they are executed
  • org.hibernate.pretty Log the state of all entities (max 20 entities) associated with the session at flush time
  • org.hibernate.cache Log all second-level cache activity
  • org.hibernate.transaction Log transaction related activity
  • org.hibernate.jdbc Log all JDBC resource acquisition
  • org.hibernate.hql.ast.AST Log HQL and SQL ASTs during query parsing
  • org.hibernate.secure Log all JAAS authorization requests
  • org.hibernate Log everything. This is a lot of information but it is useful for troubleshooting

SQL Formatter

java/hibernate/log.1524126147.txt.gz · 마지막으로 수정됨: 2018/04/19 16:52 저자 kwon37xi