将log4j2-test.xml文件放在test/resource下,修改appenders全部打印到控制台
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appenders>
<console name="Console">
<PatternLayout
pattern="%d [%X{traceId} %X{rpcId} - %X{loginUserEmail}/%X{loginUserID}/%X{remoteAddr}/%X{clientId} - %X{requestURIWithQueryString}] %-5p %c{2} - %m%n%throwable"
charset="UTF-8"/>
</console>
</appenders>
<loggers>
<root level="debug">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>
测试类
public class UserRepositoryTest {
/**不配置也可以都去,建议配置*/
@BeforClass
public static void setLogger(){
System.setProperty("logging.config", "classpath:log4j2-test.xml");
}
@Test
public void test(){
new UserRepository().getUserByName("test");
}
}