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 entities #22734

Merged
merged 1 commit into from
Jan 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
@@ -0,0 +1,32 @@
package io.quarkus.deployment;

import org.objectweb.asm.ClassVisitor;

/**
* A subclass of {@link ClassVisitor} that allows carrying around data
* that are useful in the context of Quarkus bytecode transformations.
* Class visitors that require access to these data should extend this class.
*/
public abstract class QuarkusClassVisitor extends ClassVisitor {
public QuarkusClassVisitor(int api) {
super(api);
}

public QuarkusClassVisitor(int api, ClassVisitor classVisitor) {
super(api, classVisitor);
}

// ---

// this could possibly be a Map<String, Object> or something like that,
// but there's no need at the moment
private int originalClassReaderOptions;

public int getOriginalClassReaderOptions() {
return originalClassReaderOptions;
}

public void setOriginalClassReaderOptions(int originalClassReaderOptions) {
this.originalClassReaderOptions = originalClassReaderOptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.quarkus.bootstrap.BootstrapDebug;
import io.quarkus.bootstrap.classloading.ClassPathElement;
import io.quarkus.bootstrap.classloading.QuarkusClassLoader;
import io.quarkus.deployment.QuarkusClassVisitor;
import io.quarkus.deployment.QuarkusClassWriter;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.ApplicationArchivesBuildItem;
Expand Down Expand Up @@ -313,6 +314,9 @@ private byte[] transformClass(String className, List<BiFunction<String, ClassVis
ClassVisitor visitor = writer;
for (BiFunction<String, ClassVisitor, ClassVisitor> i : visitors) {
visitor = i.apply(className, visitor);
if (visitor instanceof QuarkusClassVisitor) {
((QuarkusClassVisitor) visitor).setOriginalClassReaderOptions(classReaderOptions);
}
}
cr.accept(visitor, classReaderOptions);
data = writer.toByteArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;

import io.quarkus.deployment.QuarkusClassVisitor;
import io.quarkus.deployment.QuarkusClassWriter;
import io.quarkus.gizmo.Gizmo;
import net.bytebuddy.ClassFileVersion;
Expand All @@ -36,7 +37,7 @@ public ClassVisitor apply(String className, ClassVisitor outputClassVisitor) {
return new HibernateEnhancingClassVisitor(className, outputClassVisitor);
}

private static class HibernateEnhancingClassVisitor extends ClassVisitor {
private static class HibernateEnhancingClassVisitor extends QuarkusClassVisitor {

private final String className;
private final ClassVisitor outputClassVisitor;
Expand Down Expand Up @@ -78,7 +79,7 @@ public void visitEnd() {
final byte[] transformedBytes = hibernateEnhancement(className, inputBytes);
//Then re-convert the transformed bytecode to not interrupt the visitor chain:
ClassReader cr = new ClassReader(transformedBytes);
cr.accept(outputClassVisitor, 0);
cr.accept(outputClassVisitor, super.getOriginalClassReaderOptions());
}

private byte[] hibernateEnhancement(final String className, final byte[] originalBytes) {
Expand Down
23 changes: 18 additions & 5 deletions integration-tests/logging-panache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
<artifactId>quarkus-arc-deployment</artifactId>
<scope>test</scope>
</dependency>
<!--
- this dependency is enough to test that the Logging with Panache
- and Hibernate ORM with Panache class transformations do not collide
- on setting the class reader options (issue #20569)
-->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-panache</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
Expand Down Expand Up @@ -66,6 +66,19 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-h2-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-deployment</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.quarkus.logging;

import javax.persistence.Entity;

import io.quarkus.hibernate.orm.panache.PanacheEntity;

@Entity
public class LoggingEntity extends PanacheEntity {
public void something() {
Log.info("Hi!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public class LoggingWithPanacheTest {
@RegisterExtension
static final QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(LoggingBean.class, NoStackTraceTestException.class))
.addClasses(LoggingBean.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 -> "io.quarkus.logging.LoggingBean".equals(record.getLoggerName()))
.setLogRecordPredicate(record -> record.getLoggerName().startsWith("io.quarkus.logging.Logging"))
.assertLogRecords(records -> {
Formatter formatter = new PatternFormatter("[%p] %m");
List<String> lines = records.stream().map(formatter::format).map(String::trim).collect(Collectors.toList());
Expand All @@ -38,7 +38,8 @@ public class LoggingWithPanacheTest {
"[WARN] three: foo | bar | baz",
"[ERROR] four: foo | bar | baz | quux",
"[WARN] foo | bar | baz | quux: io.quarkus.logging.NoStackTraceTestException",
"[ERROR] Hello Error: io.quarkus.logging.NoStackTraceTestException");
"[ERROR] Hello Error: io.quarkus.logging.NoStackTraceTestException",
"[INFO] Hi!");
});

@Inject
Expand All @@ -47,5 +48,6 @@ public class LoggingWithPanacheTest {
@Test
public void test() {
bean.doSomething();
new LoggingEntity().something();
}
}