[英]log4j-jul configuration is somehow silencing all log4j2 logging with gradle junit5 tests
To start here's the full source as it is https://github.com/xenoterracide/iv-adh从这里开始是完整的源代码,因为它是https://github.com/xenoterracide/iv-adh
When I enable the system property in my JUnit tasks all of my logging disappears.当我在 JUnit 任务中启用系统属性时,我的所有日志记录都会消失。
tasks.test {
// Use junit platform for unit tests.
useJUnitPlatform()
// this isn't currently enabled in the code, so you can see the logging working when you run the ./gradlew build, but if you uncomment it, you'll see the logs disappear.
systemProperty("java.util.logging.manager","org.apache.logging.log4j.jul.LogManager")
testLogging {
lifecycle {
showStandardStreams = true
displayGranularity = 2
events.addAll(listOf(
TestLogEvent.STARTED,
TestLogEvent.PASSED,
TestLogEvent.SKIPPED,
TestLogEvent.FAILED
))
}
}
reports {
html.isEnabled = false
junitXml.isEnabled = true
}
}
Here's my log4j config这是我的log4j 配置
static void configureLog4j() {
var console = "console";
var builder = ConfigurationBuilderFactory.newConfigurationBuilder();
var defaultAppender = builder.newAppender( console, "CONSOLE" )
.addAttribute( "target", ConsoleAppender.Target.SYSTEM_OUT );
defaultAppender.add( builder.newLayout( "PatternLayout" )
.addAttribute( "pattern", "%highlight{%-5level} - %msg%n - Context %MDC%n%throwable" ) );
builder.add( defaultAppender );
builder.add( builder.newRootLogger( Level.ERROR )
.add( builder.newAppenderRef( console ) ) );
builder.add( builder.newLogger( "com.xenoterracide", Level.DEBUG ) );
Configurator.initialize( builder.build() );
}
here's the relevant part of a test这是测试的相关部分
class RecordAssemblerTest {
@BeforeAll
static void configureLog4j() {
Application.configureLog4j();
}
@Test
void parsers() throws Exception {
I did see other answers on here, but this is the answer they gave, so I'm not certain why it's silencing my other logging.我确实在这里看到了其他答案,但这是他们给出的答案,所以我不确定为什么它会抑制我的其他日志记录。 My end goal is to have all logging working but to silence the
javax.money
/ moneta
logging.我的最终目标是让所有日志记录工作,但使
javax.money
/ moneta
日志记录保持沉默。
update:更新:
setting the property in the JavaExec
task works when using ./gradlew run
, not certain why it isn't working for tests though.在使用
./gradlew run
时,在JavaExec
任务中设置属性有效,但不确定为什么它不适用于测试。
tasks.withType<JavaExec> {
systemProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager")
}
You can debug logging using System.out .您可以使用 System.out 调试日志记录。 Using your code add the
printConfig
method and call it with @AfterAll.使用您的代码添加
printConfig
方法并使用 @AfterAll 调用它。 When I do this with your code and run ./gradlew build
I get the following output:当我使用您的代码执行此操作并运行
./gradlew build
时,我得到以下 output:
FileProcessorTest STANDARD_ERROR
java.util.logging.config.class was null
java.util.logging.config.file was null
LogManager=org.apache.logging.log4j.jul.LogManager
scn=CoreLogger, n=, uph=true, l=WARNING, fl=null
->org.slf4j.bridge.SLF4JBridgeHandler, h=ALL, fl=null
scn=CoreLogger, n=org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=javax.management.timer, uph=true, l=null, fl=null
scn=CoreLogger, n=org.junit.platform.launcher.core.LauncherConfigurationParameters, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.junit.platform.launcher.core.EngineDiscoveryOrchestrator, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.junit.jupiter.engine.descriptor.JupiterTestDescriptor, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.junit.platform.launcher.core.ServiceLoaderTestExecutionListenerRegistry, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.junit.jupiter.engine.descriptor.MethodBasedTestDescriptor, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.junit.jupiter.engine.execution.JupiterEngineExecutionContext, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.junit.jupiter.engine.discovery.MethodOrderingVisitor, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.junit.platform.launcher.core.InternalTestPlan, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=javax.management.monitor, uph=true, l=null, fl=null
scn=CoreLogger, n=javax.management.notification, uph=true, l=null, fl=null
scn=CoreLogger, n=org.junit.jupiter.engine.execution.ConditionEvaluator, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=javax.management.mbeanserver, uph=true, l=null, fl=null
scn=CoreLogger, n=org.junit.platform.launcher.core.ServiceLoaderPostDiscoveryFilterRegistry, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.junit.jupiter.engine.extension.TimeoutConfiguration, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.junit.platform.launcher.core.TestExecutionListenerRegistry, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.junit.jupiter.engine.extension.MutableExtensionRegistry, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.junit.jupiter.engine.discovery.MethodSelectorResolver, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=javax.management.relation, uph=true, l=null, fl=null
scn=CoreLogger, n=javax.management.modelmbean, uph=true, l=null, fl=null
scn=CoreLogger, n=org.junit.jupiter.engine.execution.ExecutableInvoker, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.javamoney.moneta.spi.MoneyUtils, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=javax.management, uph=true, l=null, fl=null
scn=CoreLogger, n=org.junit.jupiter.engine.config.InstantiatingConfigurationParameterConverter, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.junit.platform.launcher.core.EngineIdValidator, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.junit.jupiter.engine.descriptor.DisplayNameUtils, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.junit.platform.commons.util.ReflectionUtils, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=javax.management.misc, uph=true, l=null, fl=null
scn=CoreLogger, n=org.junit.platform.engine.support.hierarchical.NodeTestTask, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.javamoney.moneta.spi.MonetaryConfig, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=javax.management.mlet, uph=true, l=null, fl=null
scn=CoreLogger, n=org.junit.jupiter.engine.config.EnumConfigurationParameterConverter, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.junit.platform.commons.util.ClasspathScanner, uph=true, l=SEVERE, fl=null
scn=CoreLogger, n=org.javamoney.moneta.DefaultMonetaryContextFactory, uph=true, l=SEVERE, fl=null
The takeaways are:要点是:
uph=true
[PASS]uph=true
[PASS] 发布到 SLF4JBridgeHandlercom.xenoterracide
loggers.com.xenoterracide
记录器。 Project uses logj. So lets run this again but without the log4j.jul.LogManager set in either place and I'll use System.out:所以让我们再次运行它,但没有在任一位置设置 log4j.jul.LogManager,我将使用 System.out:
FileProcessorTest STANDARD_OUT
java.util.logging.config.class was null
java.util.logging.config.file was null
LogManager=java.util.logging.LogManager
scn=RootLogger, n=, uph=true, l=INFO, fl=null
->java.util.logging.ConsoleHandler, h=INFO, fl=null
scn=Logger, n=org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation, uph=true, l=null, fl=null
scn=Logger, n=javax.management.timer, uph=true, l=null, fl=null
scn=Logger, n=global, uph=true, l=null, fl=null
scn=Logger, n=org.junit.platform.launcher.core.LauncherConfigurationParameters, uph=true, l=null, fl=null
scn=Logger, n=org.junit.platform.launcher.core.EngineDiscoveryOrchestrator, uph=true, l=null, fl=null
scn=Logger, n=org.junit.jupiter.engine.descriptor.JupiterTestDescriptor, uph=true, l=null, fl=null
scn=Logger, n=org.junit.platform.launcher.core.ServiceLoaderTestExecutionListenerRegistry, uph=true, l=null, fl=null
scn=Logger, n=org.junit.jupiter.engine.descriptor.MethodBasedTestDescriptor, uph=true, l=null, fl=null
scn=Logger, n=org.junit.jupiter.engine.execution.JupiterEngineExecutionContext, uph=true, l=null, fl=null
scn=Logger, n=org.junit.jupiter.engine.discovery.MethodOrderingVisitor, uph=true, l=null, fl=null
scn=Logger, n=org.junit.platform.launcher.core.InternalTestPlan, uph=true, l=null, fl=null
scn=Logger, n=org.junit.jupiter.engine.execution.ConditionEvaluator, uph=true, l=null, fl=null
scn=Logger, n=javax.management.monitor, uph=true, l=null, fl=null
scn=Logger, n=javax.management.notification, uph=true, l=null, fl=null
scn=Logger, n=javax.management.mbeanserver, uph=true, l=null, fl=null
scn=Logger, n=org.junit.platform.launcher.core.ServiceLoaderPostDiscoveryFilterRegistry, uph=true, l=null, fl=null
scn=Logger, n=org.junit.jupiter.engine.extension.TimeoutConfiguration, uph=true, l=null, fl=null
scn=Logger, n=org.junit.platform.launcher.core.TestExecutionListenerRegistry, uph=true, l=null, fl=null
scn=Logger, n=org.junit.jupiter.engine.extension.MutableExtensionRegistry, uph=true, l=null, fl=null
scn=Logger, n=org.junit.jupiter.engine.discovery.MethodSelectorResolver, uph=true, l=null, fl=null
scn=Logger, n=javax.management.relation, uph=true, l=null, fl=null
scn=Logger, n=org.junit.jupiter.engine.execution.ExecutableInvoker, uph=true, l=null, fl=null
scn=Logger, n=javax.management.modelmbean, uph=true, l=null, fl=null
scn=Logger, n=org.javamoney.moneta.spi.MoneyUtils, uph=true, l=null, fl=null
scn=Logger, n=org.junit.jupiter.engine.config.InstantiatingConfigurationParameterConverter, uph=true, l=null, fl=null
scn=Logger, n=javax.management, uph=true, l=null, fl=null
scn=Logger, n=org.junit.platform.launcher.core.EngineIdValidator, uph=true, l=null, fl=null
scn=Logger, n=org.javamoney.moneta.Money, uph=true, l=null, fl=null
scn=Logger, n=org.junit.jupiter.engine.descriptor.DisplayNameUtils, uph=true, l=null, fl=null
scn=Logger, n=org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector, uph=true, l=null, fl=null
scn=Logger, n=org.junit.platform.commons.util.ReflectionUtils, uph=true, l=null, fl=null
scn=Logger, n=javax.management.misc, uph=true, l=null, fl=null
scn=Logger, n=org.junit.platform.engine.support.hierarchical.NodeTestTask, uph=true, l=null, fl=null
scn=Logger, n=org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry, uph=true, l=null, fl=null
scn=Logger, n=org.javamoney.moneta.spi.MonetaryConfig, uph=true, l=null, fl=null
scn=Logger, n=org.junit.jupiter.engine.config.EnumConfigurationParameterConverter, uph=true, l=null, fl=null
scn=Logger, n=org.junit.platform.commons.util.ClasspathScanner, uph=true, l=null, fl=null
scn=Logger, n=javax.management.mlet, uph=true, l=null, fl=null
scn=Logger, n=org.javamoney.moneta.DefaultMonetaryContextFactory, uph=true, l=null, fl=null
Now when I compare the two runs you can see:现在,当我比较两次运行时,您可以看到:
I think the issue you are seeing is in Application.java#L54 .我认为您看到的问题在Application.java#L54中。
builder.add( builder.newRootLogger( level )
One thing you could try would be to explicitly exclude the jul-to-slf4j package.您可以尝试的一件事是明确排除 jul-to-slf4j package。 Maybe that will allow logj4 to take control of jul instead of slf4j.
也许这将允许 logj4 控制 jul 而不是 slf4j。 There is also a related information Java logging: slf4j over jul and log4j2 .
还有一个相关信息Java logging: slf4j over jul 和 log4j2 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.