Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logback optimization, when appender encoder with charset #313

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parameterType.getDeclaredMethod("forName", String.class);

Not necessary since the type is fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yawkat As you see, you're not right. My first fix was right. LogLevel uses valueOf method, forName needs only for Charset

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you told me about this line parameterType.getDeclaredMethod("forName", String.class); - you're not right too. You need to set argument type - String.class, otherwise you will see exception message

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yawkat For a long time I could not understand what you meant. Only now everything became clear. So, I just don't see the code that you see.

Look at the screenshot with the changes

изображение

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yawkat Also, if you are talking about calling a method, then I think that this would be done to get an exception at the optimization stage, i.e. the exception is thrown in the optimizer code. Otherwise, the code will be generated and you will receive an error only at the compilation stage and it will be much more difficult to understand where the error is. So I left the call to this method

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>
Loading