一尘不染

为JpaTransactionManager启用日志记录

hibernate

我在Spring
3.5容器中将JpaTransactionManager与hibernate3一起使用。我无法为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

阅读 331

收藏
2020-06-20

共1个答案

一尘不染

在启用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>
2020-06-20