Java Code Examples for org.apache.logging.log4j.ThreadContext#clearMap()
The following examples show how to use
org.apache.logging.log4j.ThreadContext#clearMap() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MdcPatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testConverterFullEmpty() { ThreadContext.clearMap(); final Message msg = new SimpleMessage("Hello"); final MdcPatternConverter converter = MdcPatternConverter.newInstance(null); final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("MyLogger") // .setLevel(Level.DEBUG) // .setMessage(msg) // .build(); final StringBuilder sb = new StringBuilder(); converter.format(event, sb); final String str = sb.toString(); final String expected = "{}"; assertTrue("Incorrect result. Expected " + expected + ", actual " + str, str.equals(expected)); }
Example 2
Source File: AbstractScriptFilterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testJavascriptFilter() throws Exception { final Logger logger = LogManager.getLogger("TestJavaScriptFilter"); logger.traceEntry(); logger.info("This should not be logged"); ThreadContext.put("UserId", "JohnDoe"); logger.info("This should be logged"); ThreadContext.clearMap(); final ListAppender app = getContext().getListAppender("List"); final List<String> messages = app.getMessages(); try { assertNotNull("No Messages", messages); assertTrue("Incorrect number of messages. Expected 2, Actual " + messages.size(), messages.size() == 2); } finally { app.clear(); } }
Example 3
Source File: MdcPatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testConverterWithKeysNonePresent() { ThreadContext.clearMap(); final Message msg = new SimpleMessage("Hello"); final String [] options = new String[] {"object, subject"}; final MdcPatternConverter converter = MdcPatternConverter.newInstance(options); final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("MyLogger") // .setLevel(Level.DEBUG) // .setMessage(msg) // .build(); final StringBuilder sb = new StringBuilder(); converter.format(event, sb); final String str = sb.toString(); final String expected = "{}"; assertEquals(expected, str); }
Example 4
Source File: ThreadContextUtilityClass.java From logging-log4j2 with Apache License 2.0 | 6 votes |
public static void testGetContextReturnsMutableCopy() { ThreadContext.clearMap(); final Map<String, String> map1 = ThreadContext.getContext(); assertTrue(map1.isEmpty()); map1.put("K", "val"); // no error assertEquals("val", map1.get("K")); // adding to copy does not affect thread context map assertTrue(ThreadContext.getContext().isEmpty()); ThreadContext.put("key", "val2"); final Map<String, String> map2 = ThreadContext.getContext(); assertEquals(1, map2.size()); assertEquals("val2", map2.get("key")); map2.put("K", "val"); // no error assertEquals("val", map2.get("K")); // first copy is not affected assertNotSame(map1, map2); assertEquals(1, map1.size()); }
Example 5
Source File: InterpolatorTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testLookup() { final Map<String, String> map = new HashMap<>(); map.put(TESTKEY, TESTVAL); final StrLookup lookup = new Interpolator(new MapLookup(map)); ThreadContext.put(TESTKEY, TESTVAL); String value = lookup.lookup(TESTKEY); assertEquals(TESTVAL, value); value = lookup.lookup("ctx:" + TESTKEY); assertEquals(TESTVAL, value); value = lookup.lookup("sys:" + TESTKEY); assertEquals(TESTVAL, value); value = lookup.lookup("SYS:" + TESTKEY2); assertEquals(TESTVAL, value); value = lookup.lookup("BadKey"); assertNull(value); ThreadContext.clearMap(); value = lookup.lookup("ctx:" + TESTKEY); assertEquals(TESTVAL, value); value = lookup.lookup("jndi:" + TEST_CONTEXT_RESOURCE_NAME); assertEquals(TEST_CONTEXT_NAME, value); }
Example 6
Source File: MdcPatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testConverterFullSingle() { ThreadContext.clearMap(); ThreadContext.put("foo", "bar"); final Message msg = new SimpleMessage("Hello"); final MdcPatternConverter converter = MdcPatternConverter.newInstance(null); final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("MyLogger") // .setLevel(Level.DEBUG) // .setMessage(msg) // .build(); final StringBuilder sb = new StringBuilder(); converter.format(event, sb); final String str = sb.toString(); final String expected = "{foo=bar}"; assertTrue("Incorrect result. Expected " + expected + ", actual " + str, str.equals(expected)); }
Example 7
Source File: DynamicThresholdFilterTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testFilter() { ThreadContext.put("userid", "testuser"); ThreadContext.put("organization", "apache"); final KeyValuePair[] pairs = new KeyValuePair[] { new KeyValuePair("testuser", "DEBUG"), new KeyValuePair("JohnDoe", "warn") }; final DynamicThresholdFilter filter = DynamicThresholdFilter.createFilter("userid", pairs, Level.ERROR, null, null); filter.start(); assertTrue(filter.isStarted()); assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null)); assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.ERROR, null, (Object) null, (Throwable) null)); ThreadContext.clearMap(); ThreadContext.put("userid", "JohnDoe"); ThreadContext.put("organization", "apache"); LogEvent event = Log4jLogEvent.newBuilder().setLevel(Level.DEBUG).setMessage(new SimpleMessage("Test")).build(); assertSame(Filter.Result.DENY, filter.filter(event)); event = Log4jLogEvent.newBuilder().setLevel(Level.ERROR).setMessage(new SimpleMessage("Test")).build(); assertSame(Filter.Result.NEUTRAL, filter.filter(event)); ThreadContext.clearMap(); }
Example 8
Source File: LoggingApp.java From logging-log4j2 with Apache License 2.0 | 6 votes |
public LoggingApp(final String member) { ThreadContext.clearMap(); RequestContext.setSessionId("session1234"); RequestContext.setIpAddress("127.0.0.1"); RequestContext.setClientId("02121"); RequestContext.setProductName("IB"); RequestContext.setProductVersion("4.18.1"); RequestContext.setLocale("en_US"); RequestContext.setRegion("prod"); if (events == null) { events = MockEventsSupplier.getAllEvents(member); } }
Example 9
Source File: ScheduledThreadPoolExecutorExt.java From zstack with Apache License 2.0 | 6 votes |
@Override protected void afterExecute(Runnable r, Throwable t) { ThreadContext.clearMap(); ThreadContext.clearStack(); ThreadAroundHook debugHook = null; List<ThreadAroundHook> tmpHooks; synchronized (_hooks) { tmpHooks = new ArrayList<ThreadAroundHook>(_hooks); } for (ThreadAroundHook hook : tmpHooks) { debugHook = hook; try { hook.afterExecute(r, t); } catch (Exception e) { _logger.warn("Unhandle exception happend during executing ThreadAroundHook: " + debugHook.getClass().getCanonicalName(), e); } } }
Example 10
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void structuredData() { ThreadContext.put("loginId", "JohnDoe"); ThreadContext.put("ipAddress", "192.168.0.120"); ThreadContext.put("locale", Locale.US.getDisplayName()); final StructuredDataMessage msg = new StructuredDataMessage("Audit@18060", "Transfer Complete", "Transfer"); msg.put("ToAccount", "123456"); msg.put("FromAccount", "123457"); msg.put("Amount", "200.00"); logger.info(MarkerManager.getMarker("EVENT"), msg); ThreadContext.clearMap(); final List<LogEvent> events = app.getEvents(); assertEventCount(events, 1); }
Example 11
Source File: Log4jMDCAdapter.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Override @SuppressWarnings("unchecked") // nothing we can do about this, restricted by SLF4J API public void setContextMap(@SuppressWarnings("rawtypes") final Map map) { ThreadContext.clearMap(); for (final Map.Entry<String, String> entry : ((Map<String, String>) map).entrySet()) { ThreadContext.put(entry.getKey(), entry.getValue()); } }
Example 12
Source File: MapRewriteAppenderTest.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@After public void after() { ThreadContext.clearMap(); }
Example 13
Source File: ThreadContextBenchmark.java From logging-log4j2 with Apache License 2.0 | 4 votes |
public void clearAndPut() { ThreadContext.clearMap(); for (int i = 0; i < count; i++) { ThreadContext.put(keys[i], values[i]); } }
Example 14
Source File: BaseEventTest.java From logging-log4j-audit with Apache License 2.0 | 4 votes |
@Before public void before() { app.clear(); ThreadContext.clearMap(); }
Example 15
Source File: Log4jMDCAdapter.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Override public void clear() { ThreadContext.clearMap(); }
Example 16
Source File: ThreadContextUtilityClass.java From logging-log4j2 with Apache License 2.0 | 4 votes |
public static void testGetContextReturnsEmptyMapIfEmpty() { ThreadContext.clearMap(); assertTrue(ThreadContext.getContext().isEmpty()); }
Example 17
Source File: RewriteAppenderTest.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@After public void after() { ThreadContext.clearMap(); }
Example 18
Source File: ThreadContextUtilityClass.java From logging-log4j2 with Apache License 2.0 | 4 votes |
public static void testGetImmutableContextReturnsImmutableMapIfNonEmpty() { ThreadContext.clearMap(); ThreadContext.put("key", "val"); final Map<String, String> immutable = ThreadContext.getImmutableContext(); immutable.put("otherkey", "otherval"); }
Example 19
Source File: ThreadContextUtilityClass.java From logging-log4j2 with Apache License 2.0 | 4 votes |
public static void testGetImmutableContextReturnsImmutableMapIfEmpty() { ThreadContext.clearMap(); final Map<String, String> immutable = ThreadContext.getImmutableContext(); immutable.put("otherkey", "otherval"); }
Example 20
Source File: RequestContext.java From audit-log-plugin with MIT License | 2 votes |
/** * The methods in this class are not required by framework components that use the RequestContext properties. * They are provided as a convenience for applications. If they are not provided the properties can be accessed * directly through the Log4j ThreadContext Map using the keys above. */ public static void clear() { ThreadContext.clearMap(); }