Log4J - Importance of logging In Java Applications

Logging can be defined as a process of storing information about events that occurred during program execution. There are different options to show/store log messages:


- show on console
- store in a file
- send to a remove monitor


Choosing the right option depends on the requirements and type of application.


Normally every java coder does logging even if he is not uing any logging framework. For instance, System.out.println statement is used to print informative message on the console. These messages can also contain the timestamp and other useful information which will make these messages interesting for the viewer and can help in error tracking and performance monitoring. But if you use a logging framework, then there is a big plus. The logging framework adds contextual information like line number, timestamp etc which prevents the developers from writing extra code. Result is better logging and less cost.


Importance of logging applications


Logging helps in debugging as well. Although debuggers are available but frankly it takes time to debug an application using a debugger. An application can be debugged more easily with few well-placed logging messages. So we can safely say that logging is a very good debugging tool. If logging is done sensibly and wisely, it can provide detailed context for application failures.


In distributed applications (e.g. web/remote applications), the logging is very important. The administrator can read logs to learn about the problems that occurred during some interval.


Java's built-in APIs provide logging options but they are not that flexible. Another option is to use Apache’s open source logging framework called log4j. 


Drawbacks of logging applications


There are some drawbacks of use logging in your application. For example: logging will


- pollute the code
- increase the size of the code
- reduce the speed


These are important points because, we ultimately want efficient applications. Log4J has the solution to this. You may turn on or turn off the logging at runtime by changing the configuration file. This means no change in the Java source code (binary).


Log4j


log4j is an open source project created by Apache and is part of Apache Logging Services Project. Currently Apache has 3 logging frameworks: 


- log4j for Java
- log4cxx for C++ 
- log4net for the Microsoft .NET framework


Apache also provides a tool called Chainsaw, which can be used for log analysis. If you are interested to learn about Chainsaw, visit the following link:
Apache Chainsaw -
API docs available at:
Logger (Apache Log4j 1.2.15 API)


The log4j package can be downloaded from 
Apache Logging Services Project - Apache log4j


Standard API vs log4j


A common question asked by Java developers is: 


Why we should use log4j logging framework when Java provides an API for logging (java.util.logging)?


Log4j has following advantages over standard logging API:
- log4j provides robust logging
- log4j has more features available than standard logging API
- configuring and using log4j is easier
- log4j also has a much more robust formatting system
- many add-on programs and handlers are available for log4j


Configuring log4j


Configuring log4j is very simple. You have to download the log4j-xxx.jar (xxx is the version no) file from the Apache logging services web site which is:
Apache Logging Services Project - Apache log4j


Currently there are 3 different versions available which are log4j 1.2, log4j 1.3 and log4j 2.0.


Once you have the jar file, you have to include that in your CLASSPATH. In Eclipse, you can simply import that jar file in your project. 


Categories of log messages


Before using log4j framework, one should be aware of different categories of log messages. Following are 5 categories:


DEBUG
The DEBUG Level is used to indicate events that are useful to debug an application. Handling method for DEBUG level is: debug().


INFO
INFO level is used to highlight the progress of the application. Handling method for INFO level is: info().


WARN
The WARN level is used to indicate potentially harmful situations. Handling method for WARN level is: warn().


ERROR
The ERROR level shows errors messages that might not be serious enough and allow the application to continue. Handling method for ERROR level is: error().


FATAL
The Fatal level is used to indicate severe events that will may cause abortion of the application. Handling method for FATAL level is: fatal().


If you declare log level as debug in the configuration file, then all the other log messages will also be recorded. 
If you declare log level as info in the configuration file, then info, warn, error and fatal log messages will be recorded.
If you declare log level as warn in the configuration file, then warn, error and fatal log messages will be recorded.
If you declare log level as error in the configuration file, then error and fatal log messages will be recorded.
If you declare log level as fatal in the configuration file, then only fatal log messages will be recorded.


Main Components


There are 3 main components that are used to log messages based upon type and level. These components also control the formatting and report place at runtime. These components are:


- loggers
- appenders
- layouts




log4j.properties file


log4j.properties file is a configuration file (not in XML format). If you have a stand alone application, then log4j.properties should be in the directory where you issued the java command. In case of web application (JSP/Servlet), place log4j.properties at /WEB-INF/classes/.


A sample properties file is given below:


log4j.appender.stdout=org.apache.log4j.ConsoleAppe nder
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.Patt ernLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{MMM dd HH:mm:ss}] %-5p (%F:%L) - %m%n
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.file=C:\\Log4J\\src\\tmp\\logs \\log.txt
log4j.appender.FILE.layout=org.apache.log4j.Patter nLayout
log4j.appender.FILE.layout.ConversionPattern=[%d{MMM dd HH:mm:ss}] %-5p (%F:%L) - %m%n
log4j.rootLogger=debug, FILE,stdout


We have used only two appenders (ConsoleAppender and FileAppender)in the example above. All the possible appender options are:


AppenderSkeleton, AsyncAppender, ConsoleAppender, DailyRollingFileAppender, ExternallyRolledFileAppender, FileAppender, JDBCAppender, JMSAppender, LF5Appender, NTEventLogAppender, NullAppender, RollingFileAppender, SMTPAppender, SocketAppender, SocketHubAppender, SyslogAppender, TelnetAppender, WriterAppender


We have used PatternLayout with both the appenders. All the possible options are:


DateLayout, HTMLLayout, PatternLayout, SimpleLayout, XMLLayout


So interesting thing is, you can generate log in HTML and in XML format as well.


If you use HTMLLayout or XMLayout, then you should not mention ConversionPattern. 


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email