Saturday, 4 March 2017

How to implement Log in Selenium (Using Log4j)



-> During the Automation Execution users need some information about the Execution steps in console.

-> We need information which helps the users to understand the testcase or any failure during the testcase execution.

-> With the help of Log4j its possible to enable logging in selenium testcase.


Follow the below steps to implement logs


1) Log4j Download


Download Log4j by using this link -> https://logging.apache.org/log4j/1.2/download.html

2) Create Log4j.properties file

-> Log4j.properties file for display log message only in console

# Root logger option
log4j.rootLogger=INFO,stdout

# Redirect log messages to console
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


->Log4j.propertiesfiles for display log message only in file

# Root logger option
log4j.rootLogger=INFO,file

# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./Log/log_${current.date}.log
log4j.appender.fileout.Append=false
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.Append=false
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


->Log4j.propertiesfiles for display log message in both console and File

# Root logger option
log4j.rootLogger=INFO,stdout, file

# Redirect log messages to console
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

# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./Log/log_${current.date}.html
log4j.appender.fileout.Append=false
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.Append=false
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


-> Load properties file in Main()


Initial Environment variable (Current.date) which is used in Log4j.properties file and load Log4j.properties file.

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh_mm_ss");
        System.setProperty("current.date", dateFormat.format(new Date()));
       
           PropertyConfigurator.configure("C:/Selenium/log4j.properties");
          


-> create log for required class files



public class Login {
   
    final static Logger logger = Logger.getLogger(Login.class);

public static boolean LoginGmail(){

logger.info("Information Message");
logger.debug("Warning Message");
logger.error("Error Message");
}

}


-> Best practise for use logger

logger.info = Instead of Sysout we can use looger.info
logger.debug = use inside of conditional Statments and looping statments.
logger.error =  use inside catch block.


-> LoGGER Level

See the link for logger level ->  http://arunrajvdm.blogspot.in/2016/12/log4j-how-to-disable-all-log-details.html


1 comment:

  1. Votre blog est une source précieuse d'informations, merci pour vos publications régulières. Je recommande vivement de passer un Test CPS pour ceux qui souhaitent affiner leurs compétences en matière de clic, que ce soit pour les jeux ou la productivité.

    ReplyDelete