Search in sources :

Example 1 with PluginEntry

use of org.apache.logging.log4j.core.config.plugins.processor.PluginEntry in project logging-log4j2 by apache.

the class PluginRegistry method decodeCacheFiles.

private Map<String, List<PluginType<?>>> decodeCacheFiles(final ClassLoader loader) {
    final long startTime = System.nanoTime();
    final PluginCache cache = new PluginCache();
    try {
        final Enumeration<URL> resources = loader.getResources(PluginProcessor.PLUGIN_CACHE_FILE);
        if (resources == null) {
            LOGGER.info("Plugin preloads not available from class loader {}", loader);
        } else {
            cache.loadCacheFiles(resources);
        }
    } catch (final IOException ioe) {
        LOGGER.warn("Unable to preload plugins", ioe);
    }
    final Map<String, List<PluginType<?>>> newPluginsByCategory = new HashMap<>();
    int pluginCount = 0;
    for (final Map.Entry<String, Map<String, PluginEntry>> outer : cache.getAllCategories().entrySet()) {
        final String categoryLowerCase = outer.getKey();
        final List<PluginType<?>> types = new ArrayList<>(outer.getValue().size());
        newPluginsByCategory.put(categoryLowerCase, types);
        for (final Map.Entry<String, PluginEntry> inner : outer.getValue().entrySet()) {
            final PluginEntry entry = inner.getValue();
            final String className = entry.getClassName();
            try {
                final Class<?> clazz = loader.loadClass(className);
                final PluginType<?> type = new PluginType<>(entry, clazz, entry.getName());
                types.add(type);
                ++pluginCount;
            } catch (final ClassNotFoundException e) {
                LOGGER.info("Plugin [{}] could not be loaded due to missing classes.", className, e);
            } catch (final VerifyError e) {
                LOGGER.info("Plugin [{}] could not be loaded due to verification error.", className, e);
            }
        }
    }
    final long endTime = System.nanoTime();
    final DecimalFormat numFormat = new DecimalFormat("#0.000000");
    final double seconds = (endTime - startTime) * 1e-9;
    LOGGER.debug("Took {} seconds to load {} plugins from {}", numFormat.format(seconds), pluginCount, loader);
    return newPluginsByCategory;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) PluginEntry(org.apache.logging.log4j.core.config.plugins.processor.PluginEntry) IOException(java.io.IOException) URL(java.net.URL) ArrayList(java.util.ArrayList) List(java.util.List) PluginCache(org.apache.logging.log4j.core.config.plugins.processor.PluginCache) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 2 with PluginEntry

use of org.apache.logging.log4j.core.config.plugins.processor.PluginEntry in project logging-log4j2 by apache.

the class PluginRegistry method loadFromPackage.

/**
     * @since 2.1
     */
public Map<String, List<PluginType<?>>> loadFromPackage(final String pkg) {
    if (Strings.isBlank(pkg)) {
        // happens when splitting an empty string
        return Collections.emptyMap();
    }
    Map<String, List<PluginType<?>>> existing = pluginsByCategoryByPackage.get(pkg);
    if (existing != null) {
        // already loaded this package
        return existing;
    }
    final long startTime = System.nanoTime();
    final ResolverUtil resolver = new ResolverUtil();
    final ClassLoader classLoader = Loader.getClassLoader();
    if (classLoader != null) {
        resolver.setClassLoader(classLoader);
    }
    resolver.findInPackage(new PluginTest(), pkg);
    final Map<String, List<PluginType<?>>> newPluginsByCategory = new HashMap<>();
    for (final Class<?> clazz : resolver.getClasses()) {
        final Plugin plugin = clazz.getAnnotation(Plugin.class);
        final String categoryLowerCase = plugin.category().toLowerCase();
        List<PluginType<?>> list = newPluginsByCategory.get(categoryLowerCase);
        if (list == null) {
            newPluginsByCategory.put(categoryLowerCase, list = new ArrayList<>());
        }
        final PluginEntry mainEntry = new PluginEntry();
        final String mainElementName = plugin.elementType().equals(Plugin.EMPTY) ? plugin.name() : plugin.elementType();
        mainEntry.setKey(plugin.name().toLowerCase());
        mainEntry.setName(plugin.name());
        mainEntry.setCategory(plugin.category());
        mainEntry.setClassName(clazz.getName());
        mainEntry.setPrintable(plugin.printObject());
        mainEntry.setDefer(plugin.deferChildren());
        final PluginType<?> mainType = new PluginType<>(mainEntry, clazz, mainElementName);
        list.add(mainType);
        final PluginAliases pluginAliases = clazz.getAnnotation(PluginAliases.class);
        if (pluginAliases != null) {
            for (final String alias : pluginAliases.value()) {
                final PluginEntry aliasEntry = new PluginEntry();
                final String aliasElementName = plugin.elementType().equals(Plugin.EMPTY) ? alias.trim() : plugin.elementType();
                aliasEntry.setKey(alias.trim().toLowerCase());
                aliasEntry.setName(plugin.name());
                aliasEntry.setCategory(plugin.category());
                aliasEntry.setClassName(clazz.getName());
                aliasEntry.setPrintable(plugin.printObject());
                aliasEntry.setDefer(plugin.deferChildren());
                final PluginType<?> aliasType = new PluginType<>(aliasEntry, clazz, aliasElementName);
                list.add(aliasType);
            }
        }
    }
    final long endTime = System.nanoTime();
    final DecimalFormat numFormat = new DecimalFormat("#0.000000");
    final double seconds = (endTime - startTime) * 1e-9;
    LOGGER.debug("Took {} seconds to load {} plugins from package {}", numFormat.format(seconds), resolver.getClasses().size(), pkg);
    // Note multiple threads could be calling this method concurrently. Both will do the work,
    // but only one will be allowed to store the result in the outer map.
    // Return the inner map produced by whichever thread won the race, so all callers will get the same result.
    existing = pluginsByCategoryByPackage.putIfAbsent(pkg, newPluginsByCategory);
    if (existing != null) {
        return existing;
    }
    return newPluginsByCategory;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) PluginEntry(org.apache.logging.log4j.core.config.plugins.processor.PluginEntry) PluginAliases(org.apache.logging.log4j.core.config.plugins.PluginAliases) ArrayList(java.util.ArrayList) List(java.util.List) Plugin(org.apache.logging.log4j.core.config.plugins.Plugin)

Example 3 with PluginEntry

use of org.apache.logging.log4j.core.config.plugins.processor.PluginEntry in project hive by apache.

the class LogDivertAppender method registerRoutingAppender.

/**
 * Programmatically register a routing appender to Log4J configuration, which
 * automatically writes the log of each query to an individual file.
 * The equivalent property configuration is as follows:
 * # queryId based routing file appender
 *      appender.query-routing.type = Routing
 *      appender.query-routing.name = query-routing
 *      appender.query-routing.routes.type = Routes
 *      appender.query-routing.routes.pattern = $${ctx:queryId}
 *      # default route
 *      appender.query-routing.routes.route-default.type = Route
 *      appender.query-routing.routes.route-default.key = $${ctx:queryId}
 *      appender.query-routing.routes.route-default.app.type = null
 *      appender.query-routing.routes.route-default.app.name = Null
 *      # queryId based route
 *      appender.query-routing.routes.route-mdc.type = Route
 *      appender.query-routing.routes.route-mdc.name = IrrelevantName-query-routing
 *      appender.query-routing.routes.route-mdc.app.type = RandomAccessFile
 *      appender.query-routing.routes.route-mdc.app.name = query-file-appender
 *      appender.query-routing.routes.route-mdc.app.fileName = ${sys:hive.log.dir}/${ctx:sessionId}/${ctx:queryId}
 *      appender.query-routing.routes.route-mdc.app.layout.type = PatternLayout
 *      appender.query-routing.routes.route-mdc.app.layout.pattern = %d{ISO8601} %5p %c{2}: %m%n
 * @param conf  the configuration for HiveServer2 instance
 */
public static void registerRoutingAppender(org.apache.hadoop.conf.Configuration conf) {
    if (!HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVE_SERVER2_LOGGING_OPERATION_ENABLED, false)) {
        // spare some resources, do not register logger if it is not enabled .
        return;
    }
    String loggingLevel = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_SERVER2_LOGGING_OPERATION_LEVEL);
    OperationLog.LoggingLevel loggingMode = OperationLog.getLoggingLevel(loggingLevel);
    String layout = loggingMode == OperationLog.LoggingLevel.VERBOSE ? verboseLayout : nonVerboseLayout;
    // Create NullAppender
    PluginEntry nullEntry = new PluginEntry();
    nullEntry.setClassName(NullAppender.class.getName());
    nullEntry.setKey("null");
    nullEntry.setName("appender");
    PluginType<NullAppender> nullChildType = new PluginType<NullAppender>(nullEntry, NullAppender.class, "appender");
    Node nullChildNode = new Node(null, "Null", nullChildType);
    // Create default route
    PluginEntry defaultEntry = new PluginEntry();
    defaultEntry.setClassName(Route.class.getName());
    defaultEntry.setKey("route");
    defaultEntry.setName("Route");
    PluginType<Route> defaultType = new PluginType<Route>(defaultEntry, Route.class, "Route");
    Node nullNode = new Node(null, "Route", defaultType);
    nullNode.getChildren().add(nullChildNode);
    Route defaultRoute = Route.createRoute(null, "${ctx:queryId}", nullNode);
    // Create queryId based route
    PluginEntry entry = new PluginEntry();
    entry.setClassName(Route.class.getName());
    entry.setKey("route");
    entry.setName("Route");
    PluginType<Route> type = new PluginType<Route>(entry, Route.class, "Route");
    Node node = new Node(null, "Route", type);
    PluginEntry childEntry = new PluginEntry();
    childEntry.setClassName(HushableRandomAccessFileAppender.class.getName());
    childEntry.setKey("HushableMutableRandomAccess");
    childEntry.setName("appender");
    PluginType<HushableRandomAccessFileAppender> childType = new PluginType<>(childEntry, HushableRandomAccessFileAppender.class, "appender");
    Node childNode = new Node(node, "HushableMutableRandomAccess", childType);
    childNode.getAttributes().put("name", "query-file-appender");
    childNode.getAttributes().put("fileName", "${ctx:operationLogLocation}/${ctx:sessionId}/${ctx:queryId}");
    node.getChildren().add(childNode);
    PluginEntry filterEntry = new PluginEntry();
    filterEntry.setClassName(NameFilter.class.getName());
    filterEntry.setKey("namefilter");
    filterEntry.setName("namefilter");
    PluginType<NameFilter> filterType = new PluginType<>(filterEntry, NameFilter.class, "filter");
    Node filterNode = new Node(childNode, "NameFilter", filterType);
    filterNode.getAttributes().put("loggingLevel", loggingMode.name());
    childNode.getChildren().add(filterNode);
    PluginEntry layoutEntry = new PluginEntry();
    layoutEntry.setClassName(PatternLayout.class.getName());
    layoutEntry.setKey("patternlayout");
    layoutEntry.setName("layout");
    PluginType<PatternLayout> layoutType = new PluginType<>(layoutEntry, PatternLayout.class, "layout");
    Node layoutNode = new Node(childNode, "PatternLayout", layoutType);
    layoutNode.getAttributes().put("pattern", layout);
    childNode.getChildren().add(layoutNode);
    Route mdcRoute = Route.createRoute(null, null, node);
    Routes routes = Routes.createRoutes("${ctx:queryId}", defaultRoute, mdcRoute);
    LoggerContext context = (LoggerContext) LogManager.getContext(false);
    Configuration configuration = context.getConfiguration();
    String timeToLive = String.valueOf(HiveConf.getTimeVar(conf, HiveConf.ConfVars.HIVE_SERVER2_OPERATION_LOG_PURGEPOLICY_TIMETOLIVE, TimeUnit.SECONDS));
    PurgePolicy purgePolicy = IdlePurgePolicy.createPurgePolicy(timeToLive, null, "SECONDS", configuration);
    // Hack: due to the (non-standard) way that log4j configuration is extended to introduce the routing appender
    // the life-cycle methods are not called as expected leading to initialization problems (such as the scheduler)
    configuration.getScheduler().incrementScheduledItems();
    RoutingAppender routingAppender = RoutingAppender.createAppender(QUERY_ROUTING_APPENDER, "true", routes, configuration, null, purgePolicy, null);
    LoggerConfig loggerConfig = configuration.getRootLogger();
    loggerConfig.addAppender(routingAppender, null, null);
    context.updateLoggers();
    routingAppender.start();
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) Node(org.apache.logging.log4j.core.config.Node) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) OperationLog(org.apache.hadoop.hive.ql.session.OperationLog) PluginEntry(org.apache.logging.log4j.core.config.plugins.processor.PluginEntry) Routes(org.apache.logging.log4j.core.appender.routing.Routes) PluginType(org.apache.logging.log4j.core.config.plugins.util.PluginType) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig) RoutingAppender(org.apache.logging.log4j.core.appender.routing.RoutingAppender) Route(org.apache.logging.log4j.core.appender.routing.Route) PurgePolicy(org.apache.logging.log4j.core.appender.routing.PurgePolicy) IdlePurgePolicy(org.apache.logging.log4j.core.appender.routing.IdlePurgePolicy)

Example 4 with PluginEntry

use of org.apache.logging.log4j.core.config.plugins.processor.PluginEntry in project hive by apache.

the class LogDivertAppenderForTest method registerRoutingAppenderIfInTest.

/**
 * If the HIVE_IN_TEST is set, then programmatically register a routing appender to Log4J
 * configuration, which automatically writes the test log of each query to an individual file.
 * The equivalent property configuration is as follows:
 *  # queryId based routing file appender
 *      appender.test-query-routing.type = Routing
 *      appender.test-query-routing.name = test-query-routing
 *      appender.test-query-routing.routes.type = Routes
 *      appender.test-query-routing.routes.pattern = $${ctx:queryId}
 *      # default route
 *      appender.test-query-routing.routes.test-route-default.type = Route
 *      appender.test-query-routing.routes.test-route-default.key = $${ctx:queryId}
 *      appender.test-query-routing.routes.test-route-default.app.type = NullAppender
 *      appender.test-query-routing.routes.test-route-default.app.name = test-null-appender
 *      # queryId based route
 *      appender.test-query-routing.routes.test-route-mdc.type = Route
 *      appender.test-query-routing.routes.test-route-mdc.name = test-query-routing
 *      appender.test-query-routing.routes.test-route-mdc.app.type = RandomAccessFile
 *      appender.test-query-routing.routes.test-route-mdc.app.name = test-query-file-appender
 *      appender.test-query-routing.routes.test-route-mdc.app.fileName = ${sys:hive.log.dir}/${ctx:sessionId}/${ctx:queryId}.test
 *      appender.test-query-routing.routes.test-route-mdc.app.layout.type = PatternLayout
 *      appender.test-query-routing.routes.test-route-mdc.app.layout.pattern = %d{ISO8601} %5p %c{2}: %m%n
 *      appender.test-query-routing.routes.test-route-mdc.app.filter.type = TestFilter
 * @param conf the configuration for HiveServer2 instance
 */
public static void registerRoutingAppenderIfInTest(org.apache.hadoop.conf.Configuration conf) {
    if (!conf.getBoolean(HiveConf.ConfVars.HIVE_IN_TEST.varname, HiveConf.ConfVars.HIVE_IN_TEST.defaultBoolVal)) {
        // If not in test mode, then do no create the appender
        return;
    }
    String logLocation = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_SERVER2_LOGGING_OPERATION_LOG_LOCATION);
    // Create test-null-appender to drop events without queryId
    PluginEntry nullAppenderEntry = new PluginEntry();
    nullAppenderEntry.setClassName(NullAppender.class.getName());
    PluginType<NullAppender> nullAppenderType = new PluginType<>(nullAppenderEntry, NullAppender.class, "appender");
    Node nullAppenderChildNode = new Node(null, "test-null-appender", nullAppenderType);
    // Create default route where events go without queryId
    PluginEntry defaultRouteEntry = new PluginEntry();
    defaultRouteEntry.setClassName(Route.class.getName());
    PluginType<Route> defaultRouteType = new PluginType<>(defaultRouteEntry, Route.class, "");
    Node defaultRouteNode = new Node(null, "test-route-default", defaultRouteType);
    // Add the test-null-appender to the default route
    defaultRouteNode.getChildren().add(nullAppenderChildNode);
    // Create queryId based route
    PluginEntry queryIdRouteEntry = new PluginEntry();
    queryIdRouteEntry.setClassName(Route.class.getName());
    PluginType<Route> queryIdRouteType = new PluginType<>(queryIdRouteEntry, Route.class, "");
    Node queryIdRouteNode = new Node(null, "test-route-mdc", queryIdRouteType);
    // Create the queryId appender for the queryId route
    PluginEntry queryIdAppenderEntry = new PluginEntry();
    queryIdAppenderEntry.setClassName(HushableRandomAccessFileAppender.class.getName());
    PluginType<HushableRandomAccessFileAppender> queryIdAppenderType = new PluginType<>(queryIdAppenderEntry, HushableRandomAccessFileAppender.class, "appender");
    Node queryIdAppenderNode = new Node(queryIdRouteNode, "test-query-file-appender", queryIdAppenderType);
    queryIdAppenderNode.getAttributes().put("fileName", logLocation + "/${ctx:sessionId}/${ctx:queryId}.test");
    queryIdAppenderNode.getAttributes().put("name", "test-query-file-appender");
    // Add the queryId appender to the queryId based route
    queryIdRouteNode.getChildren().add(queryIdAppenderNode);
    // Create the filter for the queryId appender
    PluginEntry filterEntry = new PluginEntry();
    filterEntry.setClassName(TestFilter.class.getName());
    PluginType<TestFilter> filterType = new PluginType<>(filterEntry, TestFilter.class, "");
    Node filterNode = new Node(queryIdAppenderNode, "test-filter", filterType);
    // Add the filter to the queryId appender
    queryIdAppenderNode.getChildren().add(filterNode);
    // Create the layout for the queryId appender
    PluginEntry layoutEntry = new PluginEntry();
    layoutEntry.setClassName(PatternLayout.class.getName());
    PluginType<PatternLayout> layoutType = new PluginType<>(layoutEntry, PatternLayout.class, "");
    Node layoutNode = new Node(queryIdAppenderNode, "PatternLayout", layoutType);
    layoutNode.getAttributes().put("pattern", LogDivertAppender.nonVerboseLayout);
    // Add the layout to the queryId appender
    queryIdAppenderNode.getChildren().add(layoutNode);
    // Create the route objects based on the Nodes
    Route defaultRoute = Route.createRoute(null, "${ctx:queryId}", defaultRouteNode);
    Route mdcRoute = Route.createRoute(null, null, queryIdRouteNode);
    // Create the routes group
    Routes routes = Routes.createRoutes("${ctx:queryId}", defaultRoute, mdcRoute);
    LoggerContext context = (LoggerContext) LogManager.getContext(false);
    Configuration configuration = context.getConfiguration();
    String timeToLive = String.valueOf(HiveConf.getTimeVar(conf, HiveConf.ConfVars.HIVE_SERVER2_OPERATION_LOG_PURGEPOLICY_TIMETOLIVE, TimeUnit.SECONDS));
    PurgePolicy purgePolicy = IdlePurgePolicy.createPurgePolicy(timeToLive, null, "SECONDS", configuration);
    // Hack: due to the (non-standard) way that log4j configuration is extended to introduce the routing appender
    // the life-cycle methods are not called as expected leading to initialization problems (such as the scheduler)
    configuration.getScheduler().incrementScheduledItems();
    // Create the appender
    RoutingAppender routingAppender = RoutingAppender.createAppender(TEST_QUERY_ROUTING_APPENDER, "true", routes, configuration, null, purgePolicy, null);
    LoggerConfig loggerConfig = configuration.getRootLogger();
    loggerConfig.addAppender(routingAppender, null, null);
    context.updateLoggers();
    routingAppender.start();
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) Node(org.apache.logging.log4j.core.config.Node) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) PluginEntry(org.apache.logging.log4j.core.config.plugins.processor.PluginEntry) Routes(org.apache.logging.log4j.core.appender.routing.Routes) PluginType(org.apache.logging.log4j.core.config.plugins.util.PluginType) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig) RoutingAppender(org.apache.logging.log4j.core.appender.routing.RoutingAppender) Route(org.apache.logging.log4j.core.appender.routing.Route) PurgePolicy(org.apache.logging.log4j.core.appender.routing.PurgePolicy) IdlePurgePolicy(org.apache.logging.log4j.core.appender.routing.IdlePurgePolicy)

Aggregations

PluginEntry (org.apache.logging.log4j.core.config.plugins.processor.PluginEntry)4 DecimalFormat (java.text.DecimalFormat)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 LoggerContext (org.apache.logging.log4j.core.LoggerContext)2 IdlePurgePolicy (org.apache.logging.log4j.core.appender.routing.IdlePurgePolicy)2 PurgePolicy (org.apache.logging.log4j.core.appender.routing.PurgePolicy)2 Route (org.apache.logging.log4j.core.appender.routing.Route)2 Routes (org.apache.logging.log4j.core.appender.routing.Routes)2 RoutingAppender (org.apache.logging.log4j.core.appender.routing.RoutingAppender)2 Configuration (org.apache.logging.log4j.core.config.Configuration)2 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)2 Node (org.apache.logging.log4j.core.config.Node)2 PluginType (org.apache.logging.log4j.core.config.plugins.util.PluginType)2 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)2 IOException (java.io.IOException)1 URL (java.net.URL)1 Map (java.util.Map)1