log4j2如何根据配置的

时间:2022-02-21 21:53:13

log4j2如何根据配置的配置文件选取配置文件处理类的(ConfigurationFactory源码分析)

作者:honghailiang888

标签:plugin    osgi    serialized    log    listing    

123456789101112131415161718192021222324252627282930313233343536373839 <pre
code_snippet_id=
"1937793"
snippet_file_name=
"blog_20161019_7_4786896"
class
="java"
name=
"code"
deep=
"5">**
     * Locates all the plugins including search of specific packages. Warns about name collisions.     *     @param
packages the list of packages to scan 
for
plugins
     @since
2.1
     */    public
void
collectPlugins(final
List<String> packages) {
        final
String categoryLowerCase = category.toLowerCase();
        final
Map<String, PluginType<?>> newPlugins = 
new
LinkedHashMap<String, PluginType<?>>();
         // First, iterate the Log4j2Plugin.dat files found in the main CLASSPATH        Map<String, List<PluginType<?>>> builtInPlugins = PluginRegistry.getInstance().loadFromMainClassLoader();        if
(builtInPlugins.isEmpty()) {
            // If we didn't find any plugins above, someone must have messed with the log4j-core.jar.            // Search the standard package in the hopes we can find our core plugins.            builtInPlugins = PluginRegistry.getInstance().loadFromPackage(LOG4J_PACKAGES);        }        mergeByName(newPlugins, builtInPlugins.get(categoryLowerCase));         // Next, iterate any Log4j2Plugin.dat files from OSGi Bundles        for
(
final
Map<String, List<PluginType<?>>> pluginsByCategory : PluginRegistry.getInstance().getPluginsByCategoryByBundleId().values()) {
            mergeByName(newPlugins, pluginsByCategory.get(categoryLowerCase));        }         // Next iterate any packages passed to the static addPackage method.        for
(
final
String pkg : PACKAGES) {
            mergeByName(newPlugins, PluginRegistry.getInstance().loadFromPackage(pkg).get(categoryLowerCase));        }        // Finally iterate any packages provided in the configuration (note these can be changed at runtime).        if
(packages != 
null) {
            for
(
final
String pkg : packages) {
                mergeByName(newPlugins, PluginRegistry.getInstance().loadFromPackage(pkg).get(categoryLowerCase));            }        }         LOGGER.debug("PluginManager '{}' found {} plugins", category, newPlugins.size());         plugins = newPlugins;    }</pre>