Java Code Examples for org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder#setConfigurationName()

The following examples show how to use org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder#setConfigurationName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestConfigurator.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@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)));
}
 
Example 2
Source File: ConfigurationBuilderTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
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);

    builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG, true).
                add(builder.newAppenderRef("Stdout")).
                addAttribute("additivity", false));
    builder.add(builder.newLogger("org.apache.logging.log4j.core").
                add(builder.newAppenderRef("Stdout")));
    builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout")));

    builder.addProperty("MyKey", "MyValue");
    builder.add(builder.newCustomLevel("Panic", 17));
    builder.setPackages("foo,bar");
}
 
Example 3
Source File: CustomConfigurationFactory.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
public 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);

    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();
}
 
Example 4
Source File: RollingFileAppenderUpdateDataTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private ConfigurationBuilder<BuiltConfiguration> buildConfigurationBuilder(final String filePattern) {
    final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
    builder.setConfigurationName("LOG4J2-1964 demo");
    builder.setStatusLevel(Level.ERROR);
    // @formatter:off
    builder.add(builder.newAppender("consoleLog", "Console")
        .addAttribute("target", ConsoleAppender.Target.SYSTEM_ERR));
    builder.add(builder.newAppender("fooAppender", "RollingFile")
            .addAttribute("fileName", "target/rolling-update-date/foo.log")
            .addAttribute("filePattern", filePattern)
            .addComponent(builder.newComponent("SizeBasedTriggeringPolicy")
                    .addAttribute("size", "10MB")));
    builder.add(builder.newRootLogger(Level.INFO)
            .add(builder.newAppenderRef("consoleLog"))
            .add(builder.newAppenderRef("fooAppender")));
    // @formatter:on
    return builder;
}
 
Example 5
Source File: AbstractCustomConfigurationFactoryTemplate.java    From xian with Apache License 2.0 5 votes vote down vote up
private Configuration createConfiguration(final String name, ConfigurationBuilder<BuiltConfiguration> builder) {
    builder.setConfigurationName(name);
    builder.setStatusLevel(Level.ERROR);
    builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL).addAttribute("level", level()));
    if (isConsoleAppenderEnabled()) {
        AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT);
        appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "%d [%t] %-5level: %msg [%c][%X{msgId}]%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")));
    return builder.build();
}
 
Example 6
Source File: LoggingMixin.java    From picocli with Apache License 2.0 5 votes vote down vote up
public static LoggerContext initializeLog4j() {
    ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
    builder.setStatusLevel(Level.ERROR); // show internal log4j2 errors
    builder.setConfigurationName("QuickAndDirtySetup");
    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);
    //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").addAttribute("level", Level.WARN)));
    return Configurator.initialize(builder.build());
}
 
Example 7
Source File: LoggingMixin.java    From picocli with Apache License 2.0 5 votes vote down vote up
public static LoggerContext initializeLog4j() {
    ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
    builder.setStatusLevel(Level.ERROR); // show internal log4j2 errors
    builder.setConfigurationName("QuickAndDirtySetup");
    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);
    //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").addAttribute("level", Level.WARN)));
    return Configurator.initialize(builder.build());
}
 
Example 8
Source File: ConfigurationBuilderTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
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("Pulsar", "Pulsar")
        .addAttribute("serviceUrl", "pulsar://localhost:6650")
        .addAttribute("topic", "my-topic");
    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.newLogger("org.apache.logging.log4j.core", Level.DEBUG).
                add(builder.newAppenderRef("Stdout")));
    builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout")));

    builder.addProperty("MyKey", "MyValue");
    builder.add(builder.newCustomLevel("Panic", 17));
    builder.setPackages("foo,bar");
}
 
Example 9
Source File: CustomConfigurationFactory.java    From pulsar with Apache License 2.0 5 votes vote down vote up
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("Pulsar", "Pulsar")
        .addAttribute("serviceUrl", "pulsar://localhost:6650")
        .addAttribute("topic", "my-topic");
    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();
}
 
Example 10
Source File: LogUtil.java    From ldbc_graphalytics with Apache License 2.0 5 votes vote down vote up
public static void intializeLoggers() {
    ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
    builder.setConfigurationName("logger");
    RootLoggerComponentBuilder rootLogger = builder.newRootLogger(Level.DEBUG);
    builder.add(rootLogger);

    Configurator.initialize(builder.build());
}
 
Example 11
Source File: ConfigurationBuilderTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
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.newLogger("org.apache.logging.log4j.core").
                add(builder.newAppenderRef("Stdout")));
    builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout")));

    builder.addProperty("MyKey", "MyValue");
    builder.add(builder.newCustomLevel("Panic", 17));
    builder.setPackages("foo,bar");
}
 
Example 12
Source File: CustomConfigurationFactory.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 13
Source File: TestConfigurator.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example 14
Source File: BHBotConfigurationFactory.java    From BHBot with GNU General Public License v3.0 4 votes vote down vote up
private static Configuration createConfiguration(final String name, ConfigurationBuilder<BuiltConfiguration> builder) {
    System.setProperty("log4j.skipJansi", "false");

    builder.setConfigurationName(name);
    builder.setStatusLevel(Level.ERROR);

    // baseDir for logs
    builder.addProperty("baseDir", BHBot.logBaseDir);

    // STD OUT
    AppenderComponentBuilder stdOutBuilder = builder.newAppender("StdOut", "CONSOLE")
            .addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT);
    stdOutBuilder.add(builder.newLayout("PatternLayout")
            .addAttribute("pattern", "%d{ABSOLUTE} %highlight{%level}{FATAL=bg_red, ERROR=bg_red, WARN=bg_yellow, AUTOBRIBE=cyan, AUTOREVIVE=cyan, AUTOSHRINE=cyan, AUTORUNE=cyan, STATS=magenta, READOUT=yellow, INFO=green, DEBUG=blue} - %msg%n"));
    stdOutBuilder.add(builder.newFilter("ThresholdFilter", Filter.Result.DENY,
            Filter.Result.ACCEPT).addAttribute("level", Level.ERROR));
    builder.add(stdOutBuilder);

    // STD ERR
    AppenderComponentBuilder stdErrBuilder = builder.newAppender("StdErr", "CONSOLE").
            addAttribute("target", ConsoleAppender.Target.SYSTEM_ERR);
    stdErrBuilder.add(builder.newLayout("PatternLayout")
            .addAttribute("pattern", "%d{ABSOLUTE} %style{%highlight{%level}{FATAL=bg_red, ERROR=red, WARN=bg_yellow, AUTOBRIBE=cyan, AUTOREVIVE=cyan, AUTOSHRINE=cyan, AUTORUNE=cyan, STATS=magenta, READOUT=yellow, INFO=green, DEBUG=blue} - %msg%n}{red}"));
    stdErrBuilder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT,
            Filter.Result.DENY).addAttribute("level", Level.ERROR));
    builder.add(stdErrBuilder);

    // Rolling File
    // Composite Triggering Policies
    @SuppressWarnings("rawtypes")
    ComponentBuilder triggeringPolicy = builder.newComponent("Policies")
            .addComponent(builder.newComponent("SizeBasedTriggeringPolicy")
                    .addAttribute("size", "32M"))
            .addComponent(builder.newComponent("TimeBasedTriggeringPolicy"));


    // Delete Component to manage old log deletion
    @SuppressWarnings("rawtypes")
    ComponentBuilder delete = builder.newComponent("Delete")
            .addAttribute("basePath", "${baseDir}")
            .addAttribute("maxDepth", "2")
            //.addAttribute("testMode", true)
            .addComponent(builder.newComponent("IfLastModified")
                    .addAttribute("age", "" + BHBot.logMaxDays + "d"));

    // DefaultRolloverStrategy Component
    @SuppressWarnings("rawtypes")
    ComponentBuilder defaulRolloverStrategy = builder.newComponent("DefaultRolloverStrategy")
            .addAttribute("max", BHBot.logMaxDays)
            .addAttribute("compressionLevel", 9)
            .addComponent(delete);

    AppenderComponentBuilder rollingBuilder = builder.newAppender("Rolling", "RollingFile")
            .addAttribute("filePattern", "${baseDir}/$${date:yyyy-MM}/BHBot-%d{yyyy-MM-dd}-%i.log.zip")
            .addAttribute("fileName", "${baseDir}/bhbot.log");
    rollingBuilder.add(builder.newLayout("PatternLayout")
            .addAttribute("pattern", "%d [%t] %p %c - %m%n"));
    rollingBuilder
            .addComponent(triggeringPolicy)
            .addComponent(defaulRolloverStrategy);
    builder.add(rollingBuilder);


    builder.add(
            builder.newRootLogger(BHBot.logLevel)
                    .add(builder.newAppenderRef("StdOut"))
                    .add(builder.newAppenderRef("StdErr"))
                    .add(builder.newAppenderRef("Rolling"))
    );
    return builder.build();
}
 
Example 15
Source File: TestConfigurator.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@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);

}