简体   繁体   中英

Maven log4j - No appenders could be found for logger

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM