Skip to content

Commit

Permalink
Fix logback optimization, when appender encoder with charset (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
altro3 authored Aug 21, 2024
1 parent 6bb6611 commit 003fa50
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
17 changes: 17 additions & 0 deletions aot-std-optimizers/src/test/resources/logback-test6.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>false</withJansi>
<encoder>
<pattern>%cyan(%d{HH:mm:ss.SSS}) %highlight(%-5level) %gray([%thread]) %magenta(%logger{25}) [%file:%line] - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>

<logger name="com.zaxxer" level="WARN"/>

<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>

0 comments on commit 003fa50

Please sign in to comment.