Настройте путь журналов для log4j2 и весеннюю загрузку во время блока junit - PullRequest
0 голосов
/ 26 февраля 2020

Я использую весеннюю загрузку 2.2.4 с log4j2.

Я пытался использовать файл generi c log4j2. xml для основного кода и тестов junit.

Для этого файла я добавляю переменную для пути журналов:

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

Когда я выполняю тест junit с аргументом VM (-DLOGS_PATH = target), ошибок нет. проверка в порядке, и журналы создаются в ожидаемой папке.

Но когда я попытался добавить этот аргумент с @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 голосов
/ 27 февраля 2020

Я не верю, что Spring сохраняет свойства, объявленные в SpringBootTest, как системные свойства. Если вы включите jar клиента Log4j Spring Cloud Config, вы сможете указать

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

, и Log4j получит значение LOGS_PATH из среды Spring.

...