Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a prefix to log4j messages (at the object level)

Tags:

java

log4j

log4j2

I use log4j2 and I would like to add a prefix to all my messages. This prefix is passed to the constructor parameter and it depends on the instance of the class. So we're at the object level (not class or thread).

For example, I have an A class instantiated like new A(152), so when I use log.error("message") on this class, 152: is written just before the message. For new A(155), 155: will be displayed instead.

Thanks for your help

like image 761
Lucie Avatar asked May 23 '13 20:05

Lucie


1 Answers

Use MDC to achive this

In your constructor put

 MDC.put("prefix", yourvalue);

and in your XML use it like this in pattern

      %X{prefix}
like image 84
NullPointerException Avatar answered Sep 20 '22 15:09

NullPointerException