I have a container running in GKE which hosts a scala application. Across our stack, we're trying to improve debugging, and I'm trying to get our deployments to output structured logs. I've already achieved this for the python parts of our stack, but I'm having trouble with scala.
In scala we are currently using slf4j and logback to log messages. I've already done some configuration work to have logback format the output as single line json, but now I'm struggling to actually get it to pick up on custom fields.
In python I can just do something like this:
custom_fields = { "userID": "andy" }
logger.info("User logged in", custom_fields)
I've found a few examples of how to do it in scala, but I don't think they're suitable:
Here is an example using MDC, my concerns are that I don't think this is thread safe? I have multiple threads running asynchronously that are processing multiple users at once. I could wrap all my log calls in a mutex, and add/log/remove the field in the mutex, but this feels very heavy handed and like it could mess up the performance of the system: https://gquintana.github.io/2017/12/01/Structured-logging-with-SL-FJ-and-Logback.html
I've also seen examples of passing StructuredArgument to the log call: https://github.com/logstash/logstash-logback-encoder#event-specific-custom-fields
But as detailed in my previous question: How to import StructuredArgument for structured logging in scala using slf4j and logback
I cannot seem to get this working in Scala.
What's the best way to go about implementing this?