OpenJPA logging with slf4j on WebSphere

こ雲淡風輕ζ 提交于 2019-12-12 10:55:36

问题


My Java EE 6 application uses slf4j with logback as logging framework. Now I want to add the SQL traces of OpenJpa to my log files. The OpenJpa-Documentation says, I can use a parameter to this:

<property name="openjpa.Log" value="slf4j"/>

I use the WebSphere Application Server v8.0.0.1 as Java EE container. If I deploy my application to the server, this parameter does not change anything. I can change log levels in WebSphere Admin Console and this works fine. But a cannot bypass the OpenJpa logging to my slf4j framework.

Does anyone uses such configuration and solved the problem?

Btw. I know, that the InformationCenter-Article http://publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=%2Fcom.ibm.websphere.nd.doc%2Finfo%2Fae%2Fae%2Ftejb_jpatroubleshoot.html says, that the parameter will be ignored, but everything should be possible, eh?


回答1:


I don't know what version of OpenJPA is embedded in WAS 8.0.

In OpenJPA 1.x there was no possibility to use "slf4j". A workaround was copying the class org.apache.openjpa.lib.log.SLF4JLogFactory from OpenJPA 2.x sources to your application and using it directly:

<property name="openjpa.Log" value="org.apache.openjpa.lib.log.SLF4JLogFactory"/>

You can always specify the factory class name directly, the short name is only a convenience trick.

In OpenJPA 2.x the SLF4JLogFactory is present, so it should work with your current settings; perhaps you set it in a place that is overridden by other configuration? For example, if you configure JPA through EntityManagerFactory's propertyMap, it takes precedence over the settings in persistence.xml.




回答2:


I solved it.

There are four things to do:

  1. Say that OpenJpa logs the statements. That is done by the property entry in the persistence.xml.
  2. Say Slf4j that it has to fetch the logs from JUL too. This can be done by installing the SLF4JBridgeHandler by SLF4J. I installed the bridge via a ServletContextListener which is called at application start.
  3. Configure Logback that it does not log ALL logs from JUL but only the needed Logs. This is done by adding the LevelChangePropagator in the logBack-test.xml
  4. Ask the WebSphere TraceService to hand down the logs from OpenJPA to the application. This is a setting in the WebSphere Administration Console


来源:https://stackoverflow.com/questions/8703105/openjpa-logging-with-slf4j-on-websphere

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!