Friday, March 23, 2012

Logging in Selenium Webdriver using log4j



Simple four steps to use log4j for logging


Step 1 Add jar file “log4j-1.2.16.jar”
Step 2 Add “log4j.properties” to class path as shown in picture below:
























Code for log4j.properties
# Log levels
# Uncomment the following line to enable full loggin for every class
#log4j.rootLogger=trace, stdout, R
log4j.rootLogger=info, stdout, R

# Console appender configuration
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

# Rolling File Appender
log4j.appender.R=org.apache.log4j.RollingFileAppender
# Path and file name to store the log file.
log4j.appender.R.File=./logs/applog.log
log4j.appender.R.MaxFileSize=500KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
# Rolling File Appender layout
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c - %p - %m%n


Step 3 import the log4j.logger
















Step 4 usage in code


Note: Copy the log4j.properties in the src location \workspace\ProjectLog4j\src\ log4j.properties .

2 comments:

  1. followed the above step.After that when i run my testng.xml file as a test suite,gets below error .
    log4j:WARN No appenders could be found for logger (Test.Class1).
    log4j:WARN Please initialize the log4j system properly.

    ReplyDelete
  2. this is warninig, not errror,so dont worry about that

    ReplyDelete