Meera has posted 70 posts at DZone. You can read more from them at their website. View Full User Profile

Configuring Logging in JBoss

11.19.2008
| 158315 views |
  • submit to reddit

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.

Published at DZone with permission of its author, Meera Subbarao.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Sebastian Otaegui replied on Mon, 2008/11/24 - 5:06pm

Regardless the log4j configuration will remain the same between versions of jboss (and this extract of the bookis useful if you never worked with jboss before).

I find silly to publish a book about something that is not GA yet. I understand that money talks, but they could publish a book about 4.[23].x

 That would be a book I(and I believe lots of people) would buy.

Stefan Lecho replied on Mon, 2008/12/08 - 6:35am

When I want to do application logging, I would like to specify and control this in an application specific log4j.xml file. Adding this kind of logging to the general jboss-log4j.xml file can be cumbersome, because you don't have a complete deliverable, i.e. on top of deploying the deliverable (EAR, SAR, ...) the jboss-log4j.xml has to be modified. And this can be easily forgotten.

 Due to classloader issues, I've never successfully been able to configure this properly and  was "forced" to modiy the jboss-log4j.xml file. Any thoughts on a solution for this "problem" ?

Sebastian Otaegui replied on Mon, 2008/12/08 - 1:32pm

If I were you I'd post that exact same question in the jboss forums.

If you ask in a precise manner you will get your answer :)

Sebastian Otaegui replied on Mon, 2008/12/08 - 4:30pm

This may work

 

http://www.jboss.org/community/docs/DOC-11277

Irshad Ahmed replied on Tue, 2008/12/23 - 11:38am

This is nice stuff. How can I configure my application to log my messages to a file as well as console simultaneously. I tried this:

 <category name="org.jbia">

              <priority value="DEBUG"/>  

              <appender-ref ref="JBIA" />

              <appender-ref ref="CONSOLE" />

</category>

but this doesn't work, it only logs to the file but nothing appears on the console.

Meera Subbarao replied on Tue, 2008/12/23 - 5:42pm in response to: Irshad Ahmed

[quote=ibuchh]

but this doesn't work, it only logs to the file but nothing appears on the console. [/quote]

I haven't used JBoss recently, but remember that by default JBoss logs output to both the console and the server.log file. Please post in the JBoss forums, you might be able to get some help there.

Meera Subbarao

Dimitri Alexeev replied on Sat, 2009/03/07 - 12:10pm

I wonder why so many people still use <category> / <priority>  tags.

These are deprecated since 2002. 

Please stop copy/paste from each other, check the log4j javadoc, and use

<logger> / <level> tags instead.

jame jack replied on Mon, 2009/06/29 - 4:33pm

تحميل برامج برامج جوالات العاب بنات برامج تكنولوجيا كتب تعليم UltraSurf العاب برامج نت Internet Download Manager ProgDVB برامج مجانية أفضل المواقع العربية دليل مواقع مشاهدة محطات مشفرة Online TV Player 3.0.0.940 Internet Download Manager 5.17 Build 4 رقص شرقي anyTV Pro 4.32 OnLineLive 7.1.1 هزي يانواعم ProgDVB 6.06.2 SopCast 3.0.3 منتدى برامج نت Falco Image Studio 3.6 لعبة تزلج على الجليد UltraSurf 9.4 كاثرين هيغل Katherine Heigl محطة غنوة FreeZ Online TV 1.0 Free Video to Mp3 Converter 3.1.3.51 Advanced MP3 Converter 2.10 Xilisoft Video to Audio Converter 5.1.23.0515 Blaze Media Pro 8.02 AKRAM Media Creator 1.11 DVD Audio Extractor 4.5.4 Free WMA to MP3 Converter 1.16 لعبة نينجا المتقدم لعبة قذف كرة لعبة دراجات البهلوانية لعبة اعداء الغابة تحميل برامج Download DivX Subtitles 2.0 BullGuard 8.5 Google Chrome 2.0.181.1 Dev Dell Studio XPS Desktop 435T Intel Matrix Storage Manager A00 Gigabyte GA-EP45-UD3P Bios F9 Ambush HDConvertToX 1.1.229.1764 MSI Wind Nettop CS 120 Realtek Audio Driver 5.10.0.5618 Biostar T41-A7 6.x Realtek On-Board Audio Driver 5.10.0.5735 for 2000/2003/XP TweakNow RegCleaner 4.1.1 SpeedItup Free 4.97 برامج العاب - Internet Download Manager - برامج جوالات - العاب - محطة غنوة - قنوات فضائية - بنات - تكنولوجيا - كتب تعليم - UltraSurf - ق ذ -0

Gibu replied on Tue, 2010/06/01 - 6:22am

How can I get the Logger activity for User Login and Logout event in JBOSS Portal

Gibu replied on Tue, 2010/06/01 - 6:24am in response to: Gibu

I want to get the logger message for successfull and failed login attempt by a user

Harpreet Sardar replied on Thu, 2011/03/10 - 1:31pm

I am just getting to know java and start with jboss5.x and this article helped me alot.

I have some issue related to the DataSource not sure if i can post here or start a new thread.

DEPLOYMENTS MISSING DEPENDENCIES:
  Deployment "persistence.unit:unitName=#cisCommonPU" is missing the following dependencies:
    Dependency "jboss.jca:name=SybaseCssDS,service=DataSourceBinding" (should be in state "Create", but is actually in state "** NOT FOUND Depends on 'jboss.jca:name=SybaseCssDS,service=DataSourceBinding' **")
  Deployment "persistence.unit:unitName=#cisDomainPU" is missing the following dependencies:
    Dependency "jboss.jca:name=SybaseCssDS,service=DataSourceBinding" (should be in state "Create", but is actually in state "** NOT FOUND Depends on 'jboss.jca:name=SybaseCssDS,service=DataSourceBinding' **")

if you could give some insite/Help would appriciate it.

 

Thank you

Harpreet Sardar replied on Thu, 2011/03/10 - 1:32pm in response to: Gibu

I think, you can get that by adding a interceptor in struts2 or fileters which can capture the login details  and then log those. i dont think it is possible to get the logging events just by Log4j api....

Carla Brian replied on Sun, 2012/05/27 - 9:42am

Good thing I stumbled upon in this post. I am new to this. I dont have any idea about this one. - Dr Paul Perito

Mazhar Muh replied on Mon, 2014/09/15 - 1:17am

Thanks for the stuff, please fix this issue 

Deployment "persistence.unit:unitName=#cisCommonPU" is missing the following dependencies:

I think harpreet issues are most common one, there is a minor bug he is missing.

Check smart cities summit here.



Jaavar Singh replied on Wed, 2014/09/17 - 8:17am

 Hey thats great stuffs, this has fixed my issues. Thanks for sharing with us. Know more about Cloud billing as well.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.