From 83282c54d1099d11225605e4234df1a386f5ab13 Mon Sep 17 00:00:00 2001 From: Ehsan <56245694+ehsansasanian@users.noreply.github.com> Date: Fri, 2 Sep 2022 16:21:27 +0200 Subject: [PATCH] JAVA-14450: fix log4j2 MapAppender, and JSONLayout integration tests (#12681) --- .../log4j2/consoleandfile/Log4j2ConsoleAndFile.java | 2 ++ .../src/main/resources/{log4j2.properties => log4j2} | 0 .../log4j2/appender/MapAppenderIntegrationTest.java | 8 +++++--- 3 files changed, 7 insertions(+), 3 deletions(-) rename logging-modules/log4j2/src/main/resources/{log4j2.properties => log4j2} (100%) diff --git a/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/consoleandfile/Log4j2ConsoleAndFile.java b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/consoleandfile/Log4j2ConsoleAndFile.java index 56119efd9e..598322cdc6 100644 --- a/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/consoleandfile/Log4j2ConsoleAndFile.java +++ b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/consoleandfile/Log4j2ConsoleAndFile.java @@ -7,6 +7,8 @@ public class Log4j2ConsoleAndFile { private static final Logger logger = LogManager.getLogger(Log4j2ConsoleAndFile.class); + // To enable the console and file appender, just rename the log4j2 file, under /src/main/resources, to log4j2.properties + // Reason: Having the log4j2.properties file overrides the log4j2.xml file, so that other appender classes would not work public static void main(String[] args) { logger.info("Hello World!"); logger.debug("Hello World!"); diff --git a/logging-modules/log4j2/src/main/resources/log4j2.properties b/logging-modules/log4j2/src/main/resources/log4j2 similarity index 100% rename from logging-modules/log4j2/src/main/resources/log4j2.properties rename to logging-modules/log4j2/src/main/resources/log4j2 diff --git a/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/appender/MapAppenderIntegrationTest.java b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/appender/MapAppenderIntegrationTest.java index e340ebb784..21ec85ec80 100644 --- a/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/appender/MapAppenderIntegrationTest.java +++ b/logging-modules/log4j2/src/test/java/com/baeldung/logging/log4j2/appender/MapAppenderIntegrationTest.java @@ -23,13 +23,15 @@ public class MapAppenderIntegrationTest { @Test public void whenLoggerEmitsLoggingEvent_thenAppenderReceivesEvent() throws Exception { - logger.error("Error log message from {}", this.getClass() - .getSimpleName()); LoggerContext context = LoggerContext.getContext(false); Configuration config = context.getConfiguration(); MapAppender appender = config.getAppender("MapAppender"); + int eventSizeBeforeLog = appender.getEventMap() + .size(); + logger.error("Error log message from {}", this.getClass() + .getSimpleName()); assertEquals(appender.getEventMap() - .size(), 1); + .size(), ++eventSizeBeforeLog); } }