如何对log4j2属性文件进行适当的配置,以使记录器在控制台和文件中写入信息。
目前,logger会创建文件,但不会在其中写入信息。
这是我的财产文件:
name = LogConfig
property.filename = Logs
appenders = file, console
# File configuration
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName = ${filename}/Logs.log
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %m%n
# Console configuration
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %m%n
# Configuration of the file logger
loggers = file
logger.file.name = infoFile
logger.file.level = DEBUG
logger.file.appenderRefs = file
logger.file.appenderRefs.file.ref = LOGFILE
rootLogger.level = DEBUG
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
rootLogger.appenderRef.file.ref = LOGFILE
这是我使用记录器的测试文件:
package ru.atom.chat;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ChatClient.class)
@WebAppConfiguration
public class ChatClientTest {
private static final Logger log = LogManager.getLogger(ChatClientTest.class);
@Test
public void login() {
log.debug("This is a debug message");
log.info("This is an info message");
log.warn("This is a warn message");
log.error("This is an error message");
log.fatal("This is a fatal message");
}
}
运行测试后,我进入控制台进行正确的日志记录:
2018-07-14 19:03:13.870 INFO 17106 --- [主] r.a.c.ChatClientTest:这是一条信息消息 2018-07-14 19:03:13.870 WARN 17106 --- [主要] r.a.c.ChatClientTest:这是一条警告消息 2018-07-14 19:03:13.870错误17106 --- [main] r.a.c.ChatClientTest:这是一条错误消息 2018-07-14 19:03:13.871致命17106 --- [主要] r.a.c.ChatClientTest:这是一条致命消息
但是日志文件没有此信息。它只包含一些Spring日志:-(
19:03:09.801 [main]调试 org.springframework.test.context.junit4.SpringJUnit4ClassRunner- SpringJUnit4ClassRunner构造函数,以[class ru.atom.chat.ChatClientTest] 19:03:09.815 [main]调试 org.springframework.test.context.BootstrapUtils-实例化 类的CacheAwareContextLoaderDelegate [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate] 19:03:09.912 [main]调试 org.springframework.test.context.BootstrapUtils-实例化 使用构造函数的BootstrapContext [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)] 19:03:09.989 [main]调试 org.springframework.test.context.BootstrapUtils-实例化 测试类[ru.atom.chat.ChatClientTest]的TestContextBootstrapper 从班级 [org.springframework.boot.test.context.SpringBootTestContextBootstrapper] 19:03:10.043 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper -使用SpringBootContextLoader找不到测试类[ru.atom.chat.ChatClientTest]的@ContextConfiguration和@ContextHierarchy 19:03:10.048 [main]调试 org.springframework.test.context.support.AbstractContextLoader-做过 无法检测测试类的默认资源位置 [ru.atom.chat.ChatClientTest]:类路径资源 [ru / atom / chat / ChatClientTest-context.xml]不存在19:03:10.049 [主要]调试 org.springframework.test.context.support.AbstractContextLoader-做过 无法检测测试类的默认资源位置 [ru.atom.chat.ChatClientTest]:类路径资源 [ru / atom / chat / ChatClientTestContext.groovy]不存在 19:03:10.050 [main] INFO org.springframework.test.context.support.AbstractContextLoader-可以 无法检测测试类的默认资源位置 [ru.atom.chat.ChatClientTest]:找不到后缀资源 {-context.xml,Context.groovy}。 19:03:10.245 [main]调试 org.springframework.test.context.support.ActiveProfilesUtils-可以 找不到注释类型的“注释声明类” [org.springframework.test.context.ActiveProfiles]和类 [ru.atom.chat.ChatClientTest] 19:03:10.684 [main]调试 org.springframework.boot.test.context.SpringBootTestContextBootstrapper -类[ru.atom.chat.ChatClientTest]不存在@TestExecutionListeners:使用默认值。 19:03:10.685 [主要] 信息 org.springframework.boot.test.context.SpringBootTestContextBootstrapper -从位置[META-INF / spring.factories]加载了默认的TestExecutionListener类名称: [org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] 19:03:10.751 [main]调试 org.springframework.boot.test.context.SpringBootTestContextBootstrapper -跳过候选TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] ....
如何将来自该JUnit测试的日志写入文件?请帮助我,我是初学者。