2011-05-05 117 views
7

我們怎樣才能記錄spring事務機制。我顯示下面的例子Spring Doc sec 10.5.2如果我想記錄到這個級別如何做到這一點春季休眠事務日誌

我使用的是Spring,Hibernate和Log4j。

<!-- the Spring container is starting up... --> 
[AspectJInvocationContextExposingAdvisorAutoProxyCreator] - Creating implicit proxy 
for bean 'fooService' with 0 common interceptors and 1 specific interceptors 
<!-- the DefaultFooService is actually proxied --> 
[JdkDynamicAopProxy] - Creating JDK dynamic proxy for [x.y.service.DefaultFooService] 
<!-- ... the insertFoo(..) method is now being invoked on the proxy --> 
[TransactionInterceptor] - Getting transaction for x.y.service.FooService.insertFoo 
<!-- the transactional advice kicks in here... --> 
[DataSourceTransactionManager] - Creating new transaction with name [x.y.service.FooService.insertFoo] 
[DataSourceTransactionManager] - Acquired Connection 
[[email protected]] for JDBC transaction 
<!-- the insertFoo(..) method from DefaultFooService throws an exception... --> 
[RuleBasedTransactionAttribute] - Applying rules to determine whether transaction should 
rollback on java.lang.UnsupportedOperationException 
[TransactionInterceptor] - Invoking rollback for transaction on x.y.service.FooService.insertFoo 
due to throwable [java.lang.UnsupportedOperationException] 

<!-- and the transaction is rolled back (by default, RuntimeException instances cause rollback) --> 
[DataSourceTransactionManager] - Rolling back JDBC transaction on Connection 
[[email protected]] 
[DataSourceTransactionManager] - Releasing JDBC Connection after transaction 
[DataSourceUtils] - Returning JDBC Connection to DataSource 

Exception in thread "main" java.lang.UnsupportedOperationException 
at x.y.service.DefaultFooService.insertFoo(DefaultFooService.java:14) 
<!-- AOP infrastructure stack trace elements removed for clarity --> 
at $Proxy0.insertFoo(Unknown Source) 

記錄文件..

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 
log4j.rootLogger=info, stdout 
log4j.category.org.springframework.transactions=DEBUG 

回答

9

Spring Reference中有a section about Logging

它顯示瞭如何配置不同的日誌框架,其中log4j

在你的情況下,配置的最後一行是:

log4j.logger.org.springframework.transaction=DEBUG 
+0

@Lucas感謝..我已經把我的Log4j屬性文件above.can你建議soem的變化,這可以幫助我使用Spring登錄我的j2ee應用程序,hibernate – Vish 2011-05-06 05:19:23

+0

我們是否需要一些使用log4j.category的配置,因爲這不適用於我的應用程序。 :( – Vish 2011-05-06 05:28:52

+0

@Vish我從Spring文檔中複製了它,但是無法在log4j文檔中找到它。現在更改爲'logger' – 2011-05-06 07:31:38

8

如果你只願意設置的Spring的事務支持的日誌級別,嘗試添加下面的記錄到你的log4j.xml:

<logger name="org.springframework.transaction"> 
     <level value="DEBUG" /> 
</logger>