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 */ 017package 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 021import jakarta.servlet.ServletContext; 022 023import org.apache.logging.log4j.core.LogEvent; 024import org.apache.logging.log4j.core.config.plugins.Plugin; 025import org.apache.logging.log4j.core.lookup.AbstractLookup; 026import org.apache.logging.log4j.util.Strings; 027 028@Plugin(name = "web", category = "Lookup") 029public class WebLookup extends AbstractLookup { 030 private static final String ATTR_PREFIX = "attr."; 031 private static final String INIT_PARAM_PREFIX = "initParam."; 032 033 @Override 034 public String lookup(final LogEvent event, final String key) { 035 final ServletContext ctx = WebLoggerContextUtils.getServletContext(); 036 if (ctx == null) { 037 return null; 038 } 039 040 if (key.startsWith(ATTR_PREFIX)) { 041 final String attrName = key.substring(ATTR_PREFIX.length()); 042 final Object attrValue = ctx.getAttribute(attrName); 043 return attrValue == null ? null : attrValue.toString(); 044 } 045 046 if (key.startsWith(INIT_PARAM_PREFIX)) { 047 final String paramName = key.substring(INIT_PARAM_PREFIX.length()); 048 return ctx.getInitParameter(paramName); 049 } 050 051 if ("rootDir".equals(key)) { 052 final String root = ctx.getRealPath("/"); 053 if (root == null) { 054 final String msg = "Failed to resolve web:rootDir -- " + 055 "servlet container unable to translate virtual path " + 056 " to real path (probably not deployed as exploded"; 057 throw new IllegalStateException(msg); 058 } 059 return root; 060 } 061 062 if ("contextPathName".equals(key)) { 063 String path = ctx.getContextPath(); 064 if (path.trim().contains("/")) { 065 String[] fields = path.split("/"); 066 for (String field : fields) { 067 if (field.length() > 0) { 068 return field; 069 } 070 } 071 return null; 072 } 073 return ctx.getContextPath(); 074 } 075 076 if ("contextPath".equals(key)) { 077 return ctx.getContextPath(); 078 } 079 080 if ("servletContextName".equals(key)) { 081 return ctx.getServletContextName(); 082 } 083 084 if ("serverInfo".equals(key)) { 085 return ctx.getServerInfo(); 086 } 087 088 if ("effectiveMajorVersion".equals(key)) { 089 return String.valueOf(ctx.getEffectiveMajorVersion()); 090 } 091 092 if ("effectiveMinorVersion".equals(key)) { 093 return String.valueOf(ctx.getEffectiveMinorVersion()); 094 } 095 096 if ("majorVersion".equals(key)) { 097 return String.valueOf(ctx.getMajorVersion()); 098 } 099 100 if ("minorVersion".equals(key)) { 101 return String.valueOf(ctx.getMinorVersion()); 102 } 103 104 if (ctx.getAttribute(key) != null) { 105 return ctx.getAttribute(key).toString(); 106 } 107 108 if (ctx.getInitParameter(key) != null) { 109 return ctx.getInitParameter(key); 110 } 111 112 ctx.log(getClass().getName() + " unable to resolve key " + Strings.quote(key)); 113 return null; 114 } 115}