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.core.lookup; 018 019import java.io.File; 020import java.net.URI; 021import java.net.URISyntaxException; 022import java.net.URL; 023 024import org.apache.logging.log4j.core.LogEvent; 025import org.apache.logging.log4j.core.config.ConfigurationSource; 026import org.apache.logging.log4j.core.config.plugins.Plugin; 027import org.apache.logging.log4j.status.StatusLogger; 028 029/** 030 * Lookup properties of Log4j 031 */ 032@Plugin(name = "log4j", category = StrLookup.CATEGORY) 033public class Log4jLookup extends AbstractConfigurationAwareLookup { 034 035 public final static String KEY_CONFIG_LOCATION = "configLocation"; 036 public final static String KEY_CONFIG_PARENT_LOCATION = "configParentLocation"; 037 038 private static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger(); 039 040 private static String asPath(final URI uri) { 041 if (uri.getScheme() == null || uri.getScheme().equals("file")) { 042 return uri.getPath(); 043 } 044 return uri.toString(); 045 } 046 047 private static URI getParent(final URI uri) throws URISyntaxException { 048 final String s = uri.toString(); 049 final int offset = s.lastIndexOf('/'); 050 if (offset > -1) { 051 return new URI(s.substring(0, offset)); 052 } 053 return new URI("../"); 054 } 055 056 @Override 057 public String lookup(final LogEvent event, final String key) { 058 if (configuration != null) { 059 final ConfigurationSource configSrc = configuration.getConfigurationSource(); 060 final File file = configSrc.getFile(); 061 if (file != null) { 062 switch (key) { 063 case KEY_CONFIG_LOCATION: 064 return file.getAbsolutePath(); 065 066 case KEY_CONFIG_PARENT_LOCATION: 067 return file.getParentFile().getAbsolutePath(); 068 069 default: 070 return null; 071 } 072 } 073 074 final URL url = configSrc.getURL(); 075 if (url != null) { 076 try { 077 switch (key) { 078 case KEY_CONFIG_LOCATION: 079 return asPath(url.toURI()); 080 081 case KEY_CONFIG_PARENT_LOCATION: 082 return asPath(getParent(url.toURI())); 083 084 default: 085 return null; 086 } 087 } catch (final URISyntaxException use) { 088 LOGGER.error(use); 089 return null; 090 } 091 } 092 } 093 094 return null; 095 } 096}