简体   繁体   English

Maven log4j-找不到记录器的附加程序

[英]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 一切正常,直到我决定修改pom.xml

This is what I did. 这就是我所做的。

I modified the target directory for the resources file in pom.xml. 我在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 当我将项目构建到jar文件中时,我希望资源文件与jar处于同一目录(ie: RootProjDir/target)而不是位于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. 这是我的log4j.properties内容。 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: 执行jar文件时,出现以下错误:

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(): 我在线上找到了一个解决方案,但是在main()中设置log4j.properties文件目录似乎不是一个干净的解决方案:

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? Maven:如何将资源文件与jar一起放置?
  2. No appenders could be found for logger(log4j)? 找不到用于logger(log4j)的附加程序?

You can define system property -Dlog4j.configuration and set path to your configuration file. 您可以定义系统属性-Dlog4j.configuration并设置配置文件的路径。 Also you can use -Dlog4j.debug for debugging. 您也可以使用-Dlog4j.debug进行调试。

For example (note you should use absolute path): -Dlog4j.configuration=file:///C:/log4j.properties . 例如(请注意,您应该使用绝对路径):- -Dlog4j.configuration=file:///C:/log4j.properties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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