0

I am currently building a Jenkins plugin, where log4j does the logging. The log is redirected to the Jenkins console using

  LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false)       
  OutputStreamAppender fa = OutputStreamAppender.newBuilder()
        .setName("jenkinslogger")
        .setTarget(listener.getLogger())
        .setLayout(PatternLayout.newBuilder().withPattern("%d{HH:mm:ss.SSS} [%t] %-5level %logger{36}:%L - %msg%n")
              .build())
        .setConfiguration(loggerContext.getConfiguration()).build();
  fa.start();
  
  loggerContext.getConfiguration().addAppender(fa);
  loggerContext.getRootLogger().addAppender(loggerContext.getConfiguration().getAppender(fa.getName()));
  loggerContext.updateLoggers();

where listener is the current TaskListener.

While everything works fine in Java 8, in Java 11, I get the following exception in the log:

2021-02-03 16:21:45.949+0000 [id=54]    WARNING j.t.i.j.MissingClassTelemetry#reportException: Added a missed class for missing class telemetry. Class: sun.reflect.Reflection           
java.lang.ClassNotFoundException: sun.reflect.Reflection                                                                                                                                 
        at jenkins.telemetry.impl.java11.CatcherClassLoader.findClass(CatcherClassLoader.java:47)                                                                                        
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)                                                                                                               
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)                                                                                                               
        at org.apache.logging.log4j.util.LoaderUtil.loadClass(LoaderUtil.java:167)                                                                                                       
        at org.apache.logging.log4j.util.StackLocator.<clinit>(StackLocator.java:64)                                                                                                     
        at org.apache.logging.log4j.util.StackLocatorUtil.<clinit>(StackLocatorUtil.java:33)                                                                                             
        at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:139)                                                             
        at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:124)                                                             
        at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:230)                                                                               
        at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:47)                                                                                
        at org.apache.logging.log4j.LogManager.getContext(LogManager.java:174)                                                                                                           
        at de.peass.ci.JenkinsLogRedirector.<init>(JenkinsLogRedirector.java:14)                                                                                                         
        at de.peass.ci.MeasureVersionBuilder.perform(MeasureVersionBuilder.java:66)                                                                                                      
        at jenkins.tasks.SimpleBuildStep.perform(SimpleBuildStep.java:123)                                                                                                               
        at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:80)                                                                                         
        at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)                                                                                                             
        at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:804)                                                                                             
        at hudson.model.Build$BuildExecution.build(Build.java:197)                                                                                                                       
        at hudson.model.Build$BuildExecution.doRun(Build.java:163)                                                                                                                       
        at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:514)                                                                                                 
        at hudson.model.Run.execute(Run.java:1907)                                                                                                                                       
        at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)                                                                                                                       
        at hudson.model.ResourceController.execute(ResourceController.java:97)                                                                                                           
        at hudson.model.Executor.run(Executor.java:429)                                                                                                                                  
WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance. 

Afterwards, output is only displayed on the console. I supposed this has something to do with the multi-release flag (like described in Is log4j2 compatible with Java 11?), but adding Multi-Release: true manually to the MANIFEST.MF in the .hpi does not overcome the problem.

Does anyone know a solution / workaround, so that logs can be redirected in both Java 8 and 11?

David Georg Reichelt
  • 963
  • 1
  • 15
  • 36
  • Does this answer your question? [Is log4j2 compatible with Java 11?](https://stackoverflow.com/questions/53049346/is-log4j2-compatible-with-java-11) – Joe May 15 '21 at 11:54

1 Answers1

0

One solution is to give the LogManager the current classloader.

LoggerContext loggerContext = (LoggerContext) LogManager.getContext(LogManager.class.getClassLoader(), false);

While this solves the problem that logs are not redirected, it does not remove the exception. I'll accept an answer that also removes the exception.

David Georg Reichelt
  • 963
  • 1
  • 15
  • 36