Skip to content
Advertisement

Logback conversion rule parametrizing

Is there any way of parametrizing conversion rule in Logback? I’ve tried adding child nodes, additional attributes and I don’t see a way to do it.

<conversionRule conversionWord="boundedMsg" converterClass="com.package.util.logging.converters.LongMessageConverter">

I would like to add parameter that will be used by LongMessageConverter class.

My application is setup on Spring Boot and I am using Sl4J.

Advertisement

Answer

This is more a question around Logback than anything to do with Spring Boot. What you need to do is something similar to the MDCConverter. Within your pattern you would specify something like:

<conversionRule conversionWord="boundedMsg" converterClass="com.package.util.logging.converters.LongMessageConverter"/>
        
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  <encoder>
    <pattern>%boundedMsg{25} [%thread] - %msg%n</pattern>
  </encoder>
</appender>

The 25 is an option made available to the Converter during the start() method call. It is identified as the first option. You could end up passing a number of options to the converter. This way the converter is generic for any number of patterns that you specify in your Logback configuration.

The start method would look like:

private int msgLength;

@Override
public void start() {
    msgLength = Integer.parseInt(getFirstOption());
    super.start();
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement