Stephan
Stephan

Reputation: 8090

Log4j2 add word to every log call

So i have a java app that is launched from a central app the problem is that you can lunch many apps in the same time why causes the logs to be almost unreadable and my solution was just to add a app_instance_id to each call for the log but that means to modify way too many classes .

My question is : does log4j has a feature similar to this? or is there a better approach ?

PS: i do not what a separate file for every app instance

Upvotes: 0

Views: 913

Answers (1)

Joeri Hendrickx
Joeri Hendrickx

Reputation: 17445

Use the MDC. You can set your app-instance-id in the MDC at bootstrap time. Any threads that are spawned afterwards will inherit that value.

MDC.put("instanceId", instanceId);

Then, in your log4j pattern, specify %X{instanceId} to add the instanceId to every log line. This way, the log4j config will be the same for all your instances.

Upvotes: 1

Related Questions