001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017 package org.apache.logging.log4j.web; 018 // Please note that if you move this class, make sure to update the Interpolator class (if still applicable) or remove 019 // this comment if no longer relevant 020 021 import javax.servlet.ServletContext; 022 023 import org.apache.logging.log4j.core.LogEvent; 024 import org.apache.logging.log4j.core.config.plugins.Plugin; 025 import org.apache.logging.log4j.core.lookup.AbstractLookup; 026 import org.apache.logging.log4j.util.Strings; 027 028 @Plugin(name = "web", category = "Lookup") 029 public class WebLookup extends AbstractLookup { 030 private static final String ATTR_PREFIX = "attr."; 031 private static final String INIT_PARAM_PREFIX = "initParam."; 032 033 /** 034 * @deprecated Use {@link WebLoggerContextUtils#getServletContext()}. 035 */ 036 @Deprecated 037 protected ServletContext getServletContext() { 038 return WebLoggerContextUtils.getServletContext(); 039 } 040 041 @Override 042 public String lookup(final LogEvent event, final String key) { 043 final ServletContext ctx = WebLoggerContextUtils.getServletContext(); 044 if (ctx == null) { 045 return null; 046 } 047 048 if (key.startsWith(ATTR_PREFIX)) { 049 final String attrName = key.substring(ATTR_PREFIX.length()); 050 final Object attrValue = ctx.getAttribute(attrName); 051 return attrValue == null ? null : attrValue.toString(); 052 } 053 054 if (key.startsWith(INIT_PARAM_PREFIX)) { 055 final String paramName = key.substring(INIT_PARAM_PREFIX.length()); 056 return ctx.getInitParameter(paramName); 057 } 058 059 if ("rootDir".equals(key)) { 060 final String root = ctx.getRealPath("/"); 061 if (root == null) { 062 final String msg = "Failed to resolve web:rootDir -- " + 063 "servlet container unable to translate virtual path " + 064 " to real path (probably not deployed as exploded"; 065 throw new IllegalStateException(msg); 066 } 067 return root; 068 } 069 070 if ("contextPath".equals(key)) { 071 return ctx.getContextPath(); 072 } 073 074 if ("servletContextName".equals(key)) { 075 return ctx.getServletContextName(); 076 } 077 078 if ("serverInfo".equals(key)) { 079 return ctx.getServerInfo(); 080 } 081 082 if ("effectiveMajorVersion".equals(key)) { 083 return String.valueOf(ctx.getEffectiveMajorVersion()); 084 } 085 086 if ("effectiveMinorVersion".equals(key)) { 087 return String.valueOf(ctx.getEffectiveMinorVersion()); 088 } 089 090 if ("majorVersion".equals(key)) { 091 return String.valueOf(ctx.getMajorVersion()); 092 } 093 094 if ("minorVersion".equals(key)) { 095 return String.valueOf(ctx.getMinorVersion()); 096 } 097 098 if (ctx.getAttribute(key) != null) { 099 return ctx.getAttribute(key).toString(); 100 } 101 102 if (ctx.getInitParameter(key) != null) { 103 return ctx.getInitParameter(key); 104 } 105 106 ctx.log(getClass().getName() + " unable to resolve key " + Strings.quote(key)); 107 return null; 108 } 109 }