From 003fa50ed6a1cb1c6ca851935aed9f225e1f0161 Mon Sep 17 00:00:00 2001 From: altro3 Date: Wed, 21 Aug 2024 17:37:06 +0700 Subject: [PATCH] Fix logback optimization, when appender encoder with charset (#313) --- .../sourcegen/Logback14GeneratorHelper.java | 10 ++- ...ackConfigurationSourceGeneratorTest.groovy | 82 +++++++++++++++++++ .../src/test/resources/logback-test6.xml | 17 ++++ 3 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 aot-std-optimizers/src/test/resources/logback-test6.xml diff --git a/aot-std-optimizers/src/main/java/io/micronaut/aot/std/sourcegen/Logback14GeneratorHelper.java b/aot-std-optimizers/src/main/java/io/micronaut/aot/std/sourcegen/Logback14GeneratorHelper.java index 68c838df..078313ab 100644 --- a/aot-std-optimizers/src/main/java/io/micronaut/aot/std/sourcegen/Logback14GeneratorHelper.java +++ b/aot-std-optimizers/src/main/java/io/micronaut/aot/std/sourcegen/Logback14GeneratorHelper.java @@ -53,6 +53,7 @@ import java.io.IOException; import java.lang.reflect.Method; import java.net.URL; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -315,8 +316,13 @@ private boolean maybeGenerateAddOrSet(ImplicitModel model, Model parent, BiFunct return true; } else { try { - Method valueOf = parameterType.getDeclaredMethod("valueOf", String.class); - codeBuilder.addStatement("$L.$L($T.valueOf($S))", parentVarName, method.getName(), ClassName.get(parameterType), model.getBodyText()); + if (Charset.class.equals(parameterType)) { + parameterType.getDeclaredMethod("forName", String.class); + codeBuilder.addStatement("$L.$L($T.forName($S))", parentVarName, method.getName(), ClassName.get(parameterType), model.getBodyText()); + } else { + parameterType.getDeclaredMethod("valueOf", String.class); + codeBuilder.addStatement("$L.$L($T.valueOf($S))", parentVarName, method.getName(), ClassName.get(parameterType), model.getBodyText()); + } return true; } catch (NoSuchMethodException e) { throw new RuntimeException("Unable to convert type" + parameterType); diff --git a/aot-std-optimizers/src/test/groovy/io/micronaut/aot/std/sourcegen/LogbackConfigurationSourceGeneratorTest.groovy b/aot-std-optimizers/src/test/groovy/io/micronaut/aot/std/sourcegen/LogbackConfigurationSourceGeneratorTest.groovy index f83e945d..19d6bb53 100644 --- a/aot-std-optimizers/src/test/groovy/io/micronaut/aot/std/sourcegen/LogbackConfigurationSourceGeneratorTest.groovy +++ b/aot-std-optimizers/src/test/groovy/io/micronaut/aot/std/sourcegen/LogbackConfigurationSourceGeneratorTest.groovy @@ -482,6 +482,88 @@ public class StaticLogbackConfiguration implements Configurator { } } + def "logback appender with charset"() { + configFileName = "logback-test6.xml" + + when: + generate() + + then: + excludesResources("logback-test6.xml") + assertThatGeneratedSources { + doesNotCreateInitializer() + hasClass("StaticLogbackConfiguration") { + withSources """package io.micronaut.test; + +import ch.qos.logback.classic.Level; +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.core.ConsoleAppender; +import ch.qos.logback.core.Context; +import ch.qos.logback.core.status.Status; +import java.lang.String; +import java.lang.Throwable; +import java.nio.charset.Charset; + +public class StaticLogbackConfiguration implements Configurator { + private Context context; + + public Configurator.ExecutionStatus configure(LoggerContext loggerContext) { + ConsoleAppender stdout = new ConsoleAppender(); + stdout.setWithJansi(false); + PatternLayoutEncoder encoder = new PatternLayoutEncoder(); + encoder.setPattern("%cyan(%d{HH:mm:ss.SSS}) %highlight(%-5level) %gray([%thread]) %magenta(%logger{25}) [%file:%line] - %msg%n"); + encoder.setCharset(Charset.forName("UTF-8")); + encoder.setContext(context); + encoder.start(); + stdout.setEncoder(encoder); + stdout.setContext(context); + stdout.start(); + Logger com_zaxxer = loggerContext.getLogger("com.zaxxer"); + com_zaxxer.setLevel(Level.WARN); + Logger _rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); + _rootLogger.setLevel(Level.INFO); + _rootLogger.addAppender(stdout); + return Configurator.ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY; + } + + public void setContext(Context context) { + this.context = context; + } + + public Context getContext() { + return context; + } + + public void addStatus(Status status) { + } + + public void addInfo(String info) { + } + + public void addInfo(String info, Throwable ex) { + } + + public void addWarn(String warn) { + } + + public void addWarn(String warn, Throwable ex) { + } + + public void addError(String error) { + } + + public void addError(String error, Throwable ex) { + } +} +""" + } + compiles() + } + } + class TestLogbackConfigurationSourceGenerator extends LogbackConfigurationSourceGenerator { @Override protected String getLogbackFileName() { diff --git a/aot-std-optimizers/src/test/resources/logback-test6.xml b/aot-std-optimizers/src/test/resources/logback-test6.xml new file mode 100644 index 00000000..c2d7a266 --- /dev/null +++ b/aot-std-optimizers/src/test/resources/logback-test6.xml @@ -0,0 +1,17 @@ + + + + + false + + %cyan(%d{HH:mm:ss.SSS}) %highlight(%-5level) %gray([%thread]) %magenta(%logger{25}) [%file:%line] - %msg%n + UTF-8 + + + + + + + + +