My new website on Artificial Intelligence and Machine Learning www.aimlfront.com

Log4j

With log4j it is possible to enable logging at runtime without modifying the application binary. The log4j package is designed so that these statements can remain in shipped code without incurring a heavy performance cost. Logging behavior can be controlled by editing a configuration file, without touching the application binary.

Before starting configuring Log4j, will see the log levels

LevelDescription
OFFThe highest possible rank and is intended to turn off logging.
FATALSevere errors that cause premature termination. Expect these to be immediately visible on a status console.
ERROROther runtime errors or unexpected conditions. Expect these to be immediately visible on a status console.
WARNUse of deprecated APIs, poor use of API, 'almost' errors, other runtime situations that are undesirable or unexpected, but not necessarily "wrong". Expect these to be immediately visible on a status console.
INFOInteresting runtime events (startup/shutdown). Expect these to be immediately visible on a console, so be conservative and keep to a minimum.
DEBUGDetailed information on the flow through the system. Expect these to be written to logs only.
TRACEMost detailed information. Expect these to be written to logs only. Since version 1.2.12.

There are three ways to configure log4j: with a properties file, with an XML file and through Java code. Within either you can define three main components: Loggers, Appenders and Layouts. Configuring logging via a file has the advantage of turning logging on or off without modifying the application that uses log4j. The application can be allowed to run with logging off until there's a problem, for example, and then logging can be turned back on simply by modifying the configuration file.

Loggers are logical log file names. They are the names that are known to the Java application. Each logger is independently configurable as to what level of logging (FATAL, ERROR, etc.) it currently logs. In early versions of log4j, these were called category and priority, but now they're called logger and level, respectively.

The actual outputs are done by Appenders. There are numerous Appenders available, with descriptive names, such as FileAppender, ConsoleAppender, SocketAppender, SyslogAppender, NTEventLogAppender and even SMTPAppender. Multiple Appenders can be attached to any Logger, so it's possible to log the same information to multiple outputs; for example to a file locally and to a socket listener on another computer.Appenders use Layouts to format log entries. A popular way to format one-line-at-a-time log files is PatternLayout, which uses a pattern string, much like the C / C++ function printf. There are also HTMLLayout and XMLLayout formatters for use when HTML or XML formats are more convenient, respectively.


TTCC

TTCC is a message format used by log4j.TTCC is an acronym for Time Thread Category Component. It uses the following pattern:

  %r [%t] %-5p %c %x - %m%n


MnemonicDescription
%rUsed to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event.
%tUsed to output the name of the thread that generated the logging event.
%pUsed to output the priority of the logging event.
%cUsed to output the category of the logging event.
%xUsed to output the NDC (nested diagnostic context) associated with the thread that generated the logging event.
%X{key}Used to output the MDC (mapped diagnostic context) associated with the thread that generated the logging event for specified key.
%mUsed to output the application supplied message associated with the logging event.
%nUsed to output the platform-specific newline character or characters.
%LLine number of message.

1.  Configure log4j: with a properties file (log4j.properties)
# Root logger option
log4j.rootLogger=INFO, stdout
	 
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


2.  Configure log4j: with a xml file (log4j.xml)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
 <log4j:configuration debug="true"
	xmlns:log4j='http://jakarta.apache.org/log4j/'>
 
	<appender name="console" class="org.apache.log4j.ConsoleAppender">
	    <layout class="org.apache.log4j.PatternLayout">
		<param name="ConversionPattern" 
		  value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
	    </layout>
	</appender>
 
	<root>
		<level value="INFO" />
		<appender-ref ref="console" />
	</root>
 
 </log4j:configuration>