Configuring Logging in JBoss

DZone 's Guide to

Configuring Logging in JBoss

Learn how to properly configure logs in JBoss.

· Integration Zone ·
Free Resource

The JBoss application server uses Log4J, which is an open-source logging framework, to do logging. The Log4J configuration file is located at server/xxx/conf/jboss-log4j.xml.

By default, two appenders are defined, one for the console which is set to log entries identified as of type info or higher priority, such as warning and error entries, and one for the server/xxx/log/server.log file which is set to log items identified as debug or higher priority. In addition, various category settings define the trace level for various packages to limit the amount of logging accomplished.

Some of the logging configuration changes that you might want to make include:
•    Specifying a rolling log file.
•    Limiting the amount of logging produced.
•    Adding logging for your application.
•    Defining a new log file.

Each of these topics is covered in the following text.
Rolling the server log file
The server.log file is created new each time the server is launched, and grows until the server is stopped, or until midnight. This behavior, while appropriate for a development environment, is not optimal for a production environment. In production you should specify a rolling log file, which when it reaches a certain size creates a new log file. Listing 1 shows how you can change the appender for the server.log file to create at most 20 log files of 10 megabytes (MB) in size each. All of the changes are highlighted.

Listing 1 Defining a rolling log appender

<log4j:...> <appender name="FILE"  class="org.jboss.logging.appender.RollingFileAppender">  #1 <errorHandler .../> <param name="File" value="${jboss.server.log.dir}/server.log"/> #2 <param name="Append" value="true"/>       #3 <param name="MaxFileSize" value="10MB"/>       #4 <param name="MaxBackupIndex" value="20"/>         #5 <layout .../> </appender> ...</log4j>

#1: Changed to use the rolling appender
#2: Location of log file
#3: Append to existing file on startup
#4: Limit log file size to 10MB
#5: Keep only the last 20 log files

We did not change the errorHandler or layout settings from the default. By the way, the various appenders defined in the org.jboss.logging.appender package are simple subclasses of the Log4J appenders defined in the org.apache.log4j package that automatically create the server/xxx/log directory.
The system property jboss.server.log.dir defines the location of the log file (#2).

Limiting logging

If the server log file grows too rapidly, or you want to suppress messages displayed on the console log, you can change the logging options to reduce the amount of logging.
As an example, assume that your application uses Hibernate. You might find that the log file quickly grows in size, reaching way over 100 MB within minutes. Looking at the log file you see many log entries that look those in listing 2.

Listing 2 Too much logging

2007-... DEBUG [org.hibernate.transaction.JTATransaction] commit...
2007-... DEBUG [org.hibernate.jdbc.JDBCContext] successfully ...
2007-... DEBUG [org.hibernate.impl.SessionImpl] opened session ...
2007-... DEBUG [org.hibernate.transaction.JTATransaction] Looking...

You can easily prevent the log from containing these entries by editing the jboss-log4j.xml file. As shown in listing 3, you can prevent the log from containing these DEBUG entries by adding a new category entry to the jboss-log4j.xml file and setting the priority to INFO (#2).

Listing 3 Shrinking the log file

<log4j:configuration ...> . . . <category name="org.hibernate"> #1  <priority value="INFO"/>       #2 </category> . . .</log4j:configuration


#1: Identifies the package to log
#2: Supresses DEBUG log entries

The category name (#1) comes from the name between brackets in the log file. Note that in the log entries presented, this name begins with org.hibernate. If you, instead, wanted to remove just the transaction-related logging output, use a category name of org.hibernate.transaction.
You can add similar category entries to remove other output from the log, or to reduce the level of logging performed by one of the services. For example, to set logging from JBoss Messaging to the warning level or higher, add the following categories to the jboss-log4j.xml file as shown in listing 4.
Listing 4 Limiting messaging logging to warning or higher.

<log4j:...> ... <category name="org.jboss.messaging"> <priority value="WARN"/> </category> <category name="org.jboss.jms"> <priority value="WARN"/> </category></log4j>

Note that JBoss Messaging uses two primary packages, which means we had to declare two categories. How did we know this? By looking at the class names for the entries that appeared in the server log. While the server log shows the full package and class name, the console log shows only the class name. Therefore you should always use the names in the server log to determine the logging category names.

Logging your application

If you use Log4J or Apache Jakarta Commons Logging within your application, you can log your application by adding category entries to the jboss-log4j.xml file. For example, if your application uses the package name org.jbia, you can log the classes in that package and its subpackages by adding a category to the jboss-log4j.xml file, as shown in listing 5.

Listing 5 Defining logging for your application

<log4j:...> ...<category name="org.jbia"> <priority value="DEBUG"/> </category></log4j>


The debug, and higher, messages show up in the server.log file and the info, and higher, messages show up in the console log.

Defining a specific log

In one way the server.log file is great in that all of the log information is in a single location. But it has the downside in that at times searching for data within is akin to finding a needle in a haystack. There are times when you would like to log specific messages to a particular file. For example, suppose you want to log all info messages from all classes in the org.jbia package to a file named jbia.log. Listing 6 shows the entries you need to add to the jboss-log4j.xml file to accomplish this task:

Listing 6 Creating a log file specific to an application

<log4j:...> ...<appender name="JBIA" ...>   #1 ... <param name="File" value="${jboss.server.log.dir}/jbia.log"/> #2 ... </appender> ... <category name="org.jbia"> #3 <priority value="DEBUG"/>   #4 <appender-ref ref="JBIA" />  #5 </category></log4j>

#1: Identifies the appender
#2: The log file name
#3: The class to log
#4: Log at the debug level
#5: Log only to the JBIA appender

We give the appender a unique name, JBIA in this example (#1), so that we can reference it later. The File parameter identifies the log file name (#1). We specify the category (#3) and priority (#4) to identify what we want to log, and then we reference the appender (#5) that we named earlier (#1). We did not provide the entire appender configuration as it is the same as for the FILE appender that already appears in the jboss-log4j.xml file, except with the name attribute is set to JBIA instead of FILE.

The above examples are just some of the things you can accomplish with changing the logging configuration. To find out everything that you could do, refer to the Log4J documentation, or obtain a book on Log4J.

 This article is based on a chapter from JBoss in Action: Configuring the JBoss Application Server by Javid Jamae and Peter Johnson. For more information, visit www.manning.com/jamae/.

It is being reproduced here by permission from Manning Publications.

Manning early access books and ebooks are sold exclusively through Manning.

java ,jboss ,log4j ,loggging

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}