0

I am trying to implement log4j logging for MyTest class. The package structure for the code is as described using the code under. Currently my log4j.properties is placed under the resources of trial package.

enter image description here

I do get the logs sometimes but at times the debugger displays "log4j:WARN No appenders could be found for logger." So I end up with :

  1. Where should I place the log4j.properties to get the logs consistently in MyTest?
  2. Can the same project have more than one log4j.properties in different packages?
  3. How is the log4j properties associated by debugger/compiler with a class calling it?

BasicPage.class

package com.trial.pages;
    public class BasicPage{
        protected static final Logger pageLogger = Logger.getLogger(BasicPage.class);
        <some code goes here>
    }

FirstPage.class

package com.trial.pages.mobile;
import package com.trial.pages;
public class FirstPage extends BasicPage {
     public void pageMethod() {
         pageLogger.info("These are logs from the Pages.");
     }
}

BasicTest.class

package com.core.data;
public class BasicTest{
    protected static final Logger testLogger = Logger.getLogger(BasicTest.class);
    <some code goes here>
}

MyTest.class

package com.trial.tests.mobile;
import com.trial.pages.mobile.FirstPage;
import com.core.data.BasicTest;
public class MyTest extends BasicTest{
    public void someMethod(){
        testLogger.info("These are the logs from the test activities.");
        new FirstPage.pageMethod();
    }
}
Naman
  • 27,789
  • 26
  • 218
  • 353

1 Answers1

1
  1. Where should I place the log4j.properties to get the logs consistently in MyTest?

Generally just put a log4j.xml file into src/main/resources and src/test/resources and let log4j find it by itself: no code required, the default log4j initialisation will pick it up.

2.Can the same project have more than one log4j.properties in different packages?

I would not do that because the classloader only takes the first which is found. You can have multiple configurations for different loggers in your single property file.

log4j-properties-file-multiple-loggers-in-same-class

how-can-i-create-2-separate-log-files-with-one-log4j-config-file

log4j-multiple-loggers-levels-and-appenders


EDIT1:

could you help me with the info on when and how is the log4j.properties moved to target/classes [propertyconfigurator looks inside it]

The log4j.property file will move automaticly when you build your project. But the file should be in src/main/resource/ for the automaticly move.

If this is the case, you dont need to configure the property configurator programmatically. Log4j is usable directly.

Community
  • 1
  • 1
Patrick
  • 12,336
  • 15
  • 73
  • 115
  • the log4j.properties is already in the resources folder for the `trial` package, still there is inconsistency to when during execution would this file be found and when not. – Naman Nov 25 '15 at 12:03
  • @nullpointer why it should be an a package? For a global access of the whole application just put it in the src/main/resources folder. Or Iam wrong? – Patrick Nov 25 '15 at 12:28
  • have tried doing that too.. could you help me with the info on when and how is the log4j.properties moved to target/classes [propertyconfigurator looks inside it] – Naman Nov 25 '15 at 18:37
  • Thank you :) ... figured it out..some maven parameters were to be changed to get the resources in the target/classes of the `core` package ` ../trial/src/main/resources ` After which the property configurator.configure could figure out the exact location of the resources. Could be specific though to project with more than one module. – Naman Nov 26 '15 at 09:09