Logback refresh ignores logger.config and Configurator status, and fails without logback.xml
Expected Behavior
LoggingSystem.refresh() should configure Logback the same way Logback's own startup does. The exception is a location that only Micronaut can see: logger.config, or logback.configurationFile set in Micronaut configuration. That location should win.
Actual Behaviour
LogbackLoggingSystem.refresh() resets the context and calls LogbackUtils.configure. That method departs from Logback's ContextInitializer.autoConfig in four ways:
- Rank and status are ignored. It uses the first
ServiceLoaderConfiguratorwithout sorting by@ConfiguratorRank, and it discards the returnedExecutionStatus.- A configurator that returns
INVOKE_NEXT_IF_ANYleaves the context with no appenders. - A configurator that returns
NEUTRALloses thelogback.xmlthat Logback would apply after it.
- A configurator that returns
- Any
Configuratorservice overrideslogger.config. Micronaut AOT'sStaticLogbackConfigurationis one such service. It always returnsDO_NOT_INVOKE_NEXT_IF_ANY(source), and Launch enables it by default for Gradle (aotExtension.rocker.raw#L14) and in Maven'saot-jar.properties(MicronautAot.java#L144), which applies oncemicronaut.aot.enabledis true. - Otherwise the location defaults to
logback.xml(source).- In tests, the refresh replaces
logback-test.xmlwithlogback.xml. - With no
logback.xml, startup fails withResource logback.xml not found.
- In tests, the refresh replaces
- The management
LogbackLoggingSystemreads onlylogger.config(source). It replaces the context one when the loggers endpoint is enabled. So the #8596 fix does not apply there, and-Dlogback.configurationFileis lost.
The refresh runs at every startup that has any logger.* property. PropertiesLoggingLevelsConfigurer is @Context @Requires(property = "logger") and calls refresh() from its constructor (source). So the effective configuration depends on whether such a property exists.
Steps To Reproduce
A main prints the root logger's appenders after Logback's startup and again after ApplicationContext.run(props). It uses:
- a stub
ConfiguratorinMETA-INF/services. It either adds aSTUBappender and returnsDO_NOT_INVOKE_NEXT_IF_ANYorNEUTRAL, or declines withINVOKE_NEXT_IF_ANY; logback.xml(MAIN),logback-test.xml(TEST) andcustom-logback.xml(CUSTOM).
Each row is one JVM. lv means logger.levels.foo=INFO. The "Fix" column is a prototype of (a) and (b) below.
| # | Setup | Logback startup | Today | Fix |
|---|---|---|---|---|
| 1 | test + main XML, lv |
TEST | MAIN | TEST |
| 2 | test XML only, lv |
TEST | fails | TEST |
| 3 | no XML, lv |
console | fails | console |
| 4 | stub, logger.config=custom-logback.xml |
STUB | STUB | CUSTOM |
| 5 | stub declines, main XML, lv |
MAIN | none | MAIN |
| 6 | stub returns NEUTRAL, main XML, lv |
STUB+MAIN | STUB | STUB+MAIN |
| 7 | stub, logback.configurationFile in config, lv |
STUB | STUB | CUSTOM |
| 8 | stub, -Dlogback.configurationFile, lv |
STUB | STUB | STUB |
| 9 | -Dlogback.configurationFile, lv, loggers endpoint on |
CUSTOM | MAIN | not run |
Environment Information
- macOS 26.6.2 on arm64
- JDK 25.0.4.1
- logback-classic 1.5.38
Example Application
None. See Steps To Reproduce.
Version
5.3.0-SNAPSHOT: code read on 5.3.x at ea3a123. Reproduced on 5.2.2, which has the same logic.
Proposed fix
LogbackUtils is public, and its configure(ClassLoader, LoggerContext, String) cannot tell an explicit location from the default. Add a method, deprecate the old one, and use it from both LogbackLoggingSystems. That fixes point 4.
- (a) A Micronaut-only location wins. That is
logger.config, or alogback.configurationFilethat is not also a JVM system property. Configure from it withDefaultJoranConfigurator, whatever configurators exist. Keep today's lookup, the missing-file error, and the precedence oflogback.configurationFileoverlogger.config. - (b) Otherwise, delegate to Logback through the public
new ContextInitializer(context).autoConfig(classLoader). It applies rank and status, then Joran (-Dlogback.configurationFile, thenlogback-test.xml, thenlogback.xml), thenBasicConfigurator. - (c) Optionally, and only after measuring it, skip the first startup refresh when there is no Micronaut-only location. Logback has already produced the same result.
- To keep levels isolated between several contexts in one JVM (as in micronaut-test), mark the context with
putObjectafter every refresh. reset()clears the object map, so the mark must be set again each time.
- To keep levels isolated between several contexts in one JVM (as in micronaut-test), mark the context with
Compatibility and tests
(a) only changes setups that are broken today.
(b) makes the refresh match Logback's startup:
logback-test.xmlsurvives the refresh;- a missing
logback.xmlno longer fails startup; - Joran runs after configurators that return
NEUTRALorINVOKE_NEXT_IF_ANY; - the highest rank wins;
- a missing
-Dlogback.configurationFiletarget falls back as it does in Logback.
This needs a release note.
test-suite-logbackdepends on today's behaviour: itsCustomConfiguratorreturnsNEUTRAL, and its spec forces a refresh "so the XML configuration is ignored". That configurator should returnDO_NOT_INVOKE_NEXT_IF_ANY.Tests: turn the table into specs.
- The configurator cases can use a fresh
LoggerContextand a class loader that holds the service entry. - The XML-lookup cases need their own module, because Joran resolves files through its own class loader.
- For (c), use two contexts in one JVM with different
logger.levels.
- The configurator cases can use a fresh
How this was found
This came up while profiling micronaut-runner on its benchmark-large sample. Runner's planned build-time configurator (linked under Related) would have to work around points 1 and 2.
Method: Runner JARs, micronaut-context 5.1.15 (same code), JDK 25.0.4.1. Readiness is the first HTTP 200 on /hello. Variants were interleaved at random and paired per round. Figures are medians of paired differences, with bootstrap 95% CIs.
Replacing Joran with a prototype of that configurator that also precompiled the pattern layout saved:
| Uncached | JDK AOT cache | |
|---|---|---|
No logger.* property (n=60) |
28.0 ms | 11.2 ms |
A logger.levels property (n=20) |
33.1 ms | 18.3 ms |
The configurator alone, without the precompiled layout, saved 16.5 ms uncached and 8.2 ms cached in the first row (n=60); the second row was not measured for it. Every 95% CI excludes 0. The difference between the table's two rows, which come from the same prototype, suggests the second Joran pass costs roughly 5 ms uncached and 7 ms cached. This is an estimate across sessions, not a measurement of (c).
Related
- #8596 and #9009, #8650 and #8674, #8918 and #9859.
- micronaut-projects/micronaut-aot#512: with that fix, AOT's configurator runs
DefaultJoranConfiguratoritself whenlogback.configurationFileis set, so it does not depend on this issue. (a) is what stops it shadowinglogger.configafter a refresh (rows 4 and 7). - micronaut-projects/micronaut-gradle-plugin#1380: its generated wrapper runs Joran itself instead of returning
INVOKE_NEXT_IF_ANY, so it does not need (b). Until (a) ships, alogger.configset in Micronaut configuration is ignored after a refresh, as it is with AOT's configurator. - https://github.com/alvarosanchez/micronaut-runner/issues/152: Runner's planned configurator.
- https://github.com/alvarosanchez/micronaut-runner/issues/135: Runner packages AOT's optimized JAR, including
StaticLogbackConfiguration.
Source: micronaut-projects/micronaut-core