Registering parallel threads in logback

I will try to make a brief description of my Selenium structure so that I can explain my problem.

I am using Selenium 2 (current version 2.3.1) + testNG 5.14

I installed the testng.xml file to run tests in the test suite in parallel, only 2 instances

For logging purposes, I use logback (I read this next best in the world of journals)

My problem is that when checking that the application logs, I get something like this:

18: 48: 58.551 [TestNG] INFO daastsetup.TestConfiguration - Retrieve a random user from the user pool

18: 48: 58.551 [TestNG] INFO daastsetup.TestConfiguration - Retrieve a random user from the user pool

18: 48: 58.551 [TestNG] DEBUG daastset.TestConfiguration - Creating a DataSource to access the DataBase

18: 48: 58.551 [TestNG] DEBUG daastset.TestConfiguration - Creating a DataSource to access the DataBase

18: 48: 58.552 [TestNG] DEBUG daastsetup.TestConfiguration - Running SQL Query

18: 48: 58.552 [TestNG] DEBUG daastsetup.TestConfiguration - Running SQL Query

18: 48: 59.613 [TestNG] TRACE daastsetup.TestConfiguration - Request successful

18: 48: 59.613 [TestNG] TRACE daastsetup.TestConfiguration - Request successful

As you can see, it is not possible to see the difference between two threads that work simultaneously. My question is: is there a way to configure the log settings so that they also add a number or identifier to identify each thread running?

PD Just in case, this can help, my logback.xml file looks like this:

<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>logs/selenium.log</file> <encoder> <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern> </encoder> </appender> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <root level="trace"> <appender-ref ref="FILE" /> <appender-ref ref="STDOUT" /> </root> </configuration> 

Thanks for the help =)

} {Panacea

+8
multithreading testng webdriver logback
source share
3 answers

The Diagnostic Context Magazine (MDC) is your friend. It allows you to add local stream variables that you can control, copy between streams, and write to the log using the logging format.

From the docs:

One of the goals of magazine design is to audit and debug complex distributed applications. Most distributed real-world systems must deal with multiple clients simultaneously. In a typical multi-threaded implementation of such a system, different clients will handle different threads. A possible but slightly discouraged approach for differentiating the output of one client’s journal from another is to create a new and separate registrar for each client. This method promotes the distribution of magazines and can increase their overhead.

A lighter technique consists of a unique embossing of each journal request serving this client. Neil Harrison described this method in the book Templates for Logging Journal Messages in Programming Template 3 Languages, edited by R. Martin, D. Riehl, and F. Bushmann (Addison-Wesley, 1997). Logback uses a variant of this method included in the SLF4J API: Mapped Diagnostic Contexts (MDC).

To uniquely stamp each request, the user places the contextual information in the MDC, the abbreviation for the context diagnostic context. The main parts of the MDC class are shown below. For a complete list of methods, refer to MDC javadocs .

+10
source share

FYI, you can display the stream identifier using% stream, as described in the documentation, at http://logback.qos.ch/manual/configuration.html :

 The output is formatted using a PatternLayoutEncoder set to the pattern %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n. 
+5
source share

If you want an alternative to the unpredictable names that you get with% thread, as usual, you can use simple local local identifiers. Its much easier on the eyes. This will work with logback ...

 public class ThreadIdConverter extends ClassicConverter { private static int nextId = 0; private static final ThreadLocal<String> threadId = new ThreadLocal<String>() { @Override protected String initialValue() { int nextId = nextId(); return String.format("%05d", nextId); } }; private static synchronized int nextId() { return ++nextId; } @Override public String convert(ILoggingEvent event) { return threadId.get(); } } 

Then put this simple line in your XML login:

 <conversionRule conversionWord="tid" converterClass="com.yourstuff.logback.ThreadIdConverter" /> 

Set your template something like this (note the "tid"):

 <pattern>%d{HH:mm:ss.SSS} [%tid] %-5level - %msg%n</pattern> 

And your logs will look like this:

 10:32:02.517 [00001] INFO something here 10:32:02.517 [00002] INFO something here 10:32:02.517 [00003] INFO something here 10:32:02.517 [00001] INFO something more here 10:32:02.517 [00001] INFO something more here 

You can do this with any registrar that supports custom extensions. Hope this helps.

+3
source share

All Articles