<div dir="auto">Did you notice that the config file still references the unshaded <span style="font-family:"courier new";font-size:12.8px">ch.qos.logback.core.</span><span style="font-family:"courier new";font-size:12.8px">rolling.</span><span style="font-family:"courier new";font-size:12.8px">FixedWindowRollingPolicy </span>class (among others)? <div dir="auto"><br></div><div dir="auto">Anywhere logback loads classes dynamically by string literal will have to be modified to use the shaded package names. ServiceLoader descriptors, etc.<div class="gmail_quote" dir="auto"><div dir="ltr" class="gmail_attr"><br></div><div dir="ltr" class="gmail_attr">HTH</div><div dir="ltr" class="gmail_attr"><br></div><div dir="ltr" class="gmail_attr">On Mon., May 16, 2022, 6:08 p.m. Adam Somerville, <<a href="mailto:adam.somerville@lyniate.com">adam.somerville@lyniate.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div lang="EN-NZ" link="blue" vlink="#954F72" style="word-wrap:break-word">
<div class="m_2391221633914455930WordSection1">
<div>
<p class="MsoNormal">Hi </p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">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:</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">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.</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">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:</p>
<p class="MsoNormal"></p>
<p class="MsoNormal"> <span style="font-family:"Courier New"">final URL url = ClassLoader.getSystemResource(config);<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> if (url == null) {<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> throw new IOException("Unable to find logback configuration file '" + config + "' on the classpath.");<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> }<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> System.out.print("URL found: " + url.toExternalForm());<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> try {<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> this.filepath = Paths.get(url.toURI()).toAbsolutePath();<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> this.filename = filepath.getFileName().toString();<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> this.factory = loadConfig(filepath);<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> ....<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> (init watch service)<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> …<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> } catch (Exception e) {
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> e.printStackTrace(System.err);<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> throw new IOException("Failed to configure logback using configuration file '" + this.filepath + "'.", e);<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> }<u></u><u></u></span></p>
<p class="MsoNormal"> and </p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"> <span style="font-family:"Courier New"">private static ILoggerFactory loadConfig(final Path config) throws IOException {<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> try {<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> final JoranConfigurator configurator = new JoranConfigurator();<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> //final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> final LoggerContext context = new LoggerContext();<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> dumpDebugInformation(context);<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> configurator.setContext(context);<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> context.reset();<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> configurator.doConfigure(config.toFile());<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> final LevelChangePropagator levelChangePropagator = new LevelChangePropagator();<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> levelChangePropagator.setContext(context);<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> context.addListener(levelChangePropagator);<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> return context;<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> } catch (final JoranException e) {<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> throw new IOException("An error occured configuring the RMT logger with file", e);//TODO Better error message<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> }<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> }<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">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.</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">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.</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">this.filepath: <correct path>\libs\RMTLogging.xml<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">this.factory (name): ch.qos.logback.classic.LoggerContext.
<u></u><u></u></span></p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">This is the same as output as when it is working. It seems like it is not binding to SLF4J correctly.
</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">I have tried both:</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"> final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">and </p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"> final LoggerContext context = new LoggerContext();</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">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.</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">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.</p>
<p class="MsoNormal"></p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"> <span style="font-family:"Courier New""><?xml version="1.0" encoding="UTF-8"?><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""><configuration><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <appender name="Main"<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> class="ch.qos.logback.core.rolling.RollingFileAppender"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <!--See also <a href="http://logback.qos.ch/manual/appenders.html#RollingFileAppender" target="_blank" rel="noreferrer">
http://logback.qos.ch/manual/appenders.html#RollingFileAppender</a> --><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <File>Result.log</File><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <encoder><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <!--Old Symphonia pattern <pattern>%-5p %c{2} %x: %m%n</pattern> --><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <pattern>%d{DATE} [%-5p] {%t} \(%c\) %m%n</pattern><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> </encoder><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <rollingPolicy<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <maxIndex>9</maxIndex><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <FileNamePattern>Result.log.%i</FileNamePattern><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> </rollingPolicy><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <triggeringPolicy<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <MaxFileSize>5MB</MaxFileSize><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> </triggeringPolicy><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> </appender><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <logger name="com.orion.symphonia3.MessageFactory" level="ERROR"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <appender-ref ref="Main" /><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> </logger><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <logger name="com.orion.symphonia3.RhapsodyLicenseCheck" level="DEBUG"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <appender-ref ref="Main" /><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> </logger><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <logger name="com.orion.symphonia3.MessageWarehouseDefn" level="DEBUG"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <appender-ref ref="Main" /><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> </logger><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <logger name="load.Message" level="DEBUG"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> <appender-ref ref="Main" /><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> </logger><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">(Etc many more loggers)<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">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.</p>
<p class="MsoNormal"></p>
<p class="MsoNormal"> Any ideas as to what is happening? If you need more information, please let me know.</p>
<p class="MsoNormal"></p>
<p class="MsoNormal"> Kind Regards</p>
<p class="MsoNormal">Adam</p>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<br><br><p style="font-family:Verdana;font-size:10pt;color:#666666"></p></div>
_______________________________________________<br>
logback-user mailing list<br>
<a href="mailto:logback-user@qos.ch" target="_blank" rel="noreferrer">logback-user@qos.ch</a><br>
<a href="http://mailman.qos.ch/mailman/listinfo/logback-user" rel="noreferrer noreferrer" target="_blank">http://mailman.qos.ch/mailman/listinfo/logback-user</a></blockquote></div></div></div>