我知道这个问题之前已经被问过,但我找不到适合我的干净的解决方案。一切都工作正常,直到我决定修改 pom.xml
这就是我所做的。
我修改了pom.xml中资源文件的目标目录。当我将项目构建为 jar 文件时,我希望资源文件与 jar (即: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>
这是我的 log4j.properties 内容。我想记录到日志文件并在终端中打印出来:
# 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
当我执行 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.
我在网上找到了一个解决方案,但在 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");
}
我应该做什么?
引用文献:
最佳答案
您可以定义系统属性-Dlog4j.configuration并设置配置文件的路径。您也可以使用-Dlog4j.debug进行调试。
例如(注意您应该使用绝对路径):
-Dlog4j.configuration=file:///C:/log4j.properties
。
关于java - Maven log4j - 找不到记录器的附加程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27250001/