Damon Ng
Damon Ng

Reputation: 129

Log4j Could Not Read Configuration File SELENIUM JAVA

How do I get log4j to read up a properties file.

I'm writing a Java for testing with selenium which I want to use log4j. As i encounter an error of Log4j could not read configuration file [ERROR] Ignoring configuration file . Kindly advise , Thanks you . In my main method if have this:

 static Logger log = Logger.getLogger(Testing.class);

Log4j.properties:

log4j.rootCategory=DEBUG, R  

# File   
log4j.appender.R=org.apache.log4j.RollingFileAppender   
log4j.appender.R.File=D:/log4j.log   

# Control the maximum log file size   
log4j.appender.R.MaxFileSize=100KB   

# Archive log files (one backup file here)   
log4j.appender.R.MaxBackupIndex=1 

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

log4j.appender.R.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n

My Testcase :

@Test  //Tested Login 
      public void TestLogin_Success() throws Exception {
          try{ 
              //PropertyConfigurator.configure("log4j.properties");
              driver = new FirefoxDriver();
              LoginBuilder.Execute(driver);  
              log.info("TEST A TEST");
              driver.quit();
          }catch (Exception e){

              //Log.error(e.getMessage());
              throw (e);
          }

      }

Upvotes: 0

Views: 3010

Answers (2)

Naman
Naman

Reputation: 32046

You can put the log4j.properties file under the resources folder corresponding to the java folder of your class file.

Also to configure it using configurator you can use the following code :

import org.apache.log4j.PropertyConfigurator;
....your base class
....inside the main/setup method
PropertyConfigurator.configure(Testing.class.getClassLoader().getResource("log4j.properties"));

To make sure if the logger is working or not, you can try and log the same details to the console window and see the differences. Just add these changes and observe :

log4j.rootCategory=DEBUG, console, R

log4j.appender.console =org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p -> %m %n 

Or in your case ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n

Upvotes: 2

user861594
user861594

Reputation: 5908

Your log4j.properties expected to be in class path. You also can set it via system property

log4j.configuration

. Below is example for ant target

<sysproperty key="log4j.configuration" value="file:///${lib.dir}/log4j.properties" />

Upvotes: 1

Related Questions