Only Institute
to provide recording daily after class to students. you can practice whole day under faculty guidance. where test and mockup tests are taken regularly. provides all mobile nos of old students on website.

Log4j Basic And Integration with Struts2

  • While developing Java/J2EE applications, for debugging an application that is to know the status of a java application at its execution time, in general we use System.out.println () statements in the application.
  • Generally SOP statements are printed on console, so there are temporary messages and whenever the console is closed then automatically the messages are removed from the console .It is not possible to store the SOPL messages in a permanent place and these are single threaded model, means these will prints only one by one message on the console screen
  • In order to overcome the problems of SOP statements Log4j came into picture, with Log4j we can store the flow details of our Java/J2EE in a file or databases
  • This is an Open Source tool given by Apache, for only java projects, to record or write the status of an application at various places.
  • Working with log4j is nothing but working with classes & interfaces given in org.apache.log4j.*
  • Log4j is a common tool, used for small to large scale Java/J2EE projects
  • In Log4j we use log statements rather SOP statements in the code to know the status of a project while it is executing
  • In real time, after a project is released and it is installed in a client location then we call the location as on-site right, when executing the program at on-site location, if we got any problems occurred then these problems must report to the off showered engineers, in this time we used to mail these Log files only so that they can check the problems easily

Components of log4j

There are three main components of logj –
1. Logger
2. Appender
3. Layout

• Logger is a class present in log4j jar .we need to create object of logger in each class where we have to write log statements instead of SOP’s.
• Appenders job is to write the messages or logs into external file or database .There are different Appenders such as FileAppender , ConsoleAppender , JDBCAppender etc.
• Layout – This specifies the format of log statements which are written by the Appenders.

Following are the different layout classes –
- SimpleLayout
- PatternLayout
- HTML Layout
- XML Layout

• How to write log statements –
1. Create Logger Object in your class
2. Use below methods of Logger to put messages
- debug() -----Lowest Priority
- info()
- warn()
- error()
- fatal() ------Highest priority

Create Simple java Application of Log4j

Files required –
1 .
2. log4j.propertile
3. jar –log4j.jar

log-1 –

# - # is comment in properties file
# Root logger option

# Redirect log messages to console
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file, support file rolling.
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

• Meaning of symbols in conversion path
1. %d{yyyy-MM-dd HH:mm:ss} = Date and time format, refer to
SimpleDateFormat JavaDoc.
2. %-5p = The logging priority, like DEBUG or ERROR. The -5 is
optional, for the pretty print format.
3. %c{1} = The logging name we set via getLogger(), refer to
log4j PatternLayout guide.
4. %L = The line number from where the logging request.
5. %m%n = The message to log and line break. –

package com.arjun;

import org.apache.log4j.Logger;

public class Test {
	public static void main(String[] args) {
		Logger logger = Logger.getLogger(Test.class); //logger class
		System.out.println("in a file");
		logger.error("divide by zero...");"excepion is occured...");
		logger.warn("please handle eception..");
		int i = 10/0;
		}catch(Exception e){
			logger.error("Exception occured "+e);


log file -

Struts2 project + log4j Integration

1. Put log4j.jar in your Project –


2. Put file in src folder –


3. Create Logger Object in your classes as –

Log file –