Technical Stuff

Start from basics

Introduction and Configuration of Log4j

Introduction and Configuration of Log4j
Introduction and Configuration of Log4j
5 (100%) 1 vote

Concept :

The JBoss application server uses log4j. log4j is an open-source logging framework, to do logging in jboss.

Following are the six Logging Levels in log4j

  1. FATAL – Use the FATAL logging level events that indicate a critical service failure
  2. ERROR – Use the ERROR level priority for events that indicate a disruption in a request or the ability to service a request. A service should have some capacity to continue to service requests in the presence of ERRORs.
  3. WARN – Use the WARN level priority for events that may indicate a non-critical service error. Resumable errors, or minor breaches in request expectations fall into this category. The distinction between WARN and ERROR may be hard to discern and so its up to the developer to judge. The simplest criterion is would this failure result in a user support call. If it would use ERROR. If it would not use WARN.
  4. INFO – Use the INFO level priority for service life-cycle events and other crucial related information. Looking at the INFO messages for a given service category should tell you exactly what state the service is in.
  5. DEBUG – Use the DEBUG level priority for log messages that convey extra information regarding life-cycle events. Developer or in depth information required for support is the basis for this priority. The important point is that when the DEBUG level priority is enabled, the JBoss server log should not grow proportionally with the number of server requests. Looking at the DEBUG and INFO messages for a given service category should tell you exactly what state the service is in, as well as what server resources it is using: ports, interfaces, log files, etc.
  6. TRACE – Use TRACE the level priority for log messages that are directly associated with activity that corresponds requests. Further, such messages should not be submitted to a Logger unless the Logger category priority threshold indicates that the message will be rendered. The point of the TRACEpriority is to allow for deep probing of the JBoss server behavior when necessary. When the TRACE level priority is enabled, you can expect the number of messages in the JBoss server log to grow at least a x N, where N is the number of requests received by the server, a some constant. The server log may well grow as power of N depending on the request-handling layer being traced.

Configuration of log4j :

The Log4J configuration file is located is as follows:

Command  >> cat /jboss-eap-5.1/Jboss-as/server/<profile>/conf/jboss-log4j.xml.

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

  1. 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

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.

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

Listing 2 Shrinking the log file

<log4j:configuration …> . . . <category name=”org.hibernate”> <priority value=”INFO”/>  </category> . . .</log4j:configuration

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.xmlfile to accomplish this task:

Listing 3 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

Happy Learning !!!

If you have doubt or queries, you can definetely comment us or can mail us on [email protected]

If you have any recommedation for future blog, You can email us on [email protected]

Top Searches :

1.Introduction to Middleware
2. Introduction to Jboss
3. Installation of Jboss
4. Heap Dump
5. Thread Dump
6. VAPT – Disabling Apache Coyoto
7. VAPT – HTTP Methods

7 thoughts on “Introduction and Configuration of Log4j

Leave a Reply

%d bloggers like this: