SeamFramework.orgCommunity Documentation

Capitolo 17. Estensioni JSR-299 disponibili come parte di Web Beans

17.1. Web Beans Logger

Importante

Questi moduli sono utilizzabili su qualsiasi implementazione JSR-299, non solo Web Beans!

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

public class Checkout {

    import org.jboss.webbeans.annotation.Logger;
    import org.jboss.webbeans.log.Log;
    
    @Logger
    private Log log;
    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.