Este contenido no está disponible en el idioma seleccionado.
5.2.3. Add Logging to an Application with JBoss Logging
To log messages from your application you create a Logger object (
org.jboss.logging.Logger
) and call the appropriate methods of that object. This task describes the steps required to add support for this to your application.
Prerequisites
You must meet the following conditions before continuing with this task:
- If you are using Maven as your build system, the project must already be configured to include the JBoss Maven Repository. Refer to Section 2.3.2, “Configure the JBoss EAP 6 Maven Repository Using the Maven Settings”
- The JBoss Logging JAR files must be in the build path for your application. How you do this depends on whether you build your application using Red Hat JBoss Developer Studio or with Maven.
- When building using Red Hat JBoss Developer Studio this can be done selecting Project -> Properties from the Red Hat JBoss Developer Studio menu, selecting Targeted Runtimes and ensuring the runtime for JBoss EAP 6 is checked.
- When building using Maven this can be done by adding the following dependency configuration to your project's
pom.xml
file.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
You do not need to include the JARs in your built application because JBoss EAP 6 provides them to deployed applications.
Once your project is setup correctly. You need to follow the following steps for each class that you want to add logging to:
Add imports
Add the import statements for the JBoss Logging class namespaces that you will be using. At a minimum you will need to importimport org.jboss.logging.Logger
.import org.jboss.logging.Logger;
import org.jboss.logging.Logger;
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a Logger object
Create an instance oforg.jboss.logging.Logger
and initialize it by calling the static methodLogger.getLogger(Class)
. Red Hat recommends creating this as a single instance variable for each class.private static final Logger LOGGER = Logger.getLogger(HelloWorld.class);
private static final Logger LOGGER = Logger.getLogger(HelloWorld.class);
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add logging messages
Add calls to the methods of theLogger
object to your code where you want it to send log messages. TheLogger
object has many different methods with different parameters for different types of messages. The easiest to use are:debug(Object message)
info(Object message)
error(Object message)
trace(Object message)
fatal(Object message)
These methods send a log message with the corresponding log level and themessage
parameter as a string.LOGGER.error("Configuration file not found.");
LOGGER.error("Configuration file not found.");
Copy to Clipboard Copied! Toggle word wrap Toggle overflow For the complete list of JBoss Logging methods refer to theorg.jboss.logging
package in the JBoss EAP 6 API Documentation.
Example 5.1. Using JBoss Logging when opening a properties file
This example shows an extract of code from a class that loads customized configuration for an application from a properties file. If the specified file is not found, a ERROR level log message is recorded.