1

I've created a maven project and under src -> test -> java, I created package learnlog4j and under that package, I've test classes.

I've created log4j.properties under root of the project, which has following content -

log4j.rootLogger=INFO, stdout

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{HH:mm:ss:SSS} %p [%C{1}] %m%n

But when I run following test class,

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

    @BeforeClass
    public void beforeClass(){
        logger.info("IN Before Class");
    }

    @Test
    public void test1(){
        logger.info("IN Test");
    }

    @AfterClass
    public void afterClass(){
        logger.info("IN After Class");
    }
}

it gives error -

log4j:WARN No appenders could be found for logger (learnlog4j.Log4jExample1).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

I searched over stackoverflow but that did not help. Could you please tell me if log4j.properties is at correct location? Or let me know if something that needs to be corrected.

Alpha
  • 13,320
  • 27
  • 96
  • 163

1 Answers1

2

You cann add a

BasicConfigurator.configure();

in beforeClass()-Method to get a basic Console adapter.

To use your log4j.properties, use

PropertyConfigurator.configure("log4j.properties");

See No appenders could be found for logger(log4j)? for details.

ma501
  • 155
  • 7
  • THis does not read information from `log4j.properties` file. It logs as per default settings. – Alpha Dec 06 '17 at 07:35
  • Yes, you are right. Sorry. I was mislead by the fact that your log4j.properties have configured a console adapter. I have updated my answer above. – ma501 Dec 07 '17 at 10:05