2

I have executed automated test cases with multithreads, managed by maven-surefire-plugin. Each class has their own thread where it is executing and the log4j2 is configured with bufferIO = true and immediateFlush= true. In the output file, all the threads are mixing the information. I know that log4j2 has this buffer and I would like to know if each thread has their own buffer and if I can read from it before the information is written into the output file.

I read other question related to this topic, but it hasn't arrived at a solution. And for that reason, I'm trying to search another path to follow.

Praveen
  • 1,791
  • 3
  • 20
  • 33
j.barrio
  • 1,006
  • 1
  • 13
  • 37
  • What are you trying to achieve? Why do you want to read from internal logging buffers? – Boris the Spider Sep 07 '18 at 08:05
  • Because the output log is mixed. I would like obtain the log of each thread separate – j.barrio Sep 07 '18 at 08:14
  • To obtain each test case output to review the errors more clearly – j.barrio Sep 07 '18 at 08:15
  • 1
    Just use tools to filter files by thread name or test case or whatever. Why overcomplicate matters? If you want to use an over engineered approach, just use a filtering appender to dump different logs into different files – Boris the Spider Sep 07 '18 at 08:18
  • I appreciate your comments and if you have a solution for this topic I propose you put an answer with filter in log4j2 or with grep if you know it. Thank you for your time. – j.barrio Sep 07 '18 at 08:49
  • 1
    If you want to run assertions against the log output, you are probably better off by not using a file appender, but configure a special in-memory appender that collects everything in a way that is convenient for your test case to inspect. Take a look at https://stackoverflow.com/questions/1827677/how-to-do-a-junit-assert-on-a-message-in-a-logger – Thilo Sep 07 '18 at 08:49

1 Answers1

1

The first thing I found is that JUnit5 is the cause of the mixed output log file, when we execute the test cases in multithreading way. When I used JUnit4 the default output, usually the console waits for the end of the test case to write.

In other hand the output file from log4j, when the user execute it with multithreding, always it shows mixing all the threads, regardless of the JUnit version.

Them, I add the thread id to the log4j layout pattern: %d{yyyy-MM-dd HH:mm:ss} %-5p- **[%tid]** %m%n

And after finish each thread I read the last lines of the output log4j file and I filter by the thread id number to obtain the relevant lines. With this point I had to solve other problem. How to obtain the absolute path of a dynamic log4j output log file? I test two solutions:

  1. By reflection (not recommended, but faster)
  2. Implement your own FileAppender to create a method to access to the fileName field

The two steps to follow the first option are this:

LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Appender appender = ctx.getConfiguration().getAppenders().get("file");

The appendervariable has the fileName field, but it is private.

j.barrio
  • 1,006
  • 1
  • 13
  • 37