CanardMoussant
CanardMoussant

Reputation: 923

Add global custom values to Play Framework logger

I have a cluster of different Akka actors, all using logback as logger. In the pure Akka actors startup, I can do this during the app initialization:

MDC.put("role", role)

role being a string representing the process main role (like "worker"), and all the logs will have this additional context values, helping the investigation.

One of the role is a frontend and uses Play framework to publish a REST API. In that case, I do not define an object extending App, and I do not know how/where to set global values like that, so that all logs emitted in the play application are marked with the role (and other additional things I want to put).

Upvotes: 1

Views: 568

Answers (1)

Will Sargent
Will Sargent

Reputation: 4396

Play is a multi threaded application, so using MDC here is not going to work effectively. The best thing you can do is use the SLF4J marker API, which can be passed between threads.

Play 2.6.x will have support for the Marker API directly, but in the mean time you should use SLF4J directly to leverage the Logstash Logback Encoder to create a rich Marker that contains your role and other information.

import static net.logstash.logback.marker.Markers.*

Marker logstashMarker = append("name", "value");
private val logger = org.slf4j.LoggerFactory.getLogger(this.getClass)
logger.debug(logstashMarker, "My message")

Then you can pass logstashMarker as an implicit parameter to your methods, without worrying about thread local information.

Note that Play handles requests and so any "global" information you have in Akka that you want in Play will have to be extracted and added -- for maximum convenience you can put that information in a WrappedRequest using action composition or by adding a filter.

Upvotes: 1

Related Questions