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.config; 018 019import java.util.List; 020import java.util.Map; 021 022import org.apache.logging.log4j.Level; 023import org.apache.logging.log4j.core.Appender; 024import org.apache.logging.log4j.core.Filter; 025import org.apache.logging.log4j.core.LogEvent; 026import org.apache.logging.log4j.core.Logger; 027import org.apache.logging.log4j.core.LoggerContext; 028import org.apache.logging.log4j.core.async.AsyncLoggerConfigDelegate; 029import org.apache.logging.log4j.core.filter.Filterable; 030import org.apache.logging.log4j.core.lookup.ConfigurationStrSubstitutor; 031import org.apache.logging.log4j.core.lookup.StrSubstitutor; 032import org.apache.logging.log4j.core.net.Advertiser; 033import org.apache.logging.log4j.core.script.ScriptManager; 034import org.apache.logging.log4j.core.util.NanoClock; 035import org.apache.logging.log4j.core.util.WatchManager; 036 037/** 038 * Interface that must be implemented to create a configuration. 039 * <p> 040 * Custom implementations are recommended to extend {@link AbstractConfiguration}. 041 * </p> 042 * 043 * @see AbstractConfiguration 044 * @see org.apache.logging.log4j.core.LifeCycle2 045 */ 046public interface Configuration extends Filterable { 047 048 /** Key for storing the Context properties. */ 049 String CONTEXT_PROPERTIES = "ContextProperties"; 050 051 /** 052 * Returns the configuration name. 053 * 054 * @return the name of the configuration. 055 */ 056 String getName(); 057 058 /** 059 * Locates the appropriate LoggerConfig for a Logger name. This will remove tokens from the package name as 060 * necessary or return the root LoggerConfig if no other matches were found. 061 * 062 * @param name The Logger name. 063 * @return The located LoggerConfig. 064 */ 065 LoggerConfig getLoggerConfig(String name); 066 067 /** 068 * Returns the Appender with the specified name. 069 * 070 * @param <T> The expected Appender type. 071 * @param name The name of the Appender. 072 * @return the Appender with the specified name or null if the Appender cannot be located. 073 */ 074 <T extends Appender> T getAppender(String name); 075 076 /** 077 * Returns a Map containing all the Appenders and their name. 078 * 079 * @return A Map containing each Appender's name and the Appender object. 080 */ 081 Map<String, Appender> getAppenders(); 082 083 void addAppender(final Appender appender); 084 085 Map<String, LoggerConfig> getLoggers(); 086 087 void addLoggerAppender(Logger logger, Appender appender); 088 089 void addLoggerFilter(Logger logger, Filter filter); 090 091 void setLoggerAdditive(Logger logger, boolean additive); 092 093 void addLogger(final String name, final LoggerConfig loggerConfig); 094 095 void removeLogger(final String name); 096 097 /** 098 * Returns the list of packages to scan for plugins for this Configuration. 099 * 100 * @return the list of plugin packages. 101 * @since 2.1 102 */ 103 List<String> getPluginPackages(); 104 105 Map<String, String> getProperties(); 106 107 /** 108 * Returns the root Logger. 109 * 110 * @return the root Logger. 111 */ 112 LoggerConfig getRootLogger(); 113 114 void addListener(ConfigurationListener listener); 115 116 void removeListener(ConfigurationListener listener); 117 118 StrSubstitutor getStrSubstitutor(); 119 120 default StrSubstitutor getConfigurationStrSubstitutor() { 121 StrSubstitutor defaultSubstitutor = getStrSubstitutor(); 122 if (defaultSubstitutor == null) { 123 return new ConfigurationStrSubstitutor(); 124 } 125 return new ConfigurationStrSubstitutor(defaultSubstitutor); 126 } 127 128 void createConfiguration(Node node, LogEvent event); 129 130 <T> T getComponent(String name); 131 132 void addComponent(String name, Object object); 133 134 void setAdvertiser(Advertiser advertiser); 135 136 Advertiser getAdvertiser(); 137 138 boolean isShutdownHookEnabled(); 139 140 long getShutdownTimeoutMillis(); 141 142 ConfigurationScheduler getScheduler(); 143 144 /** 145 * Returns the source of this configuration. 146 * 147 * @return the source of this configuration, never {@code null}, but may be 148 * {@link org.apache.logging.log4j.core.config.ConfigurationSource#NULL_SOURCE} 149 * or 150 * {@link org.apache.logging.log4j.core.config.ConfigurationSource#COMPOSITE_SOURCE} 151 */ 152 ConfigurationSource getConfigurationSource(); 153 154 /** 155 * <p> 156 * Returns a list of descriptors of the custom levels defined in the current configuration. The returned list does 157 * <em>not</em> include custom levels that are defined in code with direct calls to {@link Level#forName(String, int)}. 158 * </p> 159 * <p> 160 * Note that the list does not include levels of previous configurations. For example, suppose a configuration 161 * contains custom levels A, B and C. The configuration is then modified to contain custom levels B, C and D. For 162 * the new configuration, this method will return only {B, C, D}, that is, only the custom levels defined in 163 * <em>this</em> configuration. The previously defined level A still exists (and can be obtained with 164 * {@link Level#getLevel(String)}), it is just not in the current configuration. {@link Level#values()} will return 165 * {A, B, C, D and the built-in levels}. 166 * </p> 167 * 168 * @return the custom levels defined in the current configuration 169 */ 170 List<CustomLevelConfig> getCustomLevels(); 171 172 ScriptManager getScriptManager(); 173 174 /** 175 * Returns the {@code AsyncLoggerConfigDelegate} shared by all 176 * {@code AsyncLoggerConfig} instances defined in this Configuration. 177 * 178 * @return the {@code AsyncLoggerConfigDelegate} 179 */ 180 AsyncLoggerConfigDelegate getAsyncLoggerConfigDelegate(); 181 182 /** 183 * Return the WatchManager. 184 * 185 * @return the WatchManager. 186 */ 187 WatchManager getWatchManager(); 188 189 /* 190 * (non-Javadoc) 191 * 192 * @see 193 * org.apache.logging.log4j.core.config.ReliabilityStrategyFactory#getReliabilityStrategy(org.apache.logging.log4j 194 * .core.config.LoggerConfig) 195 */ 196 197 ReliabilityStrategy getReliabilityStrategy(LoggerConfig loggerConfig); 198 199 /** 200 * Returns the {@link NanoClock} instance for this configuration. 201 * 202 * @return the nano clock 203 */ 204 NanoClock getNanoClock(); 205 206 /** 207 * Sets the {@link NanoClock} instance for this configuration. 208 * 209 * @param nanoClock the new nano clock for this configuration. Must be non-null. 210 */ 211 void setNanoClock(NanoClock nanoClock); 212 213 /** 214 * Gets the logger context. 215 * 216 * @return the logger context. 217 */ 218 LoggerContext getLoggerContext(); 219}