SeamFramework.orgCommunity Documentation

Chapter 19. CDI extensions available as part of Weld

19.1. Weld Logger

Important

These modules are usable on any JSR-299 implementation, not just Weld!

Adding logging to your application is now even easier with simple injection of a logger object into any CDI bean. Simply annotate a org.jboss.weld.log.Log type member with the @Logger qualifier annotation and an appropriate logger object will be injected into any instance of the bean.

import org.jboss.weld.annotation.Logger;

import org.jboss.weld.log.Log;
public class Checkout {
   private @Inject @Logger Log log;
   public void invoiceItems() {
      ShoppingCart cart;
      ...
      log.debug("Items invoiced for {0}", cart);
   }
}

The example shows how objects can be interpolated into a message. This interpolation is done using java.text.MessageFormat, so see the JavaDoc for that class for more details. In this case, the ShoppingCart should have implemented the toString() method to produce a human readable value that is meaningful in messages. Normally, this call would have involved evaluating cart.toString() with String concatenation to produce a single String argument. Thus it was necessary to surround the call with an if-statement using the condition log.isDebugEnabled() to avoid the expensive String concatenation if the message was not actually going to be used. However, when using @Logger-injected logging, the conditional test can be left out since the object arguments are not evaluated unless the message is going to be logged.