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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With