qos-ch / logback

The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch
Other
3.01k stars 1.28k forks source link

logback-tyler does not support Spring Boot expressions in if conditions #875

Open DarkAtra opened 4 days ago

DarkAtra commented 4 days ago

Take the following configuration:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <if condition='${consoleEnabled}'>
        <then>
            <root level="info">
                <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
                    <encoder>
                        <pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} %5level %logger{0} [%t] - %msg%n</pattern>
                    </encoder>
                </appender>
            </root>
        </then>
    </if>
</configuration>

The logback-tyler output would be:

// package & imports

class TylerConfigurator extends TylerConfiguratorBase implements Configurator {

  @Override
  public Configurator.ExecutionStatus configure(LoggerContext loggerContext) {
    setContext(loggerContext);
    if(${consoleEnabled}) { // <---------------------------- this does not compile
      Logger logger_ROOT = setupLogger("ROOT", "info", null);
      Appender appenderCONSOLE = setupAppenderCONSOLE();
    }
    return ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY;
  }

  Appender setupAppenderCONSOLE() {
    ConsoleAppender appenderCONSOLE = new ConsoleAppender();
    appenderCONSOLE.setContext(context);
    appenderCONSOLE.setName("CONSOLE");

    // Configure component of type PatternLayoutEncoder
    PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
    patternLayoutEncoder.setContext(context);
    patternLayoutEncoder.setPattern("%d{YYYY-MM-dd HH:mm:ss.SSS} %5level %logger{0} [%t] - %msg%n");
    patternLayoutEncoder.setParent(appenderCONSOLE);
    patternLayoutEncoder.start();
    // Inject component of type PatternLayoutEncoder into parent
    appenderCONSOLE.setEncoder(patternLayoutEncoder);

    appenderCONSOLE.start();
    return appenderCONSOLE;
  }
}

As you can see, the if condition that uses a spring expression is not converted correctly - probably because logback-tyler doesn't know anything about spring.

Is there anything that spring-boot could do to participate in the conversion for spring specific features?

Versions:

spring-boot: 3.3.4
logback-classic: 1.5.11
logback-tyler: 0.9
ceki commented 1 day ago

It think adding the appropriate spring-boot code should be feasible.