为JpaTransactionManager启用日志记录

时间:2012-04-16 10:54:48

标签: spring hibernate spring-data

我在Spring 3.5容器中使用JpaTransactionManager和hibernate 3。我无法为JPA启用日志记录。我希望看到用于调试我的一些服务的事务管理日志。我正在使用log4j。

以下是我在log4j.properties

中的内容
# For JPA
log4j.logger.org.springframework.orm.jpa=DEBUG
log4j.logger.org.springframework.orm.jpa.JpaTransactionManager=DEBUG
log4j.logger.org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter=DEBUG
log4j.logger.org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean=DEBUG
log4j.logger.org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor=DEBUG
log4j.logger.org.springframework.transaction=DEBUG
log4j.logger.org.springframework=DEBUG
log4j.logger.org.hibernate=DEBUG
log4j.logger.org.hibernate.SQL=DEBUG

# Redirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\apps\application.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=5
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Root logger option
log4j.rootLogger=ALL, file

2 个答案:

答案 0 :(得分:3)

交易记录可以在log4j.properties中专门设置:

log4j.logger.org.springframework.orm.jpa.JpaTransactionManager=DEBUG

此背景下感兴趣的其他日志:

log4j.logger.org.hibernate.transaction=DEBUG
log4j.logger.org.springframework.transaction=DEBUG

答案 1 :(得分:0)

在启用JPATransactionManager的日志记录方面,这对我有用: 1.下载logback jar(logback-classic-1.0.1.jar,logback-core-1.0.1.jar)并放入classpath。由于我使用的是SLF4J,我还有log4j-over-slf4j-1.6.4.jar和slf4j-api-1.6.4.jar。 2.使用以下logback.xml(在WEB-INF / classes中)进行配置:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>
            %d %p [%c] - &lt;%m&gt;%n
        </pattern>
    </encoder>
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>DEBUG</level>
    </filter>
</appender>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${catalina.home}/logs/application.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${catalina.home}/logs/application-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>20MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>

    <encoder>
        <pattern>
            %d %p [%c] - &lt;%m&gt;%n
        </pattern>
    </encoder>
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>ALL</level>
    </filter>
</appender>

<logger name="org.hibernate.transaction.JDBCTransaction" level="DEBUG"/>
<logger name="org.hibernate.jdbc.ConnectionManager" level="DEBUG"/>
<logger name="org.springframework.orm.jpa.JpaTransactionManager" level="DEBUG"/>

<root level="ERROR">
    <appender-ref ref="FILE"/>
</root>
</configuration>