diff --git a/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/pom.xml b/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/pom.xml deleted file mode 100644 index 74464a9631..0000000000 --- a/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/pom.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - 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 deleted file mode 100644 index e92c66f168..0000000000 --- a/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/main/java/com/baeldung/log4j2/config/CustomXMLConfigurationFactory.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - 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 deleted file mode 100644 index 45ee421316..0000000000 --- a/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/main/java/com/baeldung/log4j2/config/MyXMLConfiguration.java +++ /dev/null @@ -1,35 +0,0 @@ -/** - 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 deleted file mode 100644 index 36823c8122..0000000000 --- a/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/main/resources/log4j2.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - \ 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 deleted file mode 100644 index 993c0d0648..0000000000 --- a/logging-modules/log4j2-programmatic-configuration/modify-xml-configuration/src/test/java/com/baeldung/log4j2/logtest/LogTest.java +++ /dev/null @@ -1,23 +0,0 @@ -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 deleted file mode 100644 index cd3aced397..0000000000 --- a/logging-modules/log4j2-programmatic-configuration/pom.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - 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 deleted file mode 100644 index f2a72563e9..0000000000 --- a/logging-modules/log4j2-programmatic-configuration/set-configuration-factory/pom.xml +++ /dev/null @@ -1,10 +0,0 @@ - - 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 deleted file mode 100644 index 9c48702ba0..0000000000 --- a/logging-modules/log4j2-programmatic-configuration/set-configuration-factory/src/main/java/com/baeldung/log4j2/config/CustomConfigurationFactory.java +++ /dev/null @@ -1,88 +0,0 @@ -/** - 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/simple-configuration-xml/pom.xml b/logging-modules/log4j2-programmatic-configuration/simple-configuration-xml/pom.xml deleted file mode 100644 index de8c1ff70b..0000000000 --- a/logging-modules/log4j2-programmatic-configuration/simple-configuration-xml/pom.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - 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 deleted file mode 100644 index 4c49d85471..0000000000 --- a/logging-modules/log4j2-programmatic-configuration/simple-configuration-xml/src/main/resources/log4j2.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ 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 deleted file mode 100644 index f32e0796b6..0000000000 --- a/logging-modules/log4j2-programmatic-configuration/simple-configuration-xml/src/test/java/com/baeldung/log4j2/logtest/LogTest.java +++ /dev/null @@ -1,30 +0,0 @@ -/** - 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 deleted file mode 100644 index 0f9e5be3ff..0000000000 --- a/logging-modules/log4j2-programmatic-configuration/simple-configuration/pom.xml +++ /dev/null @@ -1,10 +0,0 @@ - - 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-configurator/pom.xml b/logging-modules/log4j2-programmatic-configuration/simple-configurator/pom.xml deleted file mode 100644 index 4e7350f785..0000000000 --- a/logging-modules/log4j2-programmatic-configuration/simple-configurator/pom.xml +++ /dev/null @@ -1,10 +0,0 @@ - - 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/pom.xml b/logging-modules/log4j2/pom.xml index 651e1e7d1f..e2ec67a5b5 100644 --- a/logging-modules/log4j2/pom.xml +++ b/logging-modules/log4j2/pom.xml @@ -53,13 +53,25 @@ test-jar test - + - 2.9.3 + 2.9.5 1.4.193 2.1.1 - 2.10.0 + 2.11.0 + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.7.0 + + none + + + + diff --git a/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/Log4j2Test.java b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/Log4j2Test.java new file mode 100644 index 0000000000..abd92a2202 --- /dev/null +++ b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/Log4j2Test.java @@ -0,0 +1,18 @@ +package com.baeldung.logging.log4j2; + +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.spi.LoggerContextFactory; +import org.junit.AfterClass; + +import java.lang.reflect.Field; + +public class Log4j2Test { + @AfterClass + public static void tearDown() throws Exception { + Field factories = ConfigurationFactory.class.getDeclaredField("factories"); + factories.setAccessible(true); + factories.set(null, null); + ConfigurationFactory.resetConfigurationFactory(); + + } +} diff --git a/logging-modules/log4j2-programmatic-configuration/set-configuration-factory/src/test/java/com/baeldung/log4j2/logtest/LogTest.java b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/setconfigurationfactory/SetConfigurationFactoryTest.java similarity index 66% rename from logging-modules/log4j2-programmatic-configuration/set-configuration-factory/src/test/java/com/baeldung/log4j2/logtest/LogTest.java rename to logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/setconfigurationfactory/SetConfigurationFactoryTest.java index bf78a04dc4..2f9a837424 100644 --- a/logging-modules/log4j2-programmatic-configuration/set-configuration-factory/src/test/java/com/baeldung/log4j2/logtest/LogTest.java +++ b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/setconfigurationfactory/SetConfigurationFactoryTest.java @@ -2,32 +2,34 @@ 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; +package com.baeldung.logging.log4j2.setconfigurationfactory; +import com.baeldung.logging.log4j2.Log4j2Test; +import com.baeldung.logging.log4j2.simpleconfiguration.CustomConfigurationFactory; 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.BeforeClass; 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 { +public class SetConfigurationFactoryTest extends Log4j2Test { + @BeforeClass + public static void setUp() { CustomConfigurationFactory customConfigurationFactory = new CustomConfigurationFactory(); ConfigurationFactory.setConfigurationFactory(customConfigurationFactory); } @Test - public void simpleProgrammaticConfiguration() { - Logger logger = LogManager.getLogger(); + public void givenDirectConfiguration_whenUsingFlowMarkers_ThenLogsCorrectly() { + Logger logger = LogManager.getLogger(this.getClass()); Marker markerContent = MarkerManager.getMarker("FLOW"); logger.debug(markerContent, "Debug log message"); logger.info(markerContent, "Info log message"); logger.error(markerContent, "Error log message"); } -} +} \ 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/src/test/java/com/baeldung/logging/log4j2/simpleconfiguration/CustomConfigurationFactory.java similarity index 97% rename from logging-modules/log4j2-programmatic-configuration/simple-configuration/src/main/java/com/baeldung/log4j2/config/CustomConfigurationFactory.java rename to logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/simpleconfiguration/CustomConfigurationFactory.java index ca3cfa142d..6beb540115 100644 --- a/logging-modules/log4j2-programmatic-configuration/simple-configuration/src/main/java/com/baeldung/log4j2/config/CustomConfigurationFactory.java +++ b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/simpleconfiguration/CustomConfigurationFactory.java @@ -3,8 +3,7 @@ the configuration factory, as described in Section 3 of "Programmatic Configuration with Log4j 2" **/ - -package com.baeldung.log4j2.config; +package com.baeldung.logging.log4j2.simpleconfiguration; import java.io.IOException; import java.net.URI; @@ -26,7 +25,7 @@ import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuild 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) +@Plugin(name = "simple", category = ConfigurationFactory.CATEGORY) @Order(50) public class CustomConfigurationFactory extends ConfigurationFactory { @@ -92,3 +91,4 @@ public class CustomConfigurationFactory extends ConfigurationFactory { 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/src/test/java/com/baeldung/logging/log4j2/simpleconfiguration/SimpleConfigurationTest.java similarity index 51% rename from logging-modules/log4j2-programmatic-configuration/simple-configuration/src/test/java/com/baeldung/log4j2/logtest/LogTest.java rename to logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/simpleconfiguration/SimpleConfigurationTest.java index 5637a16508..02330a808b 100644 --- a/logging-modules/log4j2-programmatic-configuration/simple-configuration/src/test/java/com/baeldung/log4j2/logtest/LogTest.java +++ b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/simpleconfiguration/SimpleConfigurationTest.java @@ -2,21 +2,29 @@ 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; +package com.baeldung.logging.log4j2.simpleconfiguration; +import com.baeldung.logging.log4j2.Log4j2Test; 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.plugins.util.PluginManager; +import org.junit.BeforeClass; import org.junit.Test; -public class LogTest { +public class SimpleConfigurationTest extends Log4j2Test { + @BeforeClass + public static void setUp() { + PluginManager.addPackage("com.baeldung.logging.log4j2.simpleconfiguration"); + } + @Test - public void simpleProgrammaticConfiguration() { - Logger logger = LogManager.getLogger(); + public void givenSimpleConfigurationPlugin_whenUsingFlowMarkers_thenLogsCorrectly() throws Exception { + Logger logger = LogManager.getLogger(this.getClass()); Marker markerContent = MarkerManager.getMarker("FLOW"); logger.debug(markerContent, "Debug log message"); logger.info(markerContent, "Info log message"); logger.error(markerContent, "Error log message"); } -} +} \ No newline at end of file diff --git a/logging-modules/log4j2-programmatic-configuration/simple-configurator/src/test/java/com/baeldung/log4j2/logtest/LogPrinter.java b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/simpleconfigurator/LogPrinter.java similarity index 84% rename from logging-modules/log4j2-programmatic-configuration/simple-configurator/src/test/java/com/baeldung/log4j2/logtest/LogPrinter.java rename to logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/simpleconfigurator/LogPrinter.java index d96808c105..d13de25ab9 100644 --- a/logging-modules/log4j2-programmatic-configuration/simple-configurator/src/test/java/com/baeldung/log4j2/logtest/LogPrinter.java +++ b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/simpleconfigurator/LogPrinter.java @@ -1,4 +1,5 @@ -package com.baeldung.log4j2.logtest; +package com.baeldung.logging.log4j2.simpleconfigurator; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -12,4 +13,4 @@ public class LogPrinter { logger.info("Info log message"); logger.error("Error log message"); } -} +} \ 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/src/test/java/com/baeldung/logging/log4j2/simpleconfigurator/SimpleConfiguratorTest.java similarity index 60% rename from logging-modules/log4j2-programmatic-configuration/simple-configurator/src/test/java/com/baeldung/log4j2/configure/LogTest.java rename to logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/simpleconfigurator/SimpleConfiguratorTest.java index a5a10426ac..03bf996120 100644 --- a/logging-modules/log4j2-programmatic-configuration/simple-configurator/src/test/java/com/baeldung/log4j2/configure/LogTest.java +++ b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/simpleconfigurator/SimpleConfiguratorTest.java @@ -1,10 +1,11 @@ /** - This class demonstrates how to use ConfigurationBuilderFactory directly, - as described in Section 3 of "Programmatic Configuration with Log4j 2" -**/ + * This class demonstrates how to use ConfigurationBuilderFactory directly, + * as described in Section 3 of "Programmatic Configuration with Log4j 2" + **/ -package com.baeldung.log4j2.configure; +package com.baeldung.logging.log4j2.simpleconfigurator; +import com.baeldung.logging.log4j2.Log4j2Test; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.appender.ConsoleAppender; import org.apache.logging.log4j.core.config.Configurator; @@ -16,23 +17,22 @@ 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 { +public class SimpleConfiguratorTest extends Log4j2Test { + @Test - public void simpleProgrammaticConfiguration() { + public void givenDefaultLog4j2Environment_whenProgrammaticallyConfigured_thenLogsCorrectly() { ConfigurationBuilder builder = ConfigurationBuilderFactory.newConfigurationBuilder(); AppenderComponentBuilder console = builder.newAppender("Stdout", "CONSOLE") - .addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT); + .addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT); console.add(builder.newLayout("PatternLayout") - .addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable")); + .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)); + .add(builder.newAppenderRef("Stdout")) + .addAttribute("additivity", false)); builder.add(builder.newRootLogger(Level.ERROR) - .add(builder.newAppenderRef("Stdout"))); + .add(builder.newAppenderRef("Stdout"))); Configurator.initialize(builder.build()); LogPrinter logPrinter = new LogPrinter(); logPrinter.printlog(); diff --git a/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/JSONLayoutTest.java b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/JSONLayoutTest.java index 9493c32094..7a6fbf999c 100644 --- a/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/JSONLayoutTest.java +++ b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/JSONLayoutTest.java @@ -6,6 +6,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; +import com.baeldung.logging.log4j2.Log4j2Test; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.junit.Before; @@ -13,7 +14,7 @@ import org.junit.Test; import com.fasterxml.jackson.databind.ObjectMapper; -public class JSONLayoutTest { +public class JSONLayoutTest extends Log4j2Test { private static Logger logger; private ByteArrayOutputStream consoleOutput = new ByteArrayOutputStream(); diff --git a/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/xmlconfiguration/CustomXMLConfigurationFactory.java b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/xmlconfiguration/CustomXMLConfigurationFactory.java new file mode 100644 index 0000000000..f2392d9f45 --- /dev/null +++ b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/xmlconfiguration/CustomXMLConfigurationFactory.java @@ -0,0 +1,24 @@ +package com.baeldung.logging.log4j2.xmlconfiguration; + +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 = "xml", 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/src/test/java/com/baeldung/logging/log4j2/xmlconfiguration/MyXMLConfiguration.java b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/xmlconfiguration/MyXMLConfiguration.java new file mode 100644 index 0000000000..25d2536694 --- /dev/null +++ b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/xmlconfiguration/MyXMLConfiguration.java @@ -0,0 +1,36 @@ +/** +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.logging.log4j2.xmlconfiguration; + +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); + } +} \ No newline at end of file diff --git a/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/xmlconfiguration/XMLConfigLogTest.java b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/xmlconfiguration/XMLConfigLogTest.java new file mode 100644 index 0000000000..41f733804a --- /dev/null +++ b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/xmlconfiguration/XMLConfigLogTest.java @@ -0,0 +1,44 @@ + +/** + 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.logging.log4j2.xmlconfiguration; + +import com.baeldung.logging.log4j2.Log4j2Test; +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.LoggerContext; +import org.apache.logging.log4j.core.config.plugins.util.PluginManager; +import org.junit.BeforeClass; +import org.junit.Test; + +public class XMLConfigLogTest extends Log4j2Test { + + @BeforeClass + public static void setUp() { + PluginManager.addPackage("com.baeldung.logging.log4j2.xmlconfiguration"); + } + + @Test + public void givenXMLConfigurationPlugin_whenUsingFlowMarkers_ThenLogsCorrectly() throws Exception { + Logger logger = LogManager.getLogger(this.getClass()); + Marker markerContent = MarkerManager.getMarker("FLOW"); + logger.debug(markerContent, "Debug log message"); + logger.info(markerContent, "Info log message"); + logger.error(markerContent, "Error log message"); + } + + @Test + public void givenXMLConfigurationPlugin_whenSimpleLog_ThenLogsCorrectly() throws Exception { + Logger logger = LogManager.getLogger(this.getClass()); + LoggerContext ctx = (LoggerContext) LogManager.getContext(); + logger.debug("Debug log message"); + logger.info("Info log message"); + logger.error("Error log message"); + } +} diff --git a/pom.xml b/pom.xml index 16e826e4e9..452c7838f7 100644 --- a/pom.xml +++ b/pom.xml @@ -94,7 +94,6 @@ logging-modules/log-mdc logging-modules/log4j logging-modules/log4j2 - logging-modules/log4j2-programmatic-configuration logging-modules/logback lombok mapstruct