SeamFramework.orgCommunity Documentation

Chapter 6. Logging

Weld Extensions integrates JBoss Logging 3 as it's logging framework of choice. JBoss Logging 3 is a modern logging framework offering:

Note

A number of the features of JBoss Logging 3 are still under development - at the moment only runtime generation of typed is supported, and these loggers only support the default message placed on the typed logger, and will not look up a localized message.

To use a typed logger, first create the logger definition:

@MessageLogger

interface TrainSpotterLog {
   // Define log call with message, using printf-style interpolation of parameters
   @LogMessage @Message("Spotted %s diesel trains") 
   void dieselTrainsSpotted(int number);
}

You can then inject the typed logger with no further configuration:

   // Use the train spotter log, with the log category "trains"

   @Inject @Category("trains") TrainSpotterLog log;

and use it:

log.dieselTrainsSpotted(7);

JBoss Logging will use the default locale unless overridden:

   // Use the train spotter log, with the log category "trains", and select the UK locale

   @Inject @Category("trains") @Locale("en_GB") TrainSpotterLog log;

You can also log exceptions:

@MessageLogger

interface TrainSpotterLog {
   // Define log call with message, using printf-style interpolation of parameters
   // The exception parameter will be logged as an exception
   @LogMessage @Message("Failed to spot train %s") 
   void missedTrain(String trainNumber,@Cause Exception exception);
}

You can then log a message with an exception:

log.missedTrain("RH1", cause);

You can also inject a "plain old" Logger:

   @Inject Logger log;

Typed loggers also provide internationalization support, simply add the @MessageBundle annotation to the logger interface (not currently supported).

Sometimes you need to access the message directly (for example to localize an exception message). Weld Extensions let's you inject a typed message bundle. First, declare the message bundle:

@MessageBundle

interface TrainMessages {
   // Define a message using printf-style interpolation of parameters
   @Message("No trains spotted due to %s") 
   String noTrainsSpotted(String cause);
}

Inject it:

@Inject @MessageBundle TrainMessages messages;

And use it:

   throw new BadDayException(messages.noTrainsSpotted("leaves on the line"));