Comments (2)
Thank you for looking into this, and the effort in trying to solve it in LogCaptor.
In my own investigation I did see where the exception was thrown, and that the classes were from different classloaders. Your conclusions match the suspicion I had at that point: it's a quarkus thing that is not easily solved. Further research shows that @QuarkusTest
is known to do advanced things with classloading, and also disrupts usage of other junit5 extensions (e.g. junit-kafka).
Also thanks for the suggestion of ConsoleCaptor. I'll check how I can make that work in my current situation.
from log-captor.
Hoi Sander,
Gaaf om een Nederlandse ontwikkelaar hier te zien, welkom!
Thank you for raising this issue. The sample project is very helpful to easily reproduce the issue. The exception message you are getting is confusing and should be improved. I will create a change in the library for that specific use case.
LogCaptor needs Logback to capture logs and in your case it is already using that so it should not raise the exception, but this exception is being caused because of a different reason. The Logger of GreetingResource is created by jdk.internal.loader.ClassLoaders$AppClassLoader
while all classes within the test even LogCaptor is created by io.quarkus.bootstrap.classloading.QuarkusClassLoader
because of the @QuarkusTest
annotation. It will try to cast the captured Logger which is of the type ch.qos.logback.classic.Logger
to ch.qos.logback.classic.Logger
but it fails because those to objects are totally different classes to the JVM because they are created by different classloaders.
I have tried to resolve this issue on the library side by changing the classloader order, and also used reflection to specify which classloader the library should use and some other methods but I could not resolve it. I would advise to create the same issue at https://github.com/quarkusio/quarkus They might know how to resolve this classloader issue as the annotation @QuarkusTest
is causing this issue.
For the time being you can use ConsoleCaptor, which is also created by me. That library captures everything from the console output. If you make sure to forward all of your logs to the console by using slf4j-simple than console captor can easily capture those logs. You can enable it by adding the following imports to your build.gradle file:
testImplementation('io.github.hakky54:consolecaptor:1.0.2')
testImplementation('ch.qos.logback:logback-classic:1.2.11')
@Path("/hello")
public class GreetingResource {
Logger logger = LoggerFactory.getLogger(GreetingResource.class);
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
logger.info("Hello");
return "Hello RESTEasy";
}
}
import io.quarkus.test.junit.QuarkusTest;
import nl.altindag.console.ConsoleCaptor;
import org.acme.GreetingResource;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
class QuarkusTestTest {
@Test
void captureLogs() {
try(ConsoleCaptor consoleCaptor = new ConsoleCaptor()) {
new GreetingResource().hello();
List<String> standardOutput = consoleCaptor.getStandardOutput();
assertEquals(1, standardOutput.size());
assertTrue(standardOutput.get(0).contains("Hello"));
}
}
}
And the following properties file in the test resource: logback-test.xml
See below for the file content
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="TRACE">
<appender-ref ref="STDOUT" />
</root>
</configuration>
So my advise is to raise this issue at Quarkus and in the mean while you can try using ConsoleCaptor till Quarkus can provide a solution. Hope this alternative solution is something which is useful for you.
from log-captor.
Related Issues (20)
- Expose MDC logback property map in LogEvent HOT 4
- Add existing contributors HOT 11
- ConcurrentModificationException in getLogs() HOT 7
- Logcaptor error logs do not contain expected content when reusing instance HOT 7
- log4j HOT 1
- Running the unit test directly from IDEA gives error HOT 7
- Not working with org.jacoco:jacoco-maven-plugin HOT 6
- LogEvent should implement toString() HOT 4
- Not compatible with log4j2 2.17.2 HOT 5
- Allow to retrieve logs for custom log levels HOT 2
- consoleCaptor.getStandardOutput() doesn't work HOT 6
- Does this project target Log4J API 1.7? HOT 9
- spring-boot-starter + log-captor: log4j-slf4j-impl cannot be present with log4j-to-slf4j HOT 5
- Upgrade to 2.8.0 HOT 18
- Suppressing Console Output HOT 3
- Are we supposed to close LogCaptor at the end of a test? HOT 3
- Issue with custom levels HOT 3
- Seeing DEBUG level logs after adding logcaptor dependency HOT 2
- disableConsoleOutput() is throwing NPE HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from log-captor.