log4j-jul 配置以某种方式通过 gradle junit5 测试使所有 log4j2 日志记录静音

问题描述

这里是完整的源代码https://github.com/xenoterracide/iv-adh

当我在 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
  }
}

这是我的 log4j config

  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() );
  }

这是 test 的相关部分

class RecordAssemblerTest {

  @BeforeAll
  static void configureLog4j() {
    Application.configureLog4j();
  }

  @Test
  void parsers() throws Exception {

我确实在这里看到了其他答案,但这是他们给出的答案,所以我不确定为什么它会阻止我的其他日志记录。我的最终目标是让所有日志记录工作,但使 javax.money/moneta 日志记录静音。

更新:

在使用 JavaExec 时在 ./gradlew run 任务中设置属性有效,但不确定为什么它不适用于测试。

tasks.withType<JavaExec> {
  systemProperty("java.util.logging.manager","org.apache.logging.log4j.jul.LogManager")
}

解决方法

你可以debug logging using System.out。使用您的代码添加 vcl.h 方法并使用 @AfterAll 调用它。当我使用您的代码执行此操作并运行 printConfig 时,我得到以下输出:

 ./gradlew build

要点是:

  1. org.apache.logging.log4j.jul.LogManager 已安装。 [通过]
  2. org.slf4j.bridge.SLF4JBridgeHandler 已安装并设置为 ALL。您的代码正在使用 log4j,但似乎也设置了 SLF4J [警告]
  3. 所有记录器都根据 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,l=SEVERE,n=javax.management.timer,l=null,n=org.junit.platform.launcher.core.LauncherConfigurationParameters,n=org.junit.platform.launcher.core.EngineDiscoveryOrchestrator,n=org.junit.jupiter.engine.descriptor.JupiterTestDescriptor,n=org.junit.platform.launcher.core.ServiceLoaderTestExecutionListenerRegistry,n=org.junit.jupiter.engine.descriptor.MethodBasedTestDescriptor,n=org.junit.jupiter.engine.execution.JupiterEngineExecutionContext,n=org.junit.jupiter.engine.discovery.MethodOrderingVisitor,n=org.junit.platform.launcher.core.InternalTestPlan,n=javax.management.monitor,n=javax.management.notification,n=org.junit.jupiter.engine.execution.ConditionEvaluator,n=javax.management.mbeanserver,n=org.junit.platform.launcher.core.ServiceLoaderPostDiscoveryFilterRegistry,n=org.junit.jupiter.engine.extension.TimeoutConfiguration,n=org.junit.platform.launcher.core.TestExecutionListenerRegistry,n=org.junit.jupiter.engine.extension.MutableExtensionRegistry,n=org.junit.jupiter.engine.discovery.MethodSelectorResolver,n=javax.management.relation,n=javax.management.modelmbean,n=org.junit.jupiter.engine.execution.ExecutableInvoker,n=org.javamoney.moneta.spi.MoneyUtils,n=javax.management,n=org.junit.jupiter.engine.config.InstantiatingConfigurationParameterConverter,n=org.junit.platform.launcher.core.EngineIdValidator,n=org.junit.jupiter.engine.descriptor.DisplayNameUtils,n=org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector,n=org.junit.platform.commons.util.ReflectionUtils,n=javax.management.misc,n=org.junit.platform.engine.support.hierarchical.NodeTestTask,n=org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry,n=org.javamoney.moneta.spi.MonetaryConfig,n=javax.management.mlet,n=org.junit.jupiter.engine.config.EnumConfigurationParameterConverter,n=org.junit.platform.commons.util.ClasspathScanner,n=org.javamoney.moneta.DefaultMonetaryContextFactory,fl=null [PASS]
  4. 发布到 SLF4JBridgeHandler
  5. 没有 uph=true 记录器。项目使用 logj。 [通过]
  6. 根记录器设置为警告。只会看到 WARN 和 ERROR。 [有问题]
  7. 子记录器是 null 和 SEVERE 的混合体。只会看到警告和错误。[QUESTIONABLE]

所以让我们再次运行它,但不要在任何地方设置 log4j.jul.LogManager,我将使用 System.out:

com.xenoterracide

现在,当我比较两次运行时,您可以看到:

  1. 当 log4j 日志管理器运行时,级别要么是从根记录器继承的警告,要么是在子记录器上设置的严重级别。
  2. 运行 JUL 日志管理器时,所有级别都继承了 INFO。

我认为您看到的问题在于 Application.java#L54

  1. 您需要将根记录器级别设置为给定级别。 FileProcessorTest STANDARD_OUT java.util.logging.config.class was null java.util.logging.config.file was null LogManager=java.util.logging.LogManager scn=RootLogger,l=INFO,fl=null ->java.util.logging.ConsoleHandler,h=INFO,fl=null scn=Logger,n=global,n=org.javamoney.moneta.Money,fl=null
  2. log4j 配置未正确更新 JUL 记录器级别。这指向 Application::configureLog4j 中的错误。
  3. 未安装 org.slf4j.bridge.SLF4JBridgeHandler。也许当安装了桥接处理程序时,输出将发送到 SLF4J,并且没有为 SLF4J 设置控制台附加程序。

您可以尝试的一件事是明确排除 jul-to-slf4j 包。也许这将允许 logj4 控制 jul 而不是 slf4j。还有一个相关信息Java logging: slf4j over jul and log4j2

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...