Lets talk today on Log4J. Log4j is a fast and reliable, open source popular logging API framework written in Java.

Simply speaking it is a mechanism where as a developer you are able to indicate the state of the system during runtime. Logs generated during runtime helps you to perform required analysis indicating the state of the system during runtime.

It is always advised to implement logging activity during your program development. There are multiple ways of implementing logging activity.

Today I will tell you a very simple way of implementing the same.

Firstly you should know that in order to implement log4j, java provides you the logging package. The logging package consists of set of classes and interface which are used in logging. The system uses logger object to log messages.

Hence as a first step you should have the required jar files implemented in you IDE.

You the below dependency file in your pom.xml to configure the required log4J

<!– https://mvnrepository.com/artifact/log4j/log4j –>

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>1.2.17</version>

</dependency>

The next step you should first create a properties file and name it log4j.properties(log4j.properties file name is used for easy readability)

Now you should add the below code snippet in your log4j.properties file

log4j.rootLogger=INFO, Appender

log4j.appender.Appender=org.apache.log4j.FileAppender

log4j.appender.Appender.File=src/test/resources/logs/app_${current.date}_log.txt

log4j.appender.Appender.layout=org.apache.log4j.PatternLayout

log4j.appender.Appender.layout.ConversionPattern=%-7p %d [%t] %c %x – %m%n

log4j.appender.Appender.Append=false

log4j.appender.file.maxFileSize=5000KB

log4j.appender.file.maxBackupIndex=3

During the runtime, logs folder will be automatically created and your logs will be automatically generated.

Use the below logger and add to your required code base

Logger log= Logger.getLogger(<class name>.class.getName());

 

For example while in one of my selenium project I used in my base class.

public static Logger log= Logger.getLogger(TestBase.class.getName());

After this simply use

  1. info(“Your message here”) or
  2. error(“Your error message here)

// FileInputStream is a java library that accepts a constructor and reading the file Config.properties from the said location

try {

fis= new FileInputStream(“.\\src\\test\\resources\\properties\\Config.properties”);

log.info(“Reading the config file through FileInput Stream fis”);

} catch (FileNotFoundException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

log.error(“FileInputStream fis could not read the config file”);

}

try {

config.load(fis);

log.info(“config file loaded successfully”);

 

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

log.error(“Config file not loaded”);

}

For more details, visit https://logging.apache.org/log4j/2.x/ in details.

Leave Comment

Your email address will not be published. Required fields are marked *

//www.bitsenze.com/wp-content/uploads/2020/01/BS-Infoedgelogo-final-update.png