Search in sources :

Example 1 with AppenderComponentBuilder

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

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

the class CustomConfigurationFactory method addTestFixtures.

static Configuration addTestFixtures(final String name, final ConfigurationBuilder<BuiltConfiguration> builder) {
    builder.setConfigurationName(name);
    builder.setStatusLevel(Level.ERROR);
    builder.add(builder.newScriptFile("target/test-classes/scripts/filter.groovy").addIsWatched(true));
    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);
    final AppenderComponentBuilder appenderBuilder2 = builder.newAppender("Kafka", "Kafka").addAttribute("topic", "my-topic");
    appenderBuilder2.addComponent(builder.newProperty("bootstrap.servers", "localhost:9092"));
    appenderBuilder2.add(builder.newLayout("GelfLayout").addAttribute("host", "my-host").addComponent(builder.newKeyValuePair("extraField", "extraValue")));
    builder.add(appenderBuilder2);
    builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG, true).add(builder.newAppenderRef("Stdout")).addAttribute("additivity", false));
    builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout")));
    builder.add(builder.newCustomLevel("Panic", 17));
    return builder.build();
}
Also used : AppenderComponentBuilder(org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder)

Example 3 with AppenderComponentBuilder

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

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

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

the class Log4j1ConfigurationParser method buildFileAppender.

private void buildFileAppender(final String appenderName) {
    final AppenderComponentBuilder appenderBuilder = builder.newAppender(appenderName, FileAppender.PLUGIN_NAME);
    buildFileAppender(appenderName, appenderBuilder);
    builder.add(appenderBuilder);
}
Also used : AppenderComponentBuilder(org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder)

Aggregations

AppenderComponentBuilder (org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder)12 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)4 Test (org.junit.Test)3 LoggerContext (org.apache.logging.log4j.core.LoggerContext)2 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)2 Properties (java.util.Properties)1 Appender (org.apache.logging.log4j.core.Appender)1 ErrorHandler (org.apache.logging.log4j.core.ErrorHandler)1 LogEvent (org.apache.logging.log4j.core.LogEvent)1 ConfigurationException (org.apache.logging.log4j.core.config.ConfigurationException)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