Search in sources :

Example 1 with LayoutComponentBuilder

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

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

the class PropertiesConfigurationBuilder method createLayout.

private LayoutComponentBuilder createLayout(final String appenderName, final Properties properties) {
    final String type = (String) properties.remove(CONFIG_TYPE);
    if (Strings.isEmpty(type)) {
        throw new ConfigurationException("No type attribute provided for Layout on Appender " + appenderName);
    }
    final LayoutComponentBuilder layoutBuilder = builder.newLayout(type);
    return processRemainingProperties(layoutBuilder, properties);
}
Also used : ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) LayoutComponentBuilder(org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder)

Aggregations

LayoutComponentBuilder (org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder)2 LoggerContext (org.apache.logging.log4j.core.LoggerContext)1 ConfigurationException (org.apache.logging.log4j.core.config.ConfigurationException)1 AppenderComponentBuilder (org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder)1 ComponentBuilder (org.apache.logging.log4j.core.config.builder.api.ComponentBuilder)1 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)1 Test (org.junit.Test)1