Jun 04 2009

Spring logging config

Published by Gilles under J2EE, Java, Spring

spring25.png

Spring uses Apache commons-logging as a bridge between different logging implementations.
This way you can change from a logging framework to any other supported loggin mechanism at any time.

The org.springframework.beans.factory.config.CommonsLogFactoryBean will create a "logger bean" and his type will depends on your configuration.

Example for the implementation:

In your application context, add a CommonsLogFactoryBean and give it a logName.

XML:
  1. <bean id="logger" class="org.springframework.beans.factory.config.CommonsLogFactoryBean">
  2.  
  3. <property name="logName" value="log"/></bean>

In your web., add the org.springframework.web.util.Log4jConfigListener listener before the
org.springframework.web.context.ContextLoaderListener

XML:
  1. <context-param>
  2. <param-name>log4jConfigLocation</param-name>
  3. <param-value>WEB-INF/.properties</param-value>
  4. </context-param>
  5. <listener>
  6. <listener->org.springframework.web.util.Log4jConfigListener</listener->
  7. </listener>

Here, my log4j config is locatedin the WEB-INF/.properties and here is its content:

XML:
  1. log4j.rootCategory=DEBUG, CONSOLE# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
  2. log4j.appender.CONSOLE=org.apache..ConsoleAppender
  3. log4j.appender.CONSOLE.Threshold=DEBUG
  4. log4j.appender.CONSOLE.layout=org.apache..PatternLayout
  5. log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n
  6. log4j.category.net.sf.=DEBUG
  7. log4j.category.org.springframework =DEBUG

To make use of your logger in your other beans, add a member variable of type org.apache.commons.logging.Log
and configure to inject the reference to the logger bean. For my part, I choose to do it through annotations because I don't want to have to make this reference in all my beans...
So, in your application context, add the

XML:
  1. <context:annotation-config/>

tag to enable the annotations config.In the targets beans, annotate the member variable as follow:

JAVA:
  1. @Autowired @Qualifier("logger")
  2. private final Log logger = null;

No responses yet

Apr 26 2009

Hibernate statistics on Tomcat with JMX

Published by Gilles under Hibernate, J2EE, Java

This little snippet will show you how to get Statistics JMX MBean available.

Inside the tomcat startup script (bin/tomcatXw.exe), ensure that the following _OPTS are set:

JAVA_OPTS="
-Dcom.sun.management.jmxremote.port=9002
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.awt.headless=true"

In your config, add:

XML:
  1. <bean id="jmxExporter" class="org.springframework.jmx.export.MBeanExporter"></bean>
  2.  
  3. <property name="beans">
  4. <map>
  5.     <entry key=":name=statistics">
  6.      <ref local="statisticsBean"/>
  7. </entry>
  8. </map>
  9. </property>
  10. <bean id="statisticsBean" class="org..jmx.StatisticsService"/>
  11. <property name="statisticsEnabled"><value>true</value></property>
  12. <property name="sessionFactory"><ref local="sessionFactory"></ref></property>

and then set the .generate_statistics property:

XML:
  1. <prop key=".generate_statistics">true</prop>

When you browse the MBeans (using JConsole.exe - which lives in the bin dir of your JDK 1.5 distribution), you should see , if you double click on that node, you should see the Statistics Bean.

No responses yet