Search in sources :

Example 21 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext 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 22 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext in project logging-log4j2 by apache.

the class Configurator method setLevel.

/**
     * Sets logger levels.
     *
     * @param levelMap
     *            a levelMap where keys are level names and values are new
     *            Levels.
     */
public static void setLevel(final Map<String, Level> levelMap) {
    final LoggerContext loggerContext = LoggerContext.getContext(false);
    final Configuration config = loggerContext.getConfiguration();
    boolean set = false;
    for (final Map.Entry<String, Level> entry : levelMap.entrySet()) {
        final String loggerName = entry.getKey();
        final Level level = entry.getValue();
        set |= setLevel(loggerName, level, config);
    }
    if (set) {
        loggerContext.updateLoggers();
    }
}
Also used : Level(org.apache.logging.log4j.Level) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Map(java.util.Map)

Example 23 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext in project logging-log4j2 by apache.

the class ConfigurationAssemblerTest method testCustomConfigurationFactory.

@Test
public void testCustomConfigurationFactory() throws Exception {
    try {
        System.setProperty(ConfigurationFactory.CONFIGURATION_FACTORY_PROPERTY, "org.apache.logging.log4j.core.config.builder.CustomConfigurationFactory");
        System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
        final Configuration config = ((LoggerContext) LogManager.getContext(false)).getConfiguration();
        validate(config);
    } finally {
        System.getProperties().remove(Constants.LOG4J_CONTEXT_SELECTOR);
        System.getProperties().remove(ConfigurationFactory.CONFIGURATION_FACTORY_PROPERTY);
    }
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Example 24 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext in project logging-log4j2 by apache.

the class ConfigurationAssemblerTest method testBuildConfiguration.

@Test
public void testBuildConfiguration() throws Exception {
    try {
        System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
        final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
        CustomConfigurationFactory.addTestFixtures("config name", builder);
        final Configuration configuration = builder.build();
        try (LoggerContext ctx = Configurator.initialize(configuration)) {
            validate(configuration);
        }
    } finally {
        System.getProperties().remove(Constants.LOG4J_CONTEXT_SELECTOR);
    }
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Example 25 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext in project logging-log4j2 by apache.

the class AdvertiserTest method setupClass.

@BeforeClass
public static void setupClass() {
    final File file = new File(STATUS_LOG);
    file.delete();
    System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
    final LoggerContext ctx = LoggerContext.getContext();
    final Configuration config = ctx.getConfiguration();
    if (config instanceof XmlConfiguration) {
        final String name = config.getName();
        if (name == null || !name.equals("XMLConfigTest")) {
            ctx.reconfigure();
        }
    } else {
        ctx.reconfigure();
    }
}
Also used : XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) File(java.io.File) LoggerContext(org.apache.logging.log4j.core.LoggerContext) BeforeClass(org.junit.BeforeClass)

Aggregations

LoggerContext (org.apache.logging.log4j.core.LoggerContext)156 Configuration (org.apache.logging.log4j.core.config.Configuration)53 Test (org.junit.Test)33 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)32 Appender (org.apache.logging.log4j.core.Appender)15 File (java.io.File)12 IOException (java.io.IOException)11 Logger (org.apache.logging.log4j.Logger)11 BeforeClass (org.junit.BeforeClass)11 Map (java.util.Map)9 Level (org.apache.logging.log4j.Level)8 LogEvent (org.apache.logging.log4j.core.LogEvent)8 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)7 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)7 Logger (org.apache.logging.log4j.core.Logger)6 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)5 AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)5 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)5 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)4 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)4