[logback-user] Issues with logback and maven/shading.

Adam Somerville adam.somerville at lyniate.com
Tue May 17 00:07:32 CEST 2022


Hi

I work for Lyniate and I am trying to use logback in one of our products. For the most part things are great, expect for one niche issue I have been stuck on for days. It’s a little complex:

Overall, I am building a jar that people can incorporate into their own applications.  It is built with maven, when I include logback a dependency it works. However, when I try to shade logback in the jar it seems to ignore my configuration properties files. i.e. I cannot see any log file when I run.

This only occurs in the built jar, I cannot reproduce in a dev environment at all, and as mentioned it only ignores config when shading logback. Here are the relevant code snippets:
              final URL url = ClassLoader.getSystemResource(config);
     if (url == null) {
       throw new IOException("Unable to find logback configuration file '" + config + "' on the classpath.");
     }

     System.out.print("URL found: " + url.toExternalForm());

     try {
       this.filepath = Paths.get(url.toURI()).toAbsolutePath();
       this.filename = filepath.getFileName().toString();
       this.factory = loadConfig(filepath);

     ....
     (init watch service)
     …
     } catch (Exception e) {
       e.printStackTrace(System.err);
       throw new IOException("Failed to configure logback using configuration file '" + this.filepath  + "'.", e);
     }
  and

  private static ILoggerFactory loadConfig(final Path config) throws IOException {
     try {
       final JoranConfigurator configurator = new JoranConfigurator();
       //final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
       final LoggerContext context = new LoggerContext();
       dumpDebugInformation(context);

       configurator.setContext(context);
       context.reset();
       configurator.doConfigure(config.toFile());

       final LevelChangePropagator levelChangePropagator = new LevelChangePropagator();
       levelChangePropagator.setContext(context);
       context.addListener(levelChangePropagator);

       return context;

     } catch (final JoranException e) {
       throw new IOException("An error occured configuring the RMT logger with file", e);//TODO Better error message
     }
  }


FYI am also initialising a java watch service in the constructor where the first snippet is called, not shown above thought I better mention in case it is relevant.

When the code executes  I get the correct output for all the paths and what looks to be the right object. However, it does not produce my log file.

this.filepath: <correct path>\libs\RMTLogging.xml
this.factory (name): ch.qos.logback.classic.LoggerContext.

This is the same as output as when it is working. It seems like it is not binding to SLF4J correctly.

I have tried both:

                    final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();

and

                    final LoggerContext context = new LoggerContext();

I suspected the shading is somehow interfering with binding to SLF4J but instantiating the LoggerContext directly does not seem to make a difference. There are no errors or exceptions. Just no log file when I try and shade logback into my final built jar. Which means even though it has the correct config file path and appears load it,   seems it is not really being configured.

Below are my settings file. It works in dev environment and a non-shaded jar. When it is shaded with the same config it just does not work.

 <?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appender name="Main"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
     <!--See also http://logback.qos.ch/manual/appenders.html#RollingFileAppender -->
     <File>Result.log</File>
     <encoder>
       <!--Old Symphonia pattern <pattern>%-5p %c{2} %x: %m%n</pattern> -->
       <pattern>%d{DATE} [%-5p] {%t} \(%c\) %m%n</pattern>
     </encoder>
     <rollingPolicy
       class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
       <maxIndex>9</maxIndex>
       <FileNamePattern>Result.log.%i</FileNamePattern>
     </rollingPolicy>
     <triggeringPolicy
       class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
       <MaxFileSize>5MB</MaxFileSize>
     </triggeringPolicy>
  </appender>
  <logger name="com.orion.symphonia3.MessageFactory" level="ERROR">
     <appender-ref ref="Main" />
  </logger>
  <logger name="com.orion.symphonia3.RhapsodyLicenseCheck" level="DEBUG">
     <appender-ref ref="Main" />
  </logger>
  <logger name="com.orion.symphonia3.MessageWarehouseDefn" level="DEBUG">
     <appender-ref ref="Main" />
  </logger>
  <logger name="load.Message" level="DEBUG">
     <appender-ref ref="Main" />
  </logger>
(Etc many more loggers)


The shading is fairly critical for us. There are several other shaded third party jars, with no issues. It just seems to be just the configuration part of logback that causes these issue.
 Any ideas as to what is happening? If you need more information, please let me know.
 Kind Regards
Adam

Disclaimer

The information contained in this communication from the sender is confidential. It is intended solely for use by the recipient and others authorized to receive it. If you are not the recipient, you are hereby notified that any disclosure, copying, distribution or taking action in relation of the contents of this information is strictly prohibited and may be unlawful.

This email has been scanned for viruses and malware, and may have been automatically archived by Mimecast Ltd, an innovator in Software as a Service (SaaS) for business. Providing a safer and more useful place for your human generated data. Specializing in; Security, archiving and compliance. To find out more visit the Mimecast website.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20220516/36e386c1/attachment-0001.html>


More information about the logback-user mailing list