001/*
002 * Copyright 2015 Apache Software Foundation.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.apache.logging.log4j.core.lookup;
017
018import java.io.File;
019import java.net.URI;
020import java.net.URISyntaxException;
021import java.net.URL;
022
023import org.apache.logging.log4j.core.LogEvent;
024import org.apache.logging.log4j.core.LoggerContext;
025import org.apache.logging.log4j.core.config.ConfigurationSource;
026import org.apache.logging.log4j.core.config.plugins.Plugin;
027import org.apache.logging.log4j.core.impl.ContextAnchor;
028import org.apache.logging.log4j.status.StatusLogger;
029
030/**
031 * Lookup properties of Log4j
032 */
033@Plugin(name = "log4j", category = StrLookup.CATEGORY)
034public class Log4jLookup extends AbstractLookup {
035
036    public final static String KEY_CONFIG_LOCATION = "configLocation";
037    public final static String KEY_CONFIG_PARENT_LOCATION = "configParentLocation";
038
039    private static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();
040
041    private static String asPath(final URI uri) {
042        if (uri.getScheme() == null || uri.getScheme().equals("file")) {
043            return uri.getPath();
044        }
045        return uri.toString();
046    }
047
048    private static URI getParent(final URI uri) throws URISyntaxException {
049        final String s = uri.toString();
050        final int offset = s.lastIndexOf('/');
051        if (offset > -1) {
052            return new URI(s.substring(0, offset));
053        }
054        return new URI("../");
055    }
056
057    @Override
058    public String lookup(final LogEvent event, final String key) {
059        final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
060        if (ctx == null) {
061            return null;
062        }
063        final ConfigurationSource configSrc = ctx.getConfiguration().getConfigurationSource();
064        final File file = configSrc.getFile();
065        if (file != null) {
066            switch (key) {
067            case KEY_CONFIG_LOCATION:
068                return file.getAbsolutePath();
069
070            case KEY_CONFIG_PARENT_LOCATION:
071                return file.getParentFile().getAbsolutePath();
072
073            default:
074                return null;
075            }
076        }
077
078        final URL url = configSrc.getURL();
079        try {
080            switch (key) {
081            case KEY_CONFIG_LOCATION:
082                return asPath(url.toURI());
083
084            case KEY_CONFIG_PARENT_LOCATION:
085                return asPath(getParent(url.toURI()));
086
087            default:
088                return null;
089            }
090        } catch (final URISyntaxException use) {
091            LOGGER.error(use);
092            return null;
093        }
094    }
095}