2

I would like that each JUnit 5 test has its own log4j2 log file. This log file I would then like to attach to the Allure HTML test report.

Yesterday I tried two different approaches which I had googled, but it did not work. One was to use system properties in the log4j2 configuration file. But these are obviously initialized only once. Now that I write these lines, I wonder if there is a way to trigger reinitialization of the log configuration at runtime?

Then I tried a method where I programatically update the appenders. It was horribly complicated, deep inside internals of log4j2. And it did not work, some stream was already closed it said at the end, and then I gave up.

Before I try again, what would be a smart and safe way to do it?

2 Answers2

0

The solution in general would require from you to collect your output related to the current test somewhere and at the end of the test trigger Allure.getLifecycle().addAttachment().

We have a lot of test logs spawning in parallel in our project so we are saving them in H2. After test finished we just get required logs per test unique id. I suppose it could be done in better way but it is solution that just works.

RocketRaccoon
  • 2,559
  • 1
  • 21
  • 30
  • The part of adding the logs as an Allure attachment has not been a problem to me. My question was how to achieve a log4j log file per JUnit 5 test case. That part is the tricky thing for which I ask for advice. – TicTacFreshMint Sep 04 '18 at 10:54
  • There is now easy way except manipulating with aspects and Allure internals. Please see related discussion https://github.com/allure-framework/allure-java/issues/18. Our way is to save logs in the in-memory database, and share them for different reporting systems when they need. – RocketRaccoon Sep 04 '18 at 11:01
0

I managed to attach a logfile per test. What helped me were these links:

https://stackoverflow.com/a/43404124

https://github.com/sercasti/Log4j-RewriteAppender

In log4j2.xml I use the routing appender and inside it the file appender with fileName=¨target/logs/${ctx:testId}.log¨.

The tricky thing was to take care of the log events which have no ${ctx:testId} which is why I had to use the Rewrite appender in order to add it on the fly.

In JUnit 5 I implemented a BeforeTestExecutionCallback in which I do ¨ThreadContext.put(...)¨. To attach the existing log files per test to Allure is then the easier part. The log files which do not apply to a specific test I add to the Jenkins build result.

The other idea was to publish log information into Kibana and to add a link to this information in the Allure report, but I did not choose this way.

I hope this information helps someone who wants to do the same, as a starting point.