Java 如何配置Log4j2属性文件以允许在控制台和文件中写入日志

Java 如何配置Log4j2属性文件以允许在控制台和文件中写入日志,java,spring-boot,logging,Java,Spring Boot,Logging,如何正确配置log4j2属性文件,使记录器在控制台和文件中写入信息 目前,记录器创建文件,但不在其中写入信息 这是我的属性文件: name = LogConfig property.filename = Logs appenders = file, console # File configuration appender.file.type = File appender.file.name = LOGFILE appender.file.fileName = ${filename}/Log

如何正确配置log4j2属性文件,使记录器在控制台和文件中写入信息

目前,记录器创建文件,但不在其中写入信息

这是我的属性文件:

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
下面是我使用logger的测试文件:

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信息17106---[main] r、 这是一条信息消息 2018-07-14 19:03:13.870警告17106---[main] 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、 这是一条致命的消息

但是日志文件没有这个信息。它只包含一些Spring日志:-(

19:03:09.801[主]调试 org.springframework.test.context.junit4.SpringJUnit4ClassRunner- 使用[class]调用的SpringJUnit4ClassRunner构造函数 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[主]调试 org.springframework.test.context.BootstrapUtils-实例化 使用构造函数[public]的BootstrapContext org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)] 19:03:09.989[主]调试 org.springframework.test.context.BootstrapUtils-实例化 测试类[ru.atom.chat.ChatClientTest]的TestContextBootstrapper 来自课堂 [org.springframework.boot.test.context.SpringBootTestContextBootstrapper] 19:03:10.043[主要]信息 org.springframework.boot.test.context.SpringBootTestContextBootstrapper -使用SpringBootContextLoader未找到测试类[ru.atom.chat.ChatClientTest]的@ContextConfiguration和@ContextHierarchy 19:03:10.048[主]调试 org.springframework.test.context.support.AbstractContextLoader-Did 未检测测试类的默认资源位置 [ru.atom.chat.ChatClientTest]:类路径资源 [ru/atom/chat/ChatClientTest context.xml]不存在19:03:10.049 [main]调试 org.springframework.test.context.support.AbstractContextLoader-Did 未检测测试类的默认资源位置 [ru.atom.chat.ChatClientTest]:类路径资源 [ru/atom/chat/ChatClientTestContext.groovy]不存在 19:03:10.050[主要]信息 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 -@TestExecutionListeners不存在于类[ru.atom.chat.ChatClientTest]:使用默认值。19:03:10.685[main] 信息 org.springframework.boot.test.context.SpringBootTestContextBootstrapper -已从位置[META-INF/spring.factories]加载默认TestExecutionListener类名: [org.springframework.boot.test.autoconfigure.restdocs.restdocstexecutionListener, 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.resetMocksteExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.dirtiesContextBeforeModesteExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.sqlscriptstexecutionListener] 19:03:10.751[主]调试 org.springframework.boot.test.context.SpringBootTestContextBootstrapper -正在跳过候选TestExecutionListener[org.springframework.test.context.transaction.TransactionalTestExecutionListener]


如何将JUnit测试中的日志写入文件?请帮助我,我是一名初学者。

您可能需要从maven/gradle中排除spring的默认日志依赖项。检查有关日志记录的内容。我已经完成了。下面是我的build.gradle
配置{compile.exclude模块:'spring boot starter logging'}