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