1

I know this question has been asked before but I couldn't find a clean solution for mine. Everything was working fine until I decided to modify the pom.xml

This is what I did.

I modified the target directory for the resources file in pom.xml. When I build my project into jar files, I want the resources file to be in the same directory as the jar (ie: RootProjDir/target) instead of being in RootProjDir/target/classes

<resources>
      <resource>
          <directory>src/main/resources</directory>
          <includes>
              <include>directories.properties</include>
              <include>webconfig.properties</include>
              <include>log4j.properties</include>
          </includes>
          <targetPath>${project.build.directory}</targetPath>
      </resource>
  </resources>

This is my log4j.properties content. I want to log to the log file and print out in the terminal :

# output log to console and logfile 
log4j.rootLogger=DEBUG, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/Users/nuttynibbles/Documents/workspace/javaWorkspace/RootProjDir/rootproj.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Direct log messages to 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{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

When I execute the jar file, I'm getting the following error:

java -jar RootProjDir.jar 
log4j:WARN No appenders could be found for logger (org.Main).
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 found a solution online but it doesn't seems like a clean solution to set the log4j.properties file directory in main():

public static void main(String[] args) {
// This will tell log4j to read the log4j.properties in RootProDir/target
PropertyConfigurator.configure(System.getProperty("user.dir") + "/log4j.properties"); 

}

What should I do?

References:

  1. Maven: how to place resource file together with jar?
  2. No appenders could be found for logger(log4j)?
Community
  • 1
  • 1
chrizonline
  • 4,779
  • 17
  • 62
  • 102
  • This seems to be duplicate of http://stackoverflow.com/questions/12532339/no-appenders-could-be-found-for-loggerlog4j – Manish Mudgal Dec 02 '14 at 13:02
  • Yes the problem is the same, but the solution isn't. When I use BasicConfigurator.configure(), it only logs to terminal, not into file. I want to log both file and terminal. And if you read my question carefully, I didn't want to use the PropertyConfigurator. Is there other ways to resolve it? – chrizonline Dec 02 '14 at 13:04

1 Answers1

3

You can define system property -Dlog4j.configuration and set path to your configuration file. Also you can use -Dlog4j.debug for debugging.

For example (note you should use absolute path): -Dlog4j.configuration=file:///C:/log4j.properties.

Pavel Parshin
  • 507
  • 4
  • 15
  • I believe you meant to use it this way while running it in terminal? `java -jar -Dlog4j.configuration=log4j.properties RootProjDir.jar` it didn't work either – chrizonline Dec 02 '14 at 13:39
  • You should use absolute path to file. For example, `-Dlog4j.configuration=file://C:/log4j.properties`. If you can't use absolute path, that configuration file must be in classpath. – Pavel Parshin Dec 02 '14 at 14:18
  • Forgot one slash in path. Check updated answer. Also try debug mode to see what happening. – Pavel Parshin Dec 02 '14 at 14:35
  • using an absolute path is crap. What is the solution for an relative path? – Lonzak May 18 '16 at 14:21