org.apache.logging.log4j.core.config.builder.api.ComponentBuilder Java Examples

The following examples show how to use org.apache.logging.log4j.core.config.builder.api.ComponentBuilder. 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: PropertiesConfigurationBuilder.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private static <B extends ComponentBuilder<?>> B processRemainingProperties(final B builder,
                                                                            final Properties properties) {
    while (properties.size() > 0) {
        final String propertyName = properties.stringPropertyNames().iterator().next();
        final int index = propertyName.indexOf('.');
        if (index > 0) {
            final String prefix = propertyName.substring(0, index);
            final Properties componentProperties = PropertiesUtil.extractSubset(properties, prefix);
            builder.addComponent(createComponent(builder, prefix, componentProperties));
        } else {
            builder.addAttribute(propertyName, properties.getProperty(propertyName));
            properties.remove(propertyName);
        }
    }
    return builder;
}
 
Example #2
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 #3
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 #4
Source File: Log4j1ConfigurationParser.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void buildAttribute(final String componentName, final ComponentBuilder componentBuilder,
        final String sourceAttributeName, final String targetAttributeName) {
    final String attributeValue = getLog4jAppenderValue(componentName, sourceAttributeName);
    if (attributeValue != null) {
        componentBuilder.addAttribute(targetAttributeName, attributeValue);
    }
}
 
Example #5
Source File: PropertiesConfigurationBuilder.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private <B extends LoggableComponentBuilder<? extends ComponentBuilder<?>>> B addLoggersToComponent(
    final B loggerBuilder, final Properties properties) {
    final Map<String, Properties> appenderRefs = PropertiesUtil.partitionOnCommonPrefixes(
        PropertiesUtil.extractSubset(properties, "appenderRef"));
    for (final Map.Entry<String, Properties> entry : appenderRefs.entrySet()) {
        loggerBuilder.add(createAppenderRef(entry.getKey().trim(), entry.getValue()));
    }
    return loggerBuilder;
}
 
Example #6
Source File: Log4j1ConfigurationParser.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void buildMandatoryAttribute(final String componentName, final ComponentBuilder componentBuilder,
        final String sourceAttributeName, final String targetAttributeName) {
    final String attributeValue = getLog4jAppenderValue(componentName, sourceAttributeName);
    if (attributeValue != null) {
        componentBuilder.addAttribute(targetAttributeName, attributeValue);
    } else {
        reportWarning("Missing " + sourceAttributeName + " for " + componentName);
    }
}
 
Example #7
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 #8
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 #9
Source File: Log4j1ConfigurationParser.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void buildAttribute(final String componentName, final ComponentBuilder componentBuilder,
        final String sourceAttributeName, final String targetAttributeName) {
    final String attributeValue = getLog4jAppenderValue(componentName, sourceAttributeName);
    if (attributeValue != null) {
        componentBuilder.addAttribute(targetAttributeName, attributeValue);
    }
}
 
Example #10
Source File: PropertiesConfigurationBuilder.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private <B extends FilterableComponentBuilder<? extends ComponentBuilder<?>>> B addFiltersToComponent(
    final B componentBuilder, final Properties properties) {
    final Map<String, Properties> filters = PropertiesUtil.partitionOnCommonPrefixes(
        PropertiesUtil.extractSubset(properties, "filter"));
    for (final Map.Entry<String, Properties> entry : filters.entrySet()) {
        componentBuilder.add(createFilter(entry.getKey().trim(), entry.getValue()));
    }
    return componentBuilder;
}
 
Example #11
Source File: Log4j1ConfigurationParser.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private void buildMandatoryAttribute(final String componentName, final ComponentBuilder componentBuilder,
        final String sourceAttributeName, final String targetAttributeName) {
    final String attributeValue = getLog4jAppenderValue(componentName, sourceAttributeName);
    if (attributeValue != null) {
        componentBuilder.addAttribute(targetAttributeName, attributeValue);
    } else {
        reportWarning("Missing " + sourceAttributeName + " for " + componentName);
    }
}
 
Example #12
Source File: PropertiesConfigurationBuilder.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private static <B extends ComponentBuilder<B>> ComponentBuilder<B> createComponent(final ComponentBuilder<?> parent,
                                                                                   final String key,
                                                                                   final Properties properties) {
    final String name = (String) properties.remove(CONFIG_NAME);
    final String type = (String) properties.remove(CONFIG_TYPE);
    if (Strings.isEmpty(type)) {
        throw new ConfigurationException("No type attribute provided for component " + key);
    }
    final ComponentBuilder<B> componentBuilder = parent.getBuilder().newComponent(name, type);
    return processRemainingProperties(componentBuilder, properties);
}
 
Example #13
Source File: DefaultConfigurationBuilder.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public <B extends ComponentBuilder<B>> ComponentBuilder<B> newComponent(final String type) {
    return new DefaultComponentBuilder<>(this, type);
}
 
Example #14
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);

}
 
Example #15
Source File: DefaultConfigurationBuilder.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public <B extends ComponentBuilder<B>> ComponentBuilder<B> newComponent(final String name, final String type) {
    return new DefaultComponentBuilder<>(this, name, type);
}
 
Example #16
Source File: DefaultConfigurationBuilder.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public <B extends ComponentBuilder<B>> ComponentBuilder<B> newComponent(final String name, final String type,
                                                                        final String value) {
    return new DefaultComponentBuilder<>(this, name, type, value);
}
 
Example #17
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 #18
Source File: DefaultConfigurationBuilder.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
protected ConfigurationBuilder<T> add(final Component parent, final ComponentBuilder<?> builder) {
    parent.getComponents().add(builder.build());
    return this;
}
 
Example #19
Source File: DefaultComponentBuilder.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public T addComponent(final ComponentBuilder<?> builder) {
    components.add(builder.build());
    return (T) this;
}
 
Example #20
Source File: Log4j1ConfigurationParser.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
private void buildAttributeWithDefault(final String componentName, final ComponentBuilder componentBuilder,
        final String sourceAttributeName, final String targetAttributeName, final String defaultValue) {
    final String attributeValue = getLog4jAppenderValue(componentName, sourceAttributeName, defaultValue);
    componentBuilder.addAttribute(targetAttributeName, attributeValue);
}
 
Example #21
Source File: Log4j1ConfigurationParser.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
private void buildAttributeWithDefault(final String componentName, final ComponentBuilder componentBuilder,
        final String sourceAttributeName, final String targetAttributeName, final String defaultValue) {
    final String attributeValue = getLog4jAppenderValue(componentName, sourceAttributeName, defaultValue);
    componentBuilder.addAttribute(targetAttributeName, attributeValue);
}
 
Example #22
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();

}