1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.selector.servlet;
15
16 import java.io.IOException;
17
18 import jakarta.servlet.Filter;
19 import jakarta.servlet.FilterChain;
20 import jakarta.servlet.FilterConfig;
21 import jakarta.servlet.ServletException;
22 import jakarta.servlet.ServletRequest;
23 import jakarta.servlet.ServletResponse;
24
25 import org.slf4j.LoggerFactory;
26
27 import ch.qos.logback.classic.LoggerContext;
28 import ch.qos.logback.classic.selector.ContextJNDISelector;
29 import ch.qos.logback.classic.selector.ContextSelector;
30 import ch.qos.logback.classic.util.ContextSelectorStaticBinder;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class LoggerContextFilter implements Filter {
47
48 public void destroy() {
49
50 }
51
52 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
53 throws IOException, ServletException {
54
55 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
56 ContextSelector selector = ContextSelectorStaticBinder.getSingleton().getContextSelector();
57 ContextJNDISelector sel = null;
58
59 if (selector instanceof ContextJNDISelector) {
60 sel = (ContextJNDISelector) selector;
61 sel.setLocalContext(lc);
62 }
63
64 try {
65 chain.doFilter(request, response);
66 } finally {
67 if (sel != null) {
68 sel.removeLocalContext();
69 }
70 }
71 }
72
73 public void init(FilterConfig arg0) throws ServletException {
74
75 }
76 }