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 logging with panache in interfaces #29090

Merged
merged 1 commit into from
Nov 7, 2022
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 @@ -43,7 +43,8 @@ public void process(CombinedIndexBuildItem index, BuildProducer<BytecodeTransfor
/**
* Makes the following modifications to the visited class:
* <ul>
* <li>adds a {@code private static final} field of type {@code org.jboss.logging.Logger};</li>
* <li>adds a {@code private static final} field of type {@code org.jboss.logging.Logger}
* ({@code public} in case the class is an interface, to obey the JVMS rules);</li>
* <li>initializes the field (to {@code Logger.getLogger(className)}) at the beginning of the
* static initializer (creating one if missing);</li>
* <li>rewrites all invocations of {@code static} methods on {@code io.quarkus.logging.Log}
Expand All @@ -57,6 +58,8 @@ private static class AddLoggerFieldAndRewriteInvocations extends ClassVisitor {
private final String className;
private final String classNameBinary;

private boolean isInterface;

private boolean generatedLoggerField;
private boolean generatedLoggerFieldInitialization;

Expand All @@ -66,6 +69,14 @@ public AddLoggerFieldAndRewriteInvocations(ClassVisitor visitor, String classNam
this.classNameBinary = className.replace(".", "/");
}

@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
super.visit(version, access, name, signature, superName, interfaces);
if ((access & Opcodes.ACC_INTERFACE) != 0) {
isInterface = true;
}
}

@Override
public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
if (!generatedLoggerField) {
Expand Down Expand Up @@ -190,8 +201,11 @@ public void visitEnd() {
}

private void generateLoggerField() {
super.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL,
SYNTHETIC_LOGGER_FIELD_NAME, JBOSS_LOGGER_DESCRIPTOR, null, null);
// interface fields must be public static final per the JVMS
int access = isInterface
? Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL
: Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL;
super.visitField(access, SYNTHETIC_LOGGER_FIELD_NAME, JBOSS_LOGGER_DESCRIPTOR, null, null);
generatedLoggerField = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import javax.inject.Singleton;

@Singleton
public class LoggingBean {
public class LoggingBean implements LoggingInterface {
// not final to prevent constant inlining
private static String msg = "Heya!";

Expand All @@ -18,6 +18,8 @@ public void setup() {
}

public void doSomething() {
inheritedMethod("abc");

if (Log.isDebugEnabled()) {
Log.debug("starting massive computation");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.quarkus.logging;

public interface LoggingInterface {
default void inheritedMethod(String param) {
if (Log.isInfoEnabled()) {
Log.infof("Default method from interface: %s", param);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
public class LoggingWithPanacheTest {
@RegisterExtension
static final QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(LoggingBean.class, LoggingEntity.class, NoStackTraceTestException.class))
.withApplicationRoot((jar) -> jar.addClasses(LoggingBean.class, LoggingInterface.class, LoggingEntity.class,
NoStackTraceTestException.class))
.overrideConfigKey("quarkus.log.category.\"io.quarkus.logging\".min-level", "TRACE")
.overrideConfigKey("quarkus.log.category.\"io.quarkus.logging\".level", "TRACE")
.setLogRecordPredicate(record -> record.getLoggerName().startsWith("io.quarkus.logging.Logging"))
Expand All @@ -29,6 +29,7 @@ public class LoggingWithPanacheTest {
assertThat(lines).containsExactly(
"[INFO] Heya!",
"[TRACE] LoggingBean created",
"[INFO] Default method from interface: abc",
"[DEBUG] starting massive computation",
"[DEBUG] one: 42",
"[TRACE] two: 42 | 13",
Expand Down