Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 762
Piotr Avatar asked Mar 10 '23 21:03

Piotr


1 Answers

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();
}
like image 196
Shawn Clark Avatar answered Mar 19 '23 22:03

Shawn Clark