Search in sources :

Example 1 with RootLoggerComponentBuilder

use of org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder in project jmeter by apache.

the class TestGuiLogEventAppender method beforeClass.

/*
     * Configure logging with GuiLogEventAppender for root logger, and override the handler of GuiLogEventAppender
     * to see if there's any log4j2 AppenderControl level error (e.g, "Recursive call to appender gui-log-event").
     */
@BeforeClass
public static void beforeClass() {
    ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
    builder.setPackages("org.apache.jmeter.gui.logging");
    AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT);
    appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "%d %p %c{1.}: %m%n"));
    builder.add(appenderBuilder);
    appenderBuilder = builder.newAppender("gui-log-event", "GuiLogEvent");
    appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "%d %p %c{1.}: %m%n"));
    builder.add(appenderBuilder);
    RootLoggerComponentBuilder rootLoggerBuilder = builder.newRootLogger(Level.INFO);
    rootLoggerBuilder.add(builder.newAppenderRef("Stdout")).add(builder.newAppenderRef("gui-log-event"));
    builder.add(rootLoggerBuilder);
    final LoggerContext loggerContext = Configurator.initialize(builder.build());
    final Appender guiLogEventAppender = loggerContext.getRootLogger().getAppenders().get("gui-log-event");
    guiLogEventAppender.stop();
    guiLogEventAppender.setHandler(new ErrorHandler() {

        public void error(String msg) {
            log4j2LevelErrorMessages.add(msg);
        }

        public void error(String msg, Throwable t) {
            log4j2LevelErrorMessages.add(msg + " " + t);
        }

        public void error(String msg, LogEvent event, Throwable t) {
            log4j2LevelErrorMessages.add(msg + " " + t);
        }
    });
    guiLogEventAppender.start();
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) ErrorHandler(org.apache.logging.log4j.core.ErrorHandler) AppenderComponentBuilder(org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder) LogEvent(org.apache.logging.log4j.core.LogEvent) RootLoggerComponentBuilder(org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) BeforeClass(org.junit.BeforeClass)

Example 2 with RootLoggerComponentBuilder

use of org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder in project logging-log4j2 by apache.

the class PropertiesConfigurationBuilder method createRootLogger.

private RootLoggerComponentBuilder createRootLogger(final Properties properties) {
    final String level = (String) properties.remove("level");
    final String type = (String) properties.remove(CONFIG_TYPE);
    final String location = (String) properties.remove("includeLocation");
    final boolean includeLocation;
    final RootLoggerComponentBuilder loggerBuilder;
    if (type != null) {
        if (type.equalsIgnoreCase("asyncRoot")) {
            if (location != null) {
                includeLocation = Boolean.parseBoolean(location);
                loggerBuilder = builder.newAsyncRootLogger(level, includeLocation);
            } else {
                loggerBuilder = builder.newAsyncRootLogger(level);
            }
        } else {
            throw new ConfigurationException("Unknown Logger type for root logger" + type);
        }
    } else {
        if (location != null) {
            includeLocation = Boolean.parseBoolean(location);
            loggerBuilder = builder.newRootLogger(level, includeLocation);
        } else {
            loggerBuilder = builder.newRootLogger(level);
        }
    }
    addLoggersToComponent(loggerBuilder, properties);
    return addFiltersToComponent(loggerBuilder, properties);
}
Also used : ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) RootLoggerComponentBuilder(org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder)

Example 3 with RootLoggerComponentBuilder

use of org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder in project logging-log4j2 by apache.

the class Log4j1ConfigurationParser method buildRootLogger.

private void buildRootLogger(final String rootLoggerValue) {
    if (rootLoggerValue == null) {
        return;
    }
    final String[] rootLoggerParts = rootLoggerValue.split(COMMA_DELIMITED_RE);
    final String rootLoggerLevel = getLevelString(rootLoggerParts, Level.ERROR.name());
    final RootLoggerComponentBuilder loggerBuilder = builder.newRootLogger(rootLoggerLevel);
    //
    final String[] sortedAppenderNames = Arrays.copyOfRange(rootLoggerParts, 1, rootLoggerParts.length);
    Arrays.sort(sortedAppenderNames);
    for (final String appender : sortedAppenderNames) {
        loggerBuilder.add(builder.newAppenderRef(appender));
    }
    builder.add(loggerBuilder);
}
Also used : RootLoggerComponentBuilder(org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder)

Aggregations

RootLoggerComponentBuilder (org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder)3 Appender (org.apache.logging.log4j.core.Appender)1 ErrorHandler (org.apache.logging.log4j.core.ErrorHandler)1 LogEvent (org.apache.logging.log4j.core.LogEvent)1 LoggerContext (org.apache.logging.log4j.core.LoggerContext)1 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)1 ConfigurationException (org.apache.logging.log4j.core.config.ConfigurationException)1 AppenderComponentBuilder (org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder)1 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)1 BeforeClass (org.junit.BeforeClass)1