Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a MDC-like variable in Logback, but for the entire app/JVM?

Tags:

I'd like to have an indicator in my log file for which app logs something. This would mean a variable that's the same for the entire app, but different between instances of the app. I tried to use MDC for this, hoping that if I set it directly in the main method, the variable would be taken over by newly spawned threads. This used to work, more or less, using log4j, but it doesn't work at all with logback. I know you can show system variables in the log file, but there might be several instances of the app running on the same machine, so that won't work. So, how should I send this variable to my log file?

like image 658
Jorn Avatar asked Feb 08 '18 12:02

Jorn


1 Answers

I would use a JVM System Property. You can set them per process when the JVM is launched with a flag -Dthings.that.arent.bacon=turkey-bacon, or programmatically: System.setProperty("no.matter.what.they.tell.you", "turkey bacon is not bacon");.

It looks like this is already supportted in LogBack if you're using SLF4J/logback: https://logback.qos.ch/manual/layouts.html

like image 115
Jonathan S. Fisher Avatar answered Oct 03 '22 18:10

Jonathan S. Fisher