From 7d41c7edb7fcb1d057aa85b0bde9c17fdaee3d3a Mon Sep 17 00:00:00 2001 From: ShyamVeda Date: Wed, 2 May 2018 18:41:59 +0530 Subject: [PATCH] Log4j 2 Programmatic Configuration BAEL 1338 (#4057) --- .../modify-xml-configuration/pom.xml | 19 ++++ .../config/CustomXMLConfigurationFactory.java | 29 ++++++ .../log4j2/config/MyXMLConfiguration.java | 35 +++++++ .../src/main/resources/log4j2.xml | 17 ++++ .../com/baeldung/log4j2/logtest/LogTest.java | 23 +++++ .../log4j2-programmatic-configuration/pom.xml | 34 +++++++ .../set-configuration-factory/pom.xml | 10 ++ .../config/CustomConfigurationFactory.java | 88 +++++++++++++++++ .../com/baeldung/log4j2/logtest/LogTest.java | 33 +++++++ .../simple-configuration-xml/pom.xml | 17 ++++ .../src/main/resources/log4j2.xml | 32 +++++++ .../com/baeldung/log4j2/logtest/LogTest.java | 30 ++++++ .../simple-configuration/pom.xml | 10 ++ .../config/CustomConfigurationFactory.java | 94 +++++++++++++++++++ .../com/baeldung/log4j2/logtest/LogTest.java | 22 +++++ .../simple-configurator/pom.xml | 10 ++ .../baeldung/log4j2/configure/LogTest.java | 40 ++++++++ .../baeldung/log4j2/logtest/LogPrinter.java | 15 +++ pom.xml | 1 + 19 files changed, 559 insertions(+) create mode 100644 logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/pom.xml create mode 100644 logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/main/java/com/baeldung/log4j2/config/CustomXMLConfigurationFactory.java create mode 100644 logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/main/java/com/baeldung/log4j2/config/MyXMLConfiguration.java create mode 100644 logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/main/resources/log4j2.xml create mode 100644 logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/test/java/com/baeldung/log4j2/logtest/LogTest.java create mode 100644 logging-modules/log4j2-programmatic-configuration/pom.xml create mode 100644 logging-modules/log4j2-programmatic-configuration/set-configuration-factory/pom.xml create mode 100644 logging-modules/log4j2-programmatic-configuration/set-configuration-factory/src/main/java/com/baeldung/log4j2/config/CustomConfigurationFactory.java create mode 100644 logging-modules/log4j2-programmatic-configuration/set-configuration-factory/src/test/java/com/baeldung/log4j2/logtest/LogTest.java create mode 100644 logging-modules/log4j2-programmatic-configuration/simple-configuration-xml/pom.xml create mode 100644 logging-modules/log4j2-programmatic-configuration/simple-configuration-xml/src/main/resources/log4j2.xml create mode 100644 logging-modules/log4j2-programmatic-configuration/simple-configuration-xml/src/test/java/com/baeldung/log4j2/logtest/LogTest.java create mode 100644 logging-modules/log4j2-programmatic-configuration/simple-configuration/pom.xml create mode 100644 logging-modules/log4j2-programmatic-configuration/simple-configuration/src/main/java/com/baeldung/log4j2/config/CustomConfigurationFactory.java create mode 100644 logging-modules/log4j2-programmatic-configuration/simple-configuration/src/test/java/com/baeldung/log4j2/logtest/LogTest.java create mode 100644 logging-modules/log4j2-programmatic-configuration/simple-configurator/pom.xml create mode 100644 logging-modules/log4j2-programmatic-configuration/simple-configurator/src/test/java/com/baeldung/log4j2/configure/LogTest.java create mode 100644 logging-modules/log4j2-programmatic-configuration/simple-configurator/src/test/java/com/baeldung/log4j2/logtest/LogPrinter.java diff --git a/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/pom.xml b/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/pom.xml new file mode 100644 index 0000000000..74464a9631 --- /dev/null +++ b/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/pom.xml @@ -0,0 +1,19 @@ + + + 4.0.0 + + com.baeldung.log4j2 + log4j2-programmatic-configuration + 0.0.1-SNAPSHOT + + com.baeldung.log4j2 + modify-xml-configuration + 0.0.1-SNAPSHOT + modify-xml-configuration + http://maven.apache.org + + UTF-8 + + diff --git a/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/main/java/com/baeldung/log4j2/config/CustomXMLConfigurationFactory.java b/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/main/java/com/baeldung/log4j2/config/CustomXMLConfigurationFactory.java new file mode 100644 index 0000000000..e92c66f168 --- /dev/null +++ b/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/main/java/com/baeldung/log4j2/config/CustomXMLConfigurationFactory.java @@ -0,0 +1,29 @@ +/** + This class demonstrates on modifying the loaded xml configuration by + extending XMLConfigurationFactory as defined in section 4.4 of + "Programmatic Configuration with Log4j 2" +**/ +package com.baeldung.log4j2.config; + +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.core.config.ConfigurationSource; +import org.apache.logging.log4j.core.config.Order; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory; + +@Plugin(name = "CustomConfigurationFactory", category = ConfigurationFactory.CATEGORY) +@Order(50) +public class CustomXMLConfigurationFactory extends XmlConfigurationFactory { + + @Override + public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) { + return new MyXMLConfiguration(loggerContext, source); + } + + @Override + public String[] getSupportedTypes() { + return new String[] { ".xml", "*" }; + } +} diff --git a/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/main/java/com/baeldung/log4j2/config/MyXMLConfiguration.java b/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/main/java/com/baeldung/log4j2/config/MyXMLConfiguration.java new file mode 100644 index 0000000000..45ee421316 --- /dev/null +++ b/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/main/java/com/baeldung/log4j2/config/MyXMLConfiguration.java @@ -0,0 +1,35 @@ +/** + This class demonstrates on overriding the configuration loaded through xml + as defined in section 4.4 of "Programmatic Configuration with Log4j 2" +**/ +package com.baeldung.log4j2.config; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.Layout; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.appender.FileAppender; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationSource; +import org.apache.logging.log4j.core.config.LoggerConfig; +import org.apache.logging.log4j.core.config.xml.XmlConfiguration; +import org.apache.logging.log4j.core.layout.PatternLayout; + +public class MyXMLConfiguration extends XmlConfiguration { + public MyXMLConfiguration(LoggerContext loggerContext, ConfigurationSource source) { + super(loggerContext, source); + } + + @Override + protected void doConfigure() { + super.doConfigure(); + final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); + Configuration config = ctx.getConfiguration(); + LoggerConfig loggerConfig = config.getLoggerConfig("com"); + final Layout layout = PatternLayout.createLayout("[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n", null, config, null, null, false, false, null, null); + Appender appender = FileAppender.createAppender("target/test.log", "false", "false", "File", "true", "false", "false", "4000", layout, null, "false", null, config); + loggerConfig.addAppender(appender, Level.DEBUG, null); + addAppender(appender); + } +} diff --git a/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/main/resources/log4j2.xml b/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/main/resources/log4j2.xml new file mode 100644 index 0000000000..36823c8122 --- /dev/null +++ b/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/main/resources/log4j2.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/test/java/com/baeldung/log4j2/logtest/LogTest.java b/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/test/java/com/baeldung/log4j2/logtest/LogTest.java new file mode 100644 index 0000000000..993c0d0648 --- /dev/null +++ b/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/test/java/com/baeldung/log4j2/logtest/LogTest.java @@ -0,0 +1,23 @@ +package com.baeldung.log4j2.logtest; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.plugins.util.PluginManager; +import org.junit.Test; + + +public class LogTest { + static{ + PluginManager.addPackage("com.baeldung.log4j2.config"); + } + + @Test + public void simpleProgrammaticConfiguration() { + Logger logger = LogManager.getLogger(); + LoggerContext ctx = (LoggerContext) LogManager.getContext(); + logger.debug("Debug log message"); + logger.info("Info log message"); + logger.error("Error log message"); + } +} diff --git a/logging-modules/log4j2-programmatic-configuration/pom.xml b/logging-modules/log4j2-programmatic-configuration/pom.xml new file mode 100644 index 0000000000..cd3aced397 --- /dev/null +++ b/logging-modules/log4j2-programmatic-configuration/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + com.baeldung.log4j2 + log4j2-programmatic-configuration + 0.0.1-SNAPSHOT + pom + + simple-configuration + set-configuration-factory + simple-configurator + simple-configuration-xml + modify-xml-configuration + + + + junit + junit + 4.12 + test + + + org.apache.logging.log4j + log4j-core + 2.11.0 + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.11.0 + + + diff --git a/logging-modules/log4j2-programmatic-configuration/set-configuration-factory/pom.xml b/logging-modules/log4j2-programmatic-configuration/set-configuration-factory/pom.xml new file mode 100644 index 0000000000..f2a72563e9 --- /dev/null +++ b/logging-modules/log4j2-programmatic-configuration/set-configuration-factory/pom.xml @@ -0,0 +1,10 @@ + + 4.0.0 + + com.baeldung.log4j2 + log4j2-programmatic-configuration + 0.0.1-SNAPSHOT + + set-configuration-factory + \ No newline at end of file diff --git a/logging-modules/log4j2-programmatic-configuration/set-configuration-factory/src/main/java/com/baeldung/log4j2/config/CustomConfigurationFactory.java b/logging-modules/log4j2-programmatic-configuration/set-configuration-factory/src/main/java/com/baeldung/log4j2/config/CustomConfigurationFactory.java new file mode 100644 index 0000000000..9c48702ba0 --- /dev/null +++ b/logging-modules/log4j2-programmatic-configuration/set-configuration-factory/src/main/java/com/baeldung/log4j2/config/CustomConfigurationFactory.java @@ -0,0 +1,88 @@ +/** + This class demonstrates how to build the components of + the configuration factory, as described in Section 3 of + "Programmatic Configuration with Log4j 2" +**/ +package com.baeldung.log4j2.config; + +import java.io.IOException; +import java.net.URI; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.Filter; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.core.config.ConfigurationSource; +import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder; +import org.apache.logging.log4j.core.config.builder.api.ComponentBuilder; +import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder; +import org.apache.logging.log4j.core.config.builder.api.FilterComponentBuilder; +import org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder; +import org.apache.logging.log4j.core.config.builder.api.LoggerComponentBuilder; +import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder; +import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration; + +public class CustomConfigurationFactory extends ConfigurationFactory { + + static Configuration createConfiguration(final String name, ConfigurationBuilder 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("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(); + } + + public Configuration getConfiguration(final LoggerContext loggerContext, final String name, final URI configLocation) { + ConfigurationBuilder builder = newConfigurationBuilder(); + return createConfiguration(name, builder); + } + + @Override + protected String[] getSupportedTypes() { + return new String[] { "*" }; + } + + @Override + public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) { + return getConfiguration(loggerContext, source.toString(), null); + } + +} diff --git a/logging-modules/log4j2-programmatic-configuration/set-configuration-factory/src/test/java/com/baeldung/log4j2/logtest/LogTest.java b/logging-modules/log4j2-programmatic-configuration/set-configuration-factory/src/test/java/com/baeldung/log4j2/logtest/LogTest.java new file mode 100644 index 0000000000..bf78a04dc4 --- /dev/null +++ b/logging-modules/log4j2-programmatic-configuration/set-configuration-factory/src/test/java/com/baeldung/log4j2/logtest/LogTest.java @@ -0,0 +1,33 @@ +/** + This class invokes the configuration factory with static initialization, + as defined in section 4.1 of the "Programmatic Configuration with Log4j 2" +**/ +package com.baeldung.log4j2.logtest; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.MarkerManager; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import com.baeldung.log4j2.config.CustomConfigurationFactory; + +@RunWith(JUnit4.class) +public class LogTest { + static { + CustomConfigurationFactory customConfigurationFactory = new CustomConfigurationFactory(); + ConfigurationFactory.setConfigurationFactory(customConfigurationFactory); + } + + @Test + public void simpleProgrammaticConfiguration() { + Logger logger = LogManager.getLogger(); + Marker markerContent = MarkerManager.getMarker("FLOW"); + logger.debug(markerContent, "Debug log message"); + logger.info(markerContent, "Info log message"); + logger.error(markerContent, "Error log message"); + } +} diff --git a/logging-modules/log4j2-programmatic-configuration/simple-configuration-xml/pom.xml b/logging-modules/log4j2-programmatic-configuration/simple-configuration-xml/pom.xml new file mode 100644 index 0000000000..de8c1ff70b --- /dev/null +++ b/logging-modules/log4j2-programmatic-configuration/simple-configuration-xml/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + com.baeldung.log4j2 + log4j2-programmatic-configuration + 0.0.1-SNAPSHOT + + simple-configuration-xml + simple-configuration-xml + http://maven.apache.org + + UTF-8 + + diff --git a/logging-modules/log4j2-programmatic-configuration/simple-configuration-xml/src/main/resources/log4j2.xml b/logging-modules/log4j2-programmatic-configuration/simple-configuration-xml/src/main/resources/log4j2.xml new file mode 100644 index 0000000000..4c49d85471 --- /dev/null +++ b/logging-modules/log4j2-programmatic-configuration/simple-configuration-xml/src/main/resources/log4j2.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/logging-modules/log4j2-programmatic-configuration/simple-configuration-xml/src/test/java/com/baeldung/log4j2/logtest/LogTest.java b/logging-modules/log4j2-programmatic-configuration/simple-configuration-xml/src/test/java/com/baeldung/log4j2/logtest/LogTest.java new file mode 100644 index 0000000000..f32e0796b6 --- /dev/null +++ b/logging-modules/log4j2-programmatic-configuration/simple-configuration-xml/src/test/java/com/baeldung/log4j2/logtest/LogTest.java @@ -0,0 +1,30 @@ +/** + This class loads the logging configuration from the xml defined in + src/main/resources and uses the same configuration generated through + programmatic configuration as defined in simple-configuration example. +**/ + +package com.baeldung.log4j2.logtest; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.MarkerManager; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + + +@RunWith(JUnit4.class) +public class LogTest { + + @Test + public void simpleProgrammaticConfiguration(){ + Logger logger = LogManager.getLogger(); + Marker markerContent = MarkerManager.getMarker("FLOW"); + logger.debug(markerContent, "Debug log message"); + logger.info(markerContent, "Info log message"); + logger.error(markerContent, "Error log message"); + } + +} diff --git a/logging-modules/log4j2-programmatic-configuration/simple-configuration/pom.xml b/logging-modules/log4j2-programmatic-configuration/simple-configuration/pom.xml new file mode 100644 index 0000000000..0f9e5be3ff --- /dev/null +++ b/logging-modules/log4j2-programmatic-configuration/simple-configuration/pom.xml @@ -0,0 +1,10 @@ + + 4.0.0 + + com.baeldung.log4j2 + log4j2-programmatic-configuration + 0.0.1-SNAPSHOT + + simple-configuration + \ No newline at end of file diff --git a/logging-modules/log4j2-programmatic-configuration/simple-configuration/src/main/java/com/baeldung/log4j2/config/CustomConfigurationFactory.java b/logging-modules/log4j2-programmatic-configuration/simple-configuration/src/main/java/com/baeldung/log4j2/config/CustomConfigurationFactory.java new file mode 100644 index 0000000000..ca3cfa142d --- /dev/null +++ b/logging-modules/log4j2-programmatic-configuration/simple-configuration/src/main/java/com/baeldung/log4j2/config/CustomConfigurationFactory.java @@ -0,0 +1,94 @@ +/** + This class demonstrates how to build the components of + the configuration factory, as described in Section 3 of + "Programmatic Configuration with Log4j 2" +**/ + +package com.baeldung.log4j2.config; + +import java.io.IOException; +import java.net.URI; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.Filter; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.core.config.ConfigurationSource; +import org.apache.logging.log4j.core.config.Order; +import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder; +import org.apache.logging.log4j.core.config.builder.api.ComponentBuilder; +import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder; +import org.apache.logging.log4j.core.config.builder.api.FilterComponentBuilder; +import org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder; +import org.apache.logging.log4j.core.config.builder.api.LoggerComponentBuilder; +import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder; +import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration; +import org.apache.logging.log4j.core.config.plugins.Plugin; + +@Plugin(name = "CustomConfigurationFactory", category = ConfigurationFactory.CATEGORY) +@Order(50) +public class CustomConfigurationFactory extends ConfigurationFactory { + + static Configuration createConfiguration(final String name, ConfigurationBuilder 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(); + + } + + @Override + public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) { + return getConfiguration(loggerContext, source.toString(), null); + } + + public Configuration getConfiguration(final LoggerContext loggerContext, final String name, final URI configLocation) { + ConfigurationBuilder builder = newConfigurationBuilder(); + return createConfiguration(name, builder); + } + + @Override + protected String[] getSupportedTypes() { + return new String[] { "*" }; + } +} diff --git a/logging-modules/log4j2-programmatic-configuration/simple-configuration/src/test/java/com/baeldung/log4j2/logtest/LogTest.java b/logging-modules/log4j2-programmatic-configuration/simple-configuration/src/test/java/com/baeldung/log4j2/logtest/LogTest.java new file mode 100644 index 0000000000..5637a16508 --- /dev/null +++ b/logging-modules/log4j2-programmatic-configuration/simple-configuration/src/test/java/com/baeldung/log4j2/logtest/LogTest.java @@ -0,0 +1,22 @@ +/** + This class invokes the configuration factory through the run time property, + as defined in section 4.2 of the "Programmatic Configuration with Log4j 2" +**/ +package com.baeldung.log4j2.logtest; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.MarkerManager; +import org.junit.Test; + +public class LogTest { + @Test + public void simpleProgrammaticConfiguration() { + Logger logger = LogManager.getLogger(); + Marker markerContent = MarkerManager.getMarker("FLOW"); + logger.debug(markerContent, "Debug log message"); + logger.info(markerContent, "Info log message"); + logger.error(markerContent, "Error log message"); + } +} diff --git a/logging-modules/log4j2-programmatic-configuration/simple-configurator/pom.xml b/logging-modules/log4j2-programmatic-configuration/simple-configurator/pom.xml new file mode 100644 index 0000000000..4e7350f785 --- /dev/null +++ b/logging-modules/log4j2-programmatic-configuration/simple-configurator/pom.xml @@ -0,0 +1,10 @@ + + 4.0.0 + + com.baeldung.log4j2 + log4j2-programmatic-configuration + 0.0.1-SNAPSHOT + + simple-configurator + \ No newline at end of file diff --git a/logging-modules/log4j2-programmatic-configuration/simple-configurator/src/test/java/com/baeldung/log4j2/configure/LogTest.java b/logging-modules/log4j2-programmatic-configuration/simple-configurator/src/test/java/com/baeldung/log4j2/configure/LogTest.java new file mode 100644 index 0000000000..a5a10426ac --- /dev/null +++ b/logging-modules/log4j2-programmatic-configuration/simple-configurator/src/test/java/com/baeldung/log4j2/configure/LogTest.java @@ -0,0 +1,40 @@ +/** + This class demonstrates how to use ConfigurationBuilderFactory directly, + as described in Section 3 of "Programmatic Configuration with Log4j 2" +**/ + +package com.baeldung.log4j2.configure; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.appender.ConsoleAppender; +import org.apache.logging.log4j.core.config.Configurator; +import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder; +import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder; +import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory; +import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import com.baeldung.log4j2.logtest.LogPrinter; + +@RunWith(JUnit4.class) +public class LogTest { + @Test + public void simpleProgrammaticConfiguration() { + ConfigurationBuilder builder = ConfigurationBuilderFactory.newConfigurationBuilder(); + AppenderComponentBuilder console = builder.newAppender("Stdout", "CONSOLE") + .addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT); + console.add(builder.newLayout("PatternLayout") + .addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable")); + builder.add(console); + builder.add(builder.newLogger("com", Level.DEBUG) + .add(builder.newAppenderRef("Stdout")) + .addAttribute("additivity", false)); + builder.add(builder.newRootLogger(Level.ERROR) + .add(builder.newAppenderRef("Stdout"))); + Configurator.initialize(builder.build()); + LogPrinter logPrinter = new LogPrinter(); + logPrinter.printlog(); + } +} diff --git a/logging-modules/log4j2-programmatic-configuration/simple-configurator/src/test/java/com/baeldung/log4j2/logtest/LogPrinter.java b/logging-modules/log4j2-programmatic-configuration/simple-configurator/src/test/java/com/baeldung/log4j2/logtest/LogPrinter.java new file mode 100644 index 0000000000..d96808c105 --- /dev/null +++ b/logging-modules/log4j2-programmatic-configuration/simple-configurator/src/test/java/com/baeldung/log4j2/logtest/LogPrinter.java @@ -0,0 +1,15 @@ +package com.baeldung.log4j2.logtest; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + + +public class LogPrinter { + private Logger logger = LogManager.getLogger(); + + public void printlog() { + logger.debug("Debug log message"); + logger.info("Info log message"); + logger.error("Error log message"); + } +} diff --git a/pom.xml b/pom.xml index 5c89660e78..98e7f76e86 100644 --- a/pom.xml +++ b/pom.xml @@ -91,6 +91,7 @@ logging-modules/log-mdc logging-modules/log4j logging-modules/log4j2 + logging-modules/log4j2-programmatic-configuration logging-modules/logback lombok mapstruct