|
| 1 | +/** |
| 2 | + This class demonstrates how to build the components of |
| 3 | + the configuration factory, as described in Section 3 of |
| 4 | + "Programmatic Configuration with Log4j 2" |
| 5 | +**/ |
| 6 | +package com.baeldung.log4j2.config; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.net.URI; |
| 10 | + |
| 11 | +import org.apache.logging.log4j.Level; |
| 12 | +import org.apache.logging.log4j.core.Filter; |
| 13 | +import org.apache.logging.log4j.core.LoggerContext; |
| 14 | +import org.apache.logging.log4j.core.config.Configuration; |
| 15 | +import org.apache.logging.log4j.core.config.ConfigurationFactory; |
| 16 | +import org.apache.logging.log4j.core.config.ConfigurationSource; |
| 17 | +import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder; |
| 18 | +import org.apache.logging.log4j.core.config.builder.api.ComponentBuilder; |
| 19 | +import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder; |
| 20 | +import org.apache.logging.log4j.core.config.builder.api.FilterComponentBuilder; |
| 21 | +import org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder; |
| 22 | +import org.apache.logging.log4j.core.config.builder.api.LoggerComponentBuilder; |
| 23 | +import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder; |
| 24 | +import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration; |
| 25 | + |
| 26 | +public class CustomConfigurationFactory extends ConfigurationFactory { |
| 27 | + |
| 28 | + static Configuration createConfiguration(final String name, ConfigurationBuilder<BuiltConfiguration> builder) { |
| 29 | + AppenderComponentBuilder console = builder.newAppender("Stdout", "Console"); |
| 30 | + LayoutComponentBuilder layout = builder.newLayout("PatternLayout") |
| 31 | + .addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"); |
| 32 | + console.add(layout); |
| 33 | + FilterComponentBuilder filter = builder.newFilter("MarkerFilter", Filter.Result.ACCEPT, Filter.Result.DENY); |
| 34 | + filter.addAttribute("marker", "FLOW"); |
| 35 | + console.add(filter); |
| 36 | + builder.add(console); |
| 37 | + ComponentBuilder triggeringPolicies = builder.newComponent("Policies") |
| 38 | + .addComponent(builder.newComponent("CronTriggeringPolicy") |
| 39 | + .addAttribute("schedule", "0 0 0 * * ?")) |
| 40 | + .addComponent(builder.newComponent("SizeBasedTriggeringPolicy") |
| 41 | + .addAttribute("size", "100M")); |
| 42 | + AppenderComponentBuilder rollingFile = builder.newAppender("rolling", "RollingFile"); |
| 43 | + rollingFile.addAttribute("fileName", "target/rolling.log"); |
| 44 | + rollingFile.addAttribute("filePattern", "target/archive/rolling-%d{MM-dd-yy}.log.gz"); |
| 45 | + rollingFile.add(layout); |
| 46 | + rollingFile.addComponent(triggeringPolicies); |
| 47 | + builder.add(rollingFile); |
| 48 | + AppenderComponentBuilder file = builder.newAppender("FileSystem", "File"); |
| 49 | + file.addAttribute("fileName", "target/logging.log"); |
| 50 | + file.add(layout); |
| 51 | + builder.add(file); |
| 52 | + LoggerComponentBuilder logger = builder.newLogger("com", Level.DEBUG); |
| 53 | + logger.add(builder.newAppenderRef("Stdout")); |
| 54 | + logger.add(builder.newAppenderRef("rolling")); |
| 55 | + logger.add(builder.newAppenderRef("FileSystem")); |
| 56 | + logger.addAttribute("additivity", false); |
| 57 | + builder.add(logger); |
| 58 | + RootLoggerComponentBuilder rootLogger = builder.newRootLogger(Level.ERROR); |
| 59 | + rootLogger.add(builder.newAppenderRef("Stdout")); |
| 60 | + rootLogger.add(builder.newAppenderRef("rolling")); |
| 61 | + rootLogger.add(builder.newAppenderRef("FileSystem")); |
| 62 | + rootLogger.addAttribute("additivity", false); |
| 63 | + builder.add(rootLogger); |
| 64 | + try { |
| 65 | + builder.writeXmlConfiguration(System.out); |
| 66 | + } catch (IOException e) { |
| 67 | + // TODO Auto-generated catch block |
| 68 | + e.printStackTrace(); |
| 69 | + } |
| 70 | + return builder.build(); |
| 71 | + } |
| 72 | + |
| 73 | + public Configuration getConfiguration(final LoggerContext loggerContext, final String name, final URI configLocation) { |
| 74 | + ConfigurationBuilder<BuiltConfiguration> builder = newConfigurationBuilder(); |
| 75 | + return createConfiguration(name, builder); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + protected String[] getSupportedTypes() { |
| 80 | + return new String[] { "*" }; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) { |
| 85 | + return getConfiguration(loggerContext, source.toString(), null); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments