What are Log files?
Log files are check points that can be added at any step of code.Logs gives us detailed reasoning of any step failure .To see logs we first need to download Log4j jar file.
Steps to Configure Log4j in Selenium
1-Go to http://www.java2s.com/Code/Jar/a/Downloadapachelogginglog4jjar.htm
2-Download apache-logging/apache-logging-log4j.jar.zip( 435 k) and extract Jar File and Store it
3-Go to Selenium>Right Click of Project Name>Click on Properties>Select Java Build Path>Go to Libraries>Add External jar and open and select the Log4j jar from the location it was saved
4- Open a notepad, Copy and Paste the code below and save the filename as Log4j.Properties with double quotes("") on it on the Project Location
// Here we have defined root logger
log4j.rootLogger=INFO,CONSOLE,R,HTML,TTCC
// Here we define the appender
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.TTCC=org.apache.log4j.RollingFileAppender
log4j.appender.HTML=org.apache.log4j.FileAppender
// Here we define log file location
log4j.appender.R.File=./log/testlog.log
log4j.appender.TTCC.File=./log/testlog1.log
log4j.appender.HTML.File=./log/application.html
// Here we define the layout and pattern
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= %5p [%t] (%F:%L)- %m%n
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c -%p - %m%n
log4j.appender.TTCC.layout=org.apache.log4j.TTCCLayout
log4j.appender.TTCC.layout.DateFormat=ISO8601
log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout
log4j.appender.HTML.layout.Title=Application log
log4j.appender.HTML.layout.LocationInfo=true
Properties file is configured on the Project Location |
The below code will generate Log info after each Steps of code execution
Notes
1-An object "L" of Logger Class has been created to generate Log Statements
2-Class name has been passed to the getLogger method because it is easy to trace the logs in case of Multiple Classes being used in a Project. See output >NotePad file a class Name is mentioned in the Log
3-Properties file of Logs has been configured in with the use of PropertyConfigurator.configure("Log4j.Properties") code
2-Class name has been passed to the getLogger method because it is easy to trace the logs in case of Multiple Classes being used in a Project. See output >NotePad file a class Name is mentioned in the Log
3-Properties file of Logs has been configured in with the use of PropertyConfigurator.configure("Log4j.Properties") code
0 comments:
Post a Comment