Thursday, June 16, 2011

How to configure JBoss's log4j to log messages separately for different categories

Target audience: Beginners
Version: JBoss 4.2.x

We all know how to configure JBoss to log messages (DEBUG, INFO, WARN and ERROR) to a file. Let’s look at how to configure JBoss to log different categories to different files.

The configuration file can be found in [JBOSS]\server\default\conf\jboss-log4j.xml. Lets open the file. You will be able to see a default appender configure slimier to bellow

The above configuration to log the messages to a file ${jboss.server.log.dir}/server.log . And , we’ll sat that we have two categories are defined for logging the messages ${jboss.server.log.dir}/server.log

Currently all logs will be logged to "${jboss.server.log.dir}/server.log as we have configured in “FILE” appender.

Now we will configure the log messages of category “java.sql” to be logged in a different log file than the one already configured(“FILE”). Lest create another file appender configuration. We’ll name the appender as “FILE_SQL” and configure to log to ${jboss.server.log.dir}/server_sql.log

We have created the file appender and now we need to attach the category of which log messages should be logged to this file. The bellow configuration would do that.

After doing the above configuration,if you start the JBoss server and run your application, then all messages related to category “java.sql” will be logged in ${jboss.server.log.dir}/server_sql.log file.

Hope this note would be helpful for you.

No comments:

Post a Comment