Reputation: 9956
I want to have a separate log file for each application while using just one logback.xml
file. Therefore I'm using a SiftingAppender.
Every application has it's own WebApplicationInitializer that assigns a specific MDC.
When I start my Tomcat server all the log files are split as they should.
But when I send a request to the server the logging output gets written to the unknown file. Obviously the MDC value can't be read.
As far as I know MDC values are copied from the parent thread to the child thread. Where is each request's thread created so I can assign the MDC value there?
Upvotes: 0
Views: 1607
Reputation: 3589
Although classic servlet containers like Tomcat use a Thread-Per-Request model, you cannot rely on any mapping of a single thread to a single web-application. This is because requests are serviced from a pool of container-managed threads, e.g. imagine a thread pool with 3 threads A, B and C. These service requests, which alternate between two webapps W1 and W2 in a round robin fashion:
request 1 on W1 serviced by A
request 2 on W2 serviced by B
request 3 on W1 serviced by C
request 4 on W2 serviced by A
...
As you can see, Thread A first handled a request to webapp W1 and was later reused for a request to webapp W2. Therefore you cannot assign a web-app id to a thread once and be done with it. Instead, on every single request, you have to
Both of these tasks can for example be fulfilled by a Servlet Filter. The logback documentation also explains this in the section "MDC and Managed Threads". There is even a sample filter implementation available, which you can modify to fit your needs.
Upvotes: 2