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