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

The following examples show how to use org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder. 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: Log4j2ConfigurationFactory.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
private AppenderComponentBuilder createFileAppender(ConfigurationBuilder<BuiltConfiguration> builder, String logFile, LayoutComponentBuilder layout) {
    ByteValue size = ByteValue.of(getValue("log_file_size", sources, LoggingConfiguration.DEFAULT_MAX_SIZE));
    return builder.newAppender("rolling", "RollingFile")
        .addAttribute("fileName", logFile)
        .addAttribute("filePattern", logFile + ".%i")
        .add(layout)
        .addComponent(builder.newComponent("Policies")
            .addComponent(builder.newComponent("SizeBasedTriggeringPolicy").addAttribute("size", size.getBytes() + "B")))
        // Always keep exactly one history file.
        // This is needed to ensure that the rest of the file can be sent when its rotated.
        // Storing multiple history files would give the false impression that, for example,
        // when currently reading from apm.log2, the reading would continue from apm.log1.
        // This is not the case, when apm.log2 is fully read, the reading will continue from apm.log.
        // That is because we don't want to require the reader having to know the file name pattern of the rotated file.
        .addComponent(builder.newComponent("DefaultRolloverStrategy").addAttribute("max", 1));
}
 
Example #2
Source File: LoggingConfigurationFactory.java    From luna with MIT License 6 votes vote down vote up
/**
 * Returns the pattern layout defined by {@link LoggingSettings}.
 *
 * @param builder The builder.
 * @return The pattern layout.
 */
private LayoutComponentBuilder getPatternLayout(ConfigurationBuilder<BuiltConfiguration> builder) {
    var pattern = "";
    var formatType = Luna.loggingSettings().formatType();
    switch (formatType) {
        case BASIC:
            pattern = "[%d{dd MMM yyyy, h:mm:ss a}] [%level] %msg%n";
            break;
        case VERBOSE:
            pattern = "%d{[dd MMM yyyy HH:mm:ss]} [%logger{36}] [%t]%n%level: %msg%n";
            break;
    }
    if (pattern.isEmpty()) {
        throw new IllegalStateException("No pattern layout for " + formatType + "!");
    }
    return builder.newLayout("PatternLayout").addAttribute("pattern", pattern);
}
 
Example #3
Source File: Log4j2ConfigurationFactory.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
private LayoutComponentBuilder createLayout(ConfigurationBuilder<BuiltConfiguration> builder, LogFormat logFormat) {
    if (logFormat == LogFormat.PLAIN_TEXT) {
        return builder
                .newLayout("PatternLayout")
                .addAttribute("pattern", "%d [%thread] %-5level %logger{36} - %msg%n");
    } else {
        String serviceName = getValue(CoreConfiguration.SERVICE_NAME, sources, ServiceNameUtil.getDefaultServiceName());
        return builder.newLayout("EcsLayout")
            .addAttribute("eventDataset", serviceName + ".apm");
    }
}
 
Example #4
Source File: Log4j1ConfigurationParser.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private LayoutComponentBuilder newPatternLayout(final String pattern) {
    final LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout");
    if (pattern != null) {
        layoutBuilder.addAttribute("pattern", pattern);
    }
    return layoutBuilder;
}
 
Example #5
Source File: Log4j1ConfigurationParser.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private LayoutComponentBuilder newPatternLayout(final String pattern) {
    final LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout");
    if (pattern != null) {
        layoutBuilder.addAttribute("pattern", pattern);
    }
    return layoutBuilder;
}
 
Example #6
Source File: PropertiesConfigurationBuilder.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
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);
}
 
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();

}
 
Example #8
Source File: DefaultAppenderComponentBuilder.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public AppenderComponentBuilder add(final LayoutComponentBuilder builder) {
    return addComponent(builder);
}
 
Example #9
Source File: DefaultConfigurationBuilder.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public LayoutComponentBuilder newLayout(final String type) {
    return new DefaultLayoutComponentBuilder(this, type);
}
 
Example #10
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);

}