1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.web;
18
19
20
21 import javax.servlet.ServletContext;
22
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.core.LogEvent;
25 import org.apache.logging.log4j.core.LoggerContext;
26 import org.apache.logging.log4j.core.config.plugins.Plugin;
27 import org.apache.logging.log4j.core.impl.ContextAnchor;
28 import org.apache.logging.log4j.core.lookup.StrLookup;
29
30
31 @Plugin(name = "web", category = "Lookup")
32 public class WebLookup implements StrLookup {
33 private static final String ATTR_PREFIX = "attr.";
34 private static final String INIT_PARAM_PREFIX = "initParam.";
35
36 protected ServletContext getServletContext() {
37 LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get();
38 if (lc == null) {
39 lc = (LoggerContext) LogManager.getContext(false);
40 }
41 if (lc != null) {
42 final Object obj = lc.getExternalContext();
43 return obj != null && obj instanceof ServletContext ? (ServletContext) obj : null;
44 }
45 return null;
46 }
47
48 @Override
49 public String lookup(final String key) {
50 final ServletContext ctx = getServletContext();
51 if (ctx == null) {
52 return null;
53 }
54
55 if (key.startsWith(ATTR_PREFIX)) {
56 final String attrName = key.substring(ATTR_PREFIX.length());
57 final Object attrValue = ctx.getAttribute(attrName);
58 return attrValue == null ? null : attrValue.toString();
59 }
60
61 if (key.startsWith(INIT_PARAM_PREFIX)) {
62 final String paramName = key.substring(INIT_PARAM_PREFIX.length());
63 return ctx.getInitParameter(paramName);
64 }
65
66 if ("rootDir".equals(key)) {
67 final String root = ctx.getRealPath("/");
68 if (root == null) {
69 final String msg = "Failed to resolve web:rootDir -- " +
70 "servlet container unable to translate virtual path " +
71 " to real path (probably not deployed as exploded";
72 throw new IllegalStateException(msg);
73 }
74 return root;
75 }
76
77 if ("contextPath".equals(key)) {
78 return ctx.getContextPath();
79 }
80
81 if ("servletContextName".equals(key)) {
82 return ctx.getServletContextName();
83 }
84
85 if ("serverInfo".equals(key)) {
86 return ctx.getServerInfo();
87 }
88
89 if ("effectiveMajorVersion".equals(key)) {
90 return String.valueOf(ctx.getEffectiveMajorVersion());
91 }
92
93 if ("effectiveMinorVersion".equals(key)) {
94 return String.valueOf(ctx.getEffectiveMinorVersion());
95 }
96
97 if ("majorVersion".equals(key)) {
98 return String.valueOf(ctx.getMajorVersion());
99 }
100
101 if ("minorVersion".equals(key)) {
102 return String.valueOf(ctx.getMinorVersion());
103 }
104
105 if (ctx.getAttribute(key) != null) {
106 return ctx.getAttribute(key).toString();
107 }
108
109 if (ctx.getInitParameter(key) != null) {
110 return ctx.getInitParameter(key);
111 }
112
113 ctx.log(getClass().getName() + " unable to resolve key '" + key + '\'');
114 return null;
115 }
116
117 @Override
118 public String lookup(final LogEvent event, final String key) {
119 return lookup(key);
120 }
121 }