Skip to content

Code generated by Tyler doesn't always compile #7

@lewisbirks

Description

@lewisbirks

I was playing around with logback and the Configurator implementation and came across the online translator which is great, however when providing it an async appender example I noticed that the returned code did not compile.

<configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <appender name="ASYNC_CONSOLE" class="ch.qos.logback.classic.AsyncAppender"> <discardingThreshold>0</discardingThreshold> <appender-ref ref="CONSOLE"/> <includeCallerData>false</includeCallerData> <neverBlock>true</neverBlock> </appender> <root level="INFO"> <appender-ref ref="ASYNC_CONSOLE"/> </root> </configuration>

returns (comments removed for brevity)

package com.example; import ch.qos.logback.classic.AsyncAppender; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.encoder.PatternLayoutEncoder; import ch.qos.logback.classic.spi.Configurator; import ch.qos.logback.classic.tyler.TylerConfiguratorBase; import ch.qos.logback.core.Appender; import ch.qos.logback.core.ConsoleAppender; import java.lang.Override; public class TylerConfigurator extends TylerConfiguratorBase implements Configurator { @Override public Configurator.ExecutionStatus configure(LoggerContext loggerContext) { setContext(loggerContext); Appender appenderCONSOLE = setupAppenderCONSOLE(); Appender appenderASYNC_CONSOLE = setupAppenderASYNC_CONSOLE(); appenderASYNC_CONSOLE.addAppender(appenderCONSOLE); Logger logger_ROOT = setupLogger("ROOT", "INFO", null); logger_ROOT.addAppender(appenderASYNC_CONSOLE); 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{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"); patternLayoutEncoder.setParent(appenderCONSOLE); patternLayoutEncoder.start(); // Inject component of type PatternLayoutEncoder into parent appenderCONSOLE.setEncoder(patternLayoutEncoder); appenderCONSOLE.start(); return appenderCONSOLE; } Appender setupAppenderASYNC_CONSOLE() { AsyncAppender appenderASYNC_CONSOLE = new AsyncAppender(); appenderASYNC_CONSOLE.setContext(context); appenderASYNC_CONSOLE.setName("ASYNC_CONSOLE"); appenderASYNC_CONSOLE.setDiscardingThreshold(0); appenderASYNC_CONSOLE.setIncludeCallerData(false); appenderASYNC_CONSOLE.setNeverBlock(true); appenderASYNC_CONSOLE.start(); return appenderASYNC_CONSOLE; } }

The issue here is that setupAppenderASYNC_CONSOLE has a return type defined as an Appender which doesn't have the method addAppender associated with it unlike AsyncAppender.
Also, once manually fixing that, the configuration the generated code then fails at startup as AsyncAppender requires the addAppender method to be called before start.

10:56:30,620 |-INFO in ch.qos.logback.classic.LoggerContext[default] - This is logback-classic version 1.5.16 10:56:30,623 |-INFO in ch.qos.logback.classic.util.ContextInitializer@34b7bfc0 - Here is a list of configurators discovered as a service, by rank: 10:56:30,623 |-INFO in ch.qos.logback.classic.util.ContextInitializer@34b7bfc0 - com.example.TylerConfigurator 10:56:30,623 |-INFO in ch.qos.logback.classic.util.ContextInitializer@34b7bfc0 - They will be invoked in order until ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY is returned. 10:56:30,623 |-INFO in ch.qos.logback.classic.util.ContextInitializer@34b7bfc0 - Constructed configurator of type class com.example.TylerConfigurator 10:56:30,655 |-INFO in ch.qos.logback.core.ConsoleAppender[CONSOLE] - BEWARE: Writing to the console can be very slow. Avoid logging to the 10:56:30,655 |-INFO in ch.qos.logback.core.ConsoleAppender[CONSOLE] - console in production environments, especially in high volume systems. 10:56:30,655 |-INFO in ch.qos.logback.core.ConsoleAppender[CONSOLE] - See also https://logback.qos.ch/codes.html#slowConsole 10:56:30,655 |-ERROR in ch.qos.logback.classic.AsyncAppender[ASYNC_CONSOLE] - No attached appenders found. 10:56:30,658 |-INFO in ch.qos.logback.classic.AsyncAppender[ASYNC_CONSOLE] - Attaching appender named [CONSOLE] to AsyncAppender. 10:56:30,663 |-INFO in ch.qos.logback.classic.util.ContextInitializer@34b7bfc0 - com.example.TylerConfigurator.configure() call lasted 36 milliseconds. ExecutionStatus=DO_NOT_INVOKE_NEXT_IF_ANY 

from a simple hello world application

public class Test { private static final Logger log = LoggerFactory.getLogger(Test.class); public static void main(String[] args) { log.info("hello world"); } }

Metadata

Metadata

Assignees

Labels

DONEbugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions