Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

logging - Change of java Thread name is not reflecting in log4j2 logs

I have Java based application with a single thread connected to JBOSS MQ. Whenever there is a message in the queue then it pulls the message and sets the thread name with one of the message's attribute (transactionid) and continues processing. After application startup, the logging is fine for the first message. But from the second message, the change in the thread name is not reflecting in the logs.

Example code:

private static Logger logger = LogManager.getLogger(EngineManager.class.getName());

private synchronized void onMessage(javax.jms.Message msg){

Thread.currentThread().setName(msg.getStringProperty("REQUEST_ID"));
logger.info("Received a Message Event: Time spent:" + (System.currentTimeMillis()-msg.getJMSTimestamp()) + "ms");
...

}

log4j2.properties:
...
appender.console.layout.pattern = %d{ABSOLUTE} %c{1} [%t] - %m%n
...

Output:

19:29:52,726 EngineManager [sddsdsdgdd-dfvdddbfvs-2343412] - Received a Message Event: Time spent: 44ms

Rest everything is fine that's why I have given small portion of the code. What I need is when there is a second message (so on) in the queue then above transactionid (the one in []) should change in the logs. It is always same for all the messages. I tried different ways like LoggerContext.reconfigure(), LogManager.shutdowm() and then getLogger etc but couldn't succeed. Please help.

This used to work fine with log4j 1x where the change in thread name immediately reflects in the logs.

question from:https://stackoverflow.com/questions/65939472/change-of-java-thread-name-is-not-reflecting-in-log4j2-logs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...