3

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.

juherr
  • 5,640
  • 1
  • 21
  • 63
  • Are you sure the log4j property file is also on the classpath? – uniknow Apr 30 '16 at 06:13
  • I have added log4j jar file in java build path as an external jar file. – Sandy QA Proffessional Apr 30 '16 at 06:17
  • Have you searched other answers? Some solutions: [properly ensure its in class path through Eclipse](http://stackoverflow.com/a/25163361/2173960), [pass config file as parameter when running program](http://stackoverflow.com/a/11318951/2173960). – Vineet Apr 30 '16 at 06:20

2 Answers2

3

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).

uniknow
  • 938
  • 6
  • 5
  • Thanks a lot for posting. Been struggling with this for an hour and this was the only solution that provided any logging (checked several SO posts + the provided log4j link) - it's an old project so wasn't really interested pulling it apart to see what the exact problem was. – Daniel Apr 03 '17 at 09:05
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