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();
}
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);
}
}
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)));
}
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);
}
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"));
}
Aggregations