Dec 10

In the past developers have used log4j, and Java’s built in logging framework api. Commons logging (JCL) has also been used as a facade to allow easy implementation switching.

Now we also have logback, and the slf4j facade.

So the question is what should we be using to implement logging in our Java application?

Firstly using a facade is recommended. It allows you to far more easily switch the underlying logging implementation without affecting your application code. JCL suffered from a variety of classloader issues that made it troublesome to use. SLF4J has none of these issues and works well as a logging facade and is currently our facade of choice.

For the logging implementation we have long used log4j and it does a good job of providing simple logging capabilities. Our preferred logging framework for new projects however is logback. It includes many improvements over log4j such as:

  • faster
  • more flexible configurations
  • filters
  • implements SLF4J natively

written by objects \\ tags: , , , , , ,

Jul 23

Log4j comes with an appender that will send an email when a logging event occurs. The appender class is and the following gives an example of its configuration

<appender name="LogEmail" class="">
  <param name="BufferSize" value="512" />
  <param name="SMTPHost" value="" />
  <param name="From" value="" />
  <param name="To" value="" />
  <param name="Subject" value="Log from myapp" />
  <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern"
      value="%p %t %c - %m%n" />

written by objects \\ tags: , ,

May 21

If you do not want to store your log4j configuration in your classes directory then you need to tell log4j where it can find it.

One possibility is to configure it in your applicationContext.xml as shown here

<bean id="log4jInitialization"
   <property name="targetClass"
      value="org.springframework.util.Log4jConfigurer" />
   <property name="targetMethod" value="initLogging" />
   <property name="arguments">

If you are running your web application as an expanded war then another option is to use a listener in your web.xml. This will only work on an unexpanded war.


written by objects \\ tags: