Log4J: Warn No Appenders could be found for Logger

2019-07-14 05:06发布

问题:

Below is my log4j.properties file content, file is placed with the src folder in eclipse.

#Application Logs
log4j.rootlogger=INFO, logg
log4j.appender.logg=org.apache.log4j.RollingFileAppender
log4j.appender.logg.File=D:\\SandhyaFiles\\SeleniumWorkspace\\InterviewProject\\Logs\\Testlogs.log
log4j.appender.logg.layout=org.apache.log4j.PatternLayout


log4j.appender.logg.layout.ConversionPattern=%d -%c -%p - %m%n
log4j.appender.logg.maxFileSize=5MB
log4j.appender.logg.maxBackupIndex=3

Inside Library package i have initialised and used logj as below:

public class Library
{

  public static final Logger Log = Logger.getLogger(Library.class);

  public void initialized(){

  Log.info("Inside initialise")
}}

calling initialize from testcase throws log4j warning:

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

Please Help me fix this.

回答1:

It seems like log4j is not able to find the log4j properties file.

One could specify the location of the log4j.properties file explicitly via the log4j.configuration system property.

-Dlog4j.configuration=file:mylogging.properties

In case the system property log4j.configuration is not defined, then the resource is set to its default value log4j.properties and looked for in the classpath of the project (at root).



回答2:

After so many trials, weirdly re arranging the properties file to this everything is going no warning and could get the logs.

# Root logger option
log4j.rootLogger=INFO, logg

log4j.appender.logg=org.apache.log4j.RollingFileAppender
log4j.appender.logg.File=.\\Logs\\Testlogs.log
log4j.appender.logg.MaxFileSize=5MB
log4j.appender.logg.MaxBackupIndex=3
log4j.appender.logg.layout=org.apache.log4j.PatternLayout
log4j.appender.logg.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

I wonder why this is so? if somebody can explain it will be great help. Thanks