Search in sources :

Example 11 with AppenderComponentBuilder

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

the class ConfigurationBuilderTest method addTestFixtures.

private void addTestFixtures(final String name, final ConfigurationBuilder<BuiltConfiguration> builder) {
    builder.setConfigurationName(name);
    builder.setStatusLevel(Level.ERROR);
    builder.setShutdownTimeout(5000, TimeUnit.MILLISECONDS);
    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.addProperty("MyKey", "MyValue");
    builder.add(builder.newCustomLevel("Panic", 17));
    builder.setPackages("foo,bar");
}
Also used : AppenderComponentBuilder(org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder)

Example 12 with AppenderComponentBuilder

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

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