perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
Apache License 2.0
9.63k stars 1.56k forks source link

Logging with Log4j+MDC? #1095

Open zoell opened 5 years ago

zoell commented 5 years ago

Hi,

I am looking for a way to add the Session ID to every log4j log entry.

Currently I am trying the MDC like this: <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %X{sessionId} %c:%L - %m%n" />

And in my code:

post("/process", (request, response) -> {
    response.type("application/json");

    MDC.put("sessionId", request.session().id());

    logger.debug("process request"));

    DoSomethings();

    return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS));
});

In my DoSomething() I do many kind of async processing but in that class it seems this session Id is mixed across sessions, like it becomes a global variable.

What is the practical way to do this with Spark?

Thanks, zoell