spring-projects / spring-projects/spring-boot

Support for registering a custom Logback Converter conversion rule

Open
#38,687 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: pending-design-work type: enhancement
Dominant language
Java
Stars
81.5k
Forks
42.7k
Avg merge
2d 4h
Merged PRs (30d)
65

Description

In order to register a Custom Logback Converter through conversion rules it is currently required to provide a custom logback.xml file. It is also impossible to provide custom conversion rules through other auto configuration means such as a Spring Boot Auto configuration or some other spring factory mechanism.

The use case for this is to provide some additional context information to the logging. e.g. the currently authenticated user when logging.

If this is something that the Spring Boot team is willing to accept I am more than happy to work on providing an extension mechanism for this.

I have 2 approaches in mind (they can be complimentary to one another).

Using a Customizer

public interface LoggerContextCustomizer {

    void customize(LoggerContext context);

}

This can be applied in the DefaultLogbackConfinguration prior to applying the Spring defaults:

https://github.com/spring-projects/spring-boot/blob/6dff3c5978158f5ddae67b3b92200a90b3b188b7/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java#L58

The LoggerContextCustomizer customizers won't be exposed as beans, but they would be registered in a similar way as the org.springframework.boot.diagnostics.FailureAnalyzer in the spring.factories.

Using properties

This approach could use a property to register custom conversion rules
e.g.

logging.logback.conversion-rule.<conversionWord>=<converterClass>

This approach is more limiting that the one using a customizer and it does close the gap to Log4J 2.
This approach might also be slightly less invasive and would only be targeted towards conversion rules.


Such an approach is not needed for Log4J 2 since Log4J 2 has a completely different mechanism of registering custom converters and the only thing we need to do there is to write our converter with the recommended approach by Log4J 2 and the converter keys will be automatically be available to be used in the log pattern

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with DefaultLogbackConfiguration.java at the referenced customization point, then compare the proposed registration approach with FailureAnalyzer entries in spring.factories. Review the Logback custom conversion rule use case and decide which extension mechanism the project will support. Done means custom conversion rules can be registered without a custom logback.xml file.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
observability-sre
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.