From cf335e30a4af3bdf59db07eb3cb25accf309240a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20R=C3=A4del?= Date: Tue, 4 Oct 2016 02:28:27 +0200 Subject: [PATCH] BAEL-41: Added including tests: - thread context filter and pattern --- ...nsoleAppenderUsingPatternLayoutWithColorsTest.java | 11 +++++++++++ .../log4j2-console-appender_pattern-layout.xml | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingPatternLayoutWithColorsTest.java b/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingPatternLayoutWithColorsTest.java index 635e05c841..1e16ba7d0b 100644 --- a/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingPatternLayoutWithColorsTest.java +++ b/logging/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingPatternLayoutWithColorsTest.java @@ -3,6 +3,7 @@ package com.baeldung.logging.log4j2.tests; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.MarkerManager; +import org.apache.logging.log4j.ThreadContext; import org.apache.logging.log4j.junit.LoggerContextRule; import org.junit.Rule; import org.junit.Test; @@ -35,4 +36,14 @@ public class ConsoleAppenderUsingPatternLayoutWithColorsTest { Marker connectionTrace = MarkerManager.getMarker("CONN_TRACE"); logger.trace(connectionTrace, "This is a marker message at TRACE level."); } + + @Test + public void givenLoggerWithConsoleConfig_shouldFilterByThreadContext() throws Exception { + Logger logger = contextRule.getLogger("UserAudit"); + ThreadContext.put("userId", "1000"); + logger.info("This is a log-visible user login. Maybe from an admin account?"); + ThreadContext.put("userId", "1001"); + logger.info("This is a log-invisible user login."); + boolean b = true; + } } diff --git a/logging/log4j2/src/test/resources/log4j2-console-appender_pattern-layout.xml b/logging/log4j2/src/test/resources/log4j2-console-appender_pattern-layout.xml index 9cd3ccea50..d6621f9166 100644 --- a/logging/log4j2/src/test/resources/log4j2-console-appender_pattern-layout.xml +++ b/logging/log4j2/src/test/resources/log4j2-console-appender_pattern-layout.xml @@ -6,11 +6,20 @@ + + + + + + + + +