Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Commit

Permalink
Document how to customize Logback configuration programmatically
Browse files Browse the repository at this point in the history
Closes gh-889
  • Loading branch information
sdeleuze committed Jul 4, 2021
1 parent 2a773dd commit a9e661b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion spring-native-docs/src/main/asciidoc/support.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Beware that actuators significantly increase the footprint, this will be optimiz
* `spring-boot-starter-jdbc`
* `spring-boot-starter-integration`
* `spring-boot-starter-logging`
** Logback is supported, but not configuration with `logback.xml` so please configure it with `application.properties` or `application.yml` for now, see https://github.com/spring-projects-experimental/spring-native/issues/625[#625] for more details.
** Logback is supported, but https://github.com/spring-projects-experimental/spring-native/issues/625[not configuration with `logback.xml`] so please configure it with `application.properties` or `application.yml` for now, or use <<logback-workaround, this workaround>> to configure it programmatically.
** Log4j2 is not supported yet, see https://github.com/spring-projects-experimental/spring-native/issues/115[#115].
* `spring-boot-starter-mail`
* `spring-boot-starter-thymeleaf`
Expand Down
41 changes: 41 additions & 0 deletions spring-native-docs/src/main/asciidoc/troubleshooting.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,47 @@ You should add resource configuration using <<native-hints>>.
Because of a temporary limitation of the AOT plugin, developers need to trigger the `package` phase if they wish to run the application with the Spring Boot Maven plugin:
please use `mvn package spring-boot:run`.

[[logback-workaround]]
==== How can I configure programmatically Logback as a `logback.xml` alternative?

As a workaround for https://github.com/spring-projects-experimental/spring-native/issues/625[#625], you can create a `src/main/resources/META-INF/services/ch.qos.logback.classic.spi.Configurator` file with the following content:

[source,subs="attributes,verbatim"]
----
org.springframework.boot.logging.logback.CustomConfigurator
----

Add a `org.springframework.boot.logging.logback.CustomConfigurator` class similar to the one bellow which will disable Spring Boot logging configuration override, reuse Spring Boot defaults, and allow you to customize Logback `LoggerContext`:

[source,java,subs="attributes,verbatim"]
----
public class CustomConfigurator extends ContextAwareBase implements Configurator {
@Override
public void configure(LoggerContext context) {
// Set this to avoid LoggerContext override by Spring Boot
context.putObject(LoggingSystem.class.getName(), new Object());
// Apply Spring Boot default configuration, make sure CustomConfigurator
// is in the org.springframework.boot.logging.logback package
LogbackConfigurator configurator = new LogbackConfigurator(context);
new DefaultLogbackConfiguration(null).apply(configurator);
context.setPackagingDataEnabled(true);
// Your custom configuration goes there
context.addListener(...);
}
}
----

Add also the following hint to your Spring Boot application class:

[source,java,subs="attributes,verbatim"]
----
@InitializationHint(types = ColorConverter.class, initTime = InitializationTime.BUILD)
----

==== Missing configuration

The Spring AOT plugin will do the best it can to catch everything but it doesn't understand every bit of code out there.
Expand Down

0 comments on commit a9e661b

Please sign in to comment.