在junit单元期间自定义log4j2和spring boot的日志路径

时间:2020-02-26 16:50:58

标签: spring-boot junit log4j2

我正在将Spring Boot 2.2.4与log4j2一起使用。

我试图将通用的log4j2.xml文件用于主要代码和junit测试。

对于此文件,我为日志路径添加了一个变量:

<Configuration
    <Properties
        <Property name="filename">$${sys:LOGS_PATH}/api-client.log</Property>
    </Properties>
    .
    .
</Configuration>

当我使用VM参数(-DLOGS_PATH = target)执行junit测试时,没有错误。测试正常,并且在预期的文件夹中创建了日志。

但是当我尝试使用@SpringBootTest(properties = "LOGS_PATH=target")添加此参数时 我有一个例外:

2020-02-26 17:33:35,898 main ERROR FileManager (${sys:LOGS_PATH}/api-client.log) java.io.IOException: La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte java.io.IOException: La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte
    at java.io.WinNTFileSystem.canonicalize0(Native Method)
    at java.io.WinNTFileSystem.canonicalize(WinNTFileSystem.java:428)
    at java.io.File.getCanonicalPath(File.java:618)
    at java.io.File.getCanonicalFile(File.java:643)
    at org.apache.logging.log4j.core.util.FileUtils.makeParentDirs(FileUtils.java:142)
    at org.apache.logging.log4j.core.appender.FileManager$FileManagerFactory.createManager(FileManager.java:429)
    at org.apache.logging.log4j.core.appender.FileManager$FileManagerFactory.createManager(FileManager.java:417)
    at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:113)
    at org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:114)
    at org.apache.logging.log4j.core.appender.FileManager.getFileManager(FileManager.java:182)
    at org.apache.logging.log4j.core.appender.FileAppender$Builder.build(FileAppender.java:96)
    at org.apache.logging.log4j.core.appender.FileAppender$Builder.build(FileAppender.java:52)
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:122)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1002)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:942)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:934)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:552)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:241)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:288)
    at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:579)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:651)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:668)
    at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:253)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:153)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
    at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
    at org.apache.commons.logging.LogAdapter$Log4jLog.<clinit>(LogAdapter.java:155)
    at org.apache.commons.logging.LogAdapter$Log4jAdapter.createLog(LogAdapter.java:122)
    at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:89)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:59)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<clinit>(SpringJUnit4ClassRunner.java:99)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

2020-02-26 17:33:35,905 main ERROR Could not create plugin of type class org.apache.logging.log4j.core.appender.FileAppender for element File: java.lang.IllegalStateException: ManagerFactory [org.apache.logging.log4j.core.appender.FileManager$FileManagerFactory@4de5031f] unable to create manager for [${sys:LOGS_PATH}/flx-api-client.log] with data [org.apache.logging.log4j.core.appender.FileManager$FactoryData@67e2d983] java.lang.IllegalStateException: ManagerFactory [org.apache.logging.log4j.core.appender.FileManager$FileManagerFactory@4de5031f] unable to create manager for [${sys:LOGS_PATH}/flx-api-client.log] with data [org.apache.logging.log4j.core.appender.FileManager$FactoryData@67e2d983]
    at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:115)
    at org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:114)
    at org.apache.logging.log4j.core.appender.FileManager.getFileManager(FileManager.java:182)
    at org.apache.logging.log4j.core.appender.FileAppender$Builder.build(FileAppender.java:96)

请问一些想法?

1 个答案:

答案 0 :(得分:0)

我不认为Spring会将SpringBootTest中声明的属性保存为系统属性。如果包含Log4j Spring Cloud Config Client jar,则应该能够指定

<Configuration
    <Properties
        <Property name="filename">$${spring:LOGS_PATH}/api-client.log</Property>
    </Properties>
    .
    .
</Configuration>

Log4j将从Spring环境中获取LOGS_PATH值。