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

The following examples show how to use org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder#addComponent() . 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: Log4j1ConfigurationParser.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void buildDailyRollingFileAppender(final String appenderName) {
    final AppenderComponentBuilder appenderBuilder = builder.newAppender(appenderName,
            RollingFileAppender.PLUGIN_NAME);
    buildFileAppender(appenderName, appenderBuilder);
    final String fileName = getLog4jAppenderValue(appenderName, "File");
    final String datePattern = getLog4jAppenderValue(appenderName, "DatePattern", fileName + "'.'yyyy-MM-dd");
    appenderBuilder.addAttribute("filePattern", fileName + "%d{" + datePattern + "}");
    final ComponentBuilder<?> triggeringPolicy = builder.newComponent("Policies")
            .addComponent(builder.newComponent("TimeBasedTriggeringPolicy").addAttribute("modulate", true));
    appenderBuilder.addComponent(triggeringPolicy);
    appenderBuilder
            .addComponent(builder.newComponent("DefaultRolloverStrategy").addAttribute("max", Integer.MAX_VALUE));
    builder.add(appenderBuilder);
}
 
Example 2
Source File: Log4j1ConfigurationParser.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void buildRollingFileAppender(final String appenderName) {
    final AppenderComponentBuilder appenderBuilder = builder.newAppender(appenderName,
            RollingFileAppender.PLUGIN_NAME);
    buildFileAppender(appenderName, appenderBuilder);
    final String fileName = getLog4jAppenderValue(appenderName, "File");
    appenderBuilder.addAttribute("filePattern", fileName + ".%i");
    final String maxFileSizeString = getLog4jAppenderValue(appenderName, "MaxFileSize", "10485760");
    final String maxBackupIndexString = getLog4jAppenderValue(appenderName, "MaxBackupIndex", "1");
    final ComponentBuilder<?> triggeringPolicy = builder.newComponent("Policies").addComponent(
            builder.newComponent("SizeBasedTriggeringPolicy").addAttribute("size", maxFileSizeString));
    appenderBuilder.addComponent(triggeringPolicy);
    appenderBuilder.addComponent(
            builder.newComponent("DefaultRolloverStrategy").addAttribute("max", maxBackupIndexString));
    builder.add(appenderBuilder);
}
 
Example 3
Source File: Log4j1ConfigurationParser.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void buildDailyRollingFileAppender(final String appenderName) {
    final AppenderComponentBuilder appenderBuilder = builder.newAppender(appenderName,
            RollingFileAppender.PLUGIN_NAME);
    buildFileAppender(appenderName, appenderBuilder);
    final String fileName = getLog4jAppenderValue(appenderName, "File");
    final String datePattern = getLog4jAppenderValue(appenderName, "DatePattern", fileName + "'.'yyyy-MM-dd");
    appenderBuilder.addAttribute("filePattern", fileName + "%d{" + datePattern + "}");
    final ComponentBuilder<?> triggeringPolicy = builder.newComponent("Policies")
            .addComponent(builder.newComponent("TimeBasedTriggeringPolicy").addAttribute("modulate", true));
    appenderBuilder.addComponent(triggeringPolicy);
    appenderBuilder
            .addComponent(builder.newComponent("DefaultRolloverStrategy").addAttribute("max", Integer.MAX_VALUE));
    builder.add(appenderBuilder);
}
 
Example 4
Source File: Log4j1ConfigurationParser.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void buildRollingFileAppender(final String appenderName) {
    final AppenderComponentBuilder appenderBuilder = builder.newAppender(appenderName,
            RollingFileAppender.PLUGIN_NAME);
    buildFileAppender(appenderName, appenderBuilder);
    final String fileName = getLog4jAppenderValue(appenderName, "File");
    appenderBuilder.addAttribute("filePattern", fileName + ".%i");
    final String maxFileSizeString = getLog4jAppenderValue(appenderName, "MaxFileSize", "10485760");
    final String maxBackupIndexString = getLog4jAppenderValue(appenderName, "MaxBackupIndex", "1");
    final ComponentBuilder<?> triggeringPolicy = builder.newComponent("Policies").addComponent(
            builder.newComponent("SizeBasedTriggeringPolicy").addAttribute("size", maxFileSizeString));
    appenderBuilder.addComponent(triggeringPolicy);
    appenderBuilder.addComponent(
            builder.newComponent("DefaultRolloverStrategy").addAttribute("max", maxBackupIndexString));
    builder.add(appenderBuilder);
}
 
Example 5
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 6
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 7
Source File: CustomConfigurationFactory.java    From tutorials with MIT License 4 votes vote down vote up
static Configuration createConfiguration(final String name, ConfigurationBuilder<BuiltConfiguration> builder) {
    AppenderComponentBuilder console = builder.newAppender("Stdout", "Console");
    LayoutComponentBuilder layout = builder.newLayout("PatternLayout")
        .addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable");
    console.add(layout);
    FilterComponentBuilder filter = builder.newFilter("MarkerFilter", Filter.Result.ACCEPT, Filter.Result.DENY);
    filter.addAttribute("marker", "FLOW");
    console.add(filter);
    builder.add(console);
    ComponentBuilder triggeringPolicies = builder.newComponent("Policies")
        .addComponent(builder.newComponent("CronTriggeringPolicy")
            .addAttribute("schedule", "0 0 0 * * ?"))
        .addComponent(builder.newComponent("SizeBasedTriggeringPolicy")
            .addAttribute("size", "100M"));
    AppenderComponentBuilder rollingFile = builder.newAppender("rolling", "RollingFile");
    rollingFile.addAttribute("fileName", "target/rolling.log");
    rollingFile.addAttribute("filePattern", "target/archive/rolling-%d{MM-dd-yy}.log.gz");
    rollingFile.add(layout);
    rollingFile.addComponent(triggeringPolicies);
    builder.add(rollingFile);
    AppenderComponentBuilder file = builder.newAppender("FileSystem", "File");
    file.addAttribute("fileName", "target/logging.log");
    file.add(layout);
    builder.add(file);
    LoggerComponentBuilder logger = builder.newLogger("com", Level.DEBUG);
    logger.add(builder.newAppenderRef("Stdout"));
    logger.add(builder.newAppenderRef("rolling"));
    logger.add(builder.newAppenderRef("FileSystem"));
    logger.addAttribute("additivity", false);
    builder.add(logger);
    RootLoggerComponentBuilder rootLogger = builder.newRootLogger(Level.ERROR);
    rootLogger.add(builder.newAppenderRef("Stdout"));
    rootLogger.add(builder.newAppenderRef("rolling"));
    // rootLogger.add(builder.newAppenderRef("syslogAppender"));
    rootLogger.add(builder.newAppenderRef("FileSystem"));
    rootLogger.addAttribute("additivity", false);
    builder.add(rootLogger);
    try {
        builder.writeXmlConfiguration(System.out);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return builder.build();

}