0

With std::thread, it is now trivial to keep a list of threads names in the program state (either by wrapping the thread, or by using a singleton to manage the thread list.)

I am starting to use log4cxx for my logging, and I'd like to have it display the thread name. However, this means I cannot use the PatternLayout. The thread numbers aren't very useful on their own.

What is the best way to do this? Am I stuck with just outputting a map, or can I override PatternLayout/ConversionPattern in a clever way to do what I want? And if I overload it, how do I point to the class in the config file? Or can I possibly add a conversion character?

Glen Nelson
  • 333
  • 2
  • 13

1 Answers1

1

Put your thread names using MDC before setting logger in your program

 MDC::put( "threadName", threadNameString);

Then at XML config

   <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%X{threadName} "/>
    </layout>

So at each log message your thread name will apear.

For more info check answer of this question: Add process id to log file name in log4cxx

Community
  • 1
  • 1
Novalis
  • 2,265
  • 6
  • 39
  • 63