Search in sources :

Example 1 with BuiltConfiguration

use of org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration 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 BuiltConfiguration

use of org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration 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 3 with BuiltConfiguration

use of org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration in project logging-log4j2 by apache.

the class TestConfigurator method testBuilder.

@Test
public void testBuilder() throws Exception {
    final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
    builder.setStatusLevel(Level.ERROR);
    builder.setConfigurationName("BuilderTest");
    builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL).addAttribute("level", Level.DEBUG));
    final AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT);
    appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"));
    appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.DENY, Filter.Result.NEUTRAL).addAttribute("marker", "FLOW"));
    builder.add(appenderBuilder);
    builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG).add(builder.newAppenderRef("Stdout")).addAttribute("additivity", false));
    builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout")));
    ctx = Configurator.initialize(builder.build());
    final Configuration config = ctx.getConfiguration();
    assertNotNull("No configuration", config);
    assertEquals("Unexpected Configuration", "BuilderTest", config.getName());
    assertThat(config.getAppenders(), hasSize(equalTo(1)));
}
Also used : AppenderComponentBuilder(org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) Test(org.junit.Test)

Example 4 with BuiltConfiguration

use of org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration in project logging-log4j2 by apache.

the class TestConfigurator method testRolling.

@Test
public void testRolling() throws Exception {
    final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
    builder.setStatusLevel(Level.ERROR);
    builder.setConfigurationName("RollingBuilder");
    // create the console appender
    AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT);
    appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"));
    builder.add(appenderBuilder);
    final LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout").addAttribute("pattern", "%d [%t] %-5level: %msg%n");
    final ComponentBuilder triggeringPolicy = builder.newComponent("Policies").addComponent(builder.newComponent("CronTriggeringPolicy").addAttribute("schedule", "0 0 0 * * ?")).addComponent(builder.newComponent("SizeBasedTriggeringPolicy").addAttribute("size", "100M"));
    appenderBuilder = builder.newAppender("rolling", "RollingFile").addAttribute("fileName", "target/rolling.log").addAttribute("filePattern", "target/archive/rolling-%d{MM-dd-yy}.log.gz").add(layoutBuilder).addComponent(triggeringPolicy);
    builder.add(appenderBuilder);
    // create the new logger
    builder.add(builder.newLogger("TestLogger", Level.DEBUG).add(builder.newAppenderRef("rolling")).addAttribute("additivity", false));
    builder.add(builder.newRootLogger(Level.DEBUG).add(builder.newAppenderRef("rolling")));
    final Configuration config = builder.build();
    config.initialize();
    assertNotNull("No rolling file appender", config.getAppender("rolling"));
    assertEquals("Unexpected Configuration", "RollingBuilder", config.getName());
    // Initialize the new configuration
    final LoggerContext ctx = Configurator.initialize(config);
    Configurator.shutdown(ctx);
}
Also used : AppenderComponentBuilder(org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) LayoutComponentBuilder(org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) ComponentBuilder(org.apache.logging.log4j.core.config.builder.api.ComponentBuilder) LayoutComponentBuilder(org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder) AppenderComponentBuilder(org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Example 5 with BuiltConfiguration

use of org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration in project logging-log4j2 by apache.

the class TestConfigurator method testBuilderWithScripts.

@Test
public void testBuilderWithScripts() throws Exception {
    final String script = "if (logEvent.getLoggerName().equals(\"NoLocation\")) {\n" + "                return \"NoLocation\";\n" + "            } else if (logEvent.getMarker() != null && logEvent.getMarker().isInstanceOf(\"FLOW\")) {\n" + "                return \"Flow\";\n" + "            } else {\n" + "                return null;\n" + "            }";
    final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
    builder.setStatusLevel(Level.ERROR);
    builder.setConfigurationName("BuilderTest");
    builder.add(builder.newScriptFile("filter.groovy", "target/test-classes/scripts/filter.groovy").addIsWatched(true));
    final AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT);
    appenderBuilder.add(builder.newLayout("PatternLayout").addComponent(builder.newComponent("ScriptPatternSelector").addAttribute("defaultPattern", "[%-5level] %c{1.} %C{1.}.%M.%L %msg%n").addComponent(builder.newComponent("PatternMatch").addAttribute("key", "NoLocation").addAttribute("pattern", "[%-5level] %c{1.} %msg%n")).addComponent(builder.newComponent("PatternMatch").addAttribute("key", "FLOW").addAttribute("pattern", "[%-5level] %c{1.} ====== %C{1.}.%M:%L %msg ======%n")).addComponent(builder.newComponent("selectorScript", "Script", script).addAttribute("language", "beanshell"))));
    appenderBuilder.add(builder.newFilter("ScriptFilter", Filter.Result.DENY, Filter.Result.NEUTRAL).addComponent(builder.newComponent("ScriptRef").addAttribute("ref", "filter.groovy")));
    builder.add(appenderBuilder);
    builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG).add(builder.newAppenderRef("Stdout")).addAttribute("additivity", false));
    builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout")));
    ctx = Configurator.initialize(builder.build());
    final Configuration config = ctx.getConfiguration();
    assertNotNull("No configuration", config);
    assertEquals("Unexpected Configuration", "BuilderTest", config.getName());
    assertThat(config.getAppenders(), hasSize(equalTo(1)));
    assertNotNull("Filter script not found", config.getScriptManager().getScript("filter.groovy"));
    assertNotNull("pattern selector script not found", config.getScriptManager().getScript("selectorScript"));
}
Also used : AppenderComponentBuilder(org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) Test(org.junit.Test)

Aggregations

BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)5 AppenderComponentBuilder (org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder)4 Test (org.junit.Test)4 LoggerContext (org.apache.logging.log4j.core.LoggerContext)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 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)1 Configuration (org.apache.logging.log4j.core.config.Configuration)1 ComponentBuilder (org.apache.logging.log4j.core.config.builder.api.ComponentBuilder)1 LayoutComponentBuilder (org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder)1 RootLoggerComponentBuilder (org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder)1 BeforeClass (org.junit.BeforeClass)1