Java Code Examples for org.apache.logging.log4j.ThreadContext#getImmutableStack()
The following examples show how to use
org.apache.logging.log4j.ThreadContext#getImmutableStack() .
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: AsyncLogger.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Override public void translateTo(final RingBufferLogEvent event, final long sequence, final Object... args) { // Implementation note: candidate for optimization: exceeds 35 bytecodes. final AsyncLogger asyncLogger = (AsyncLogger) args[0]; final StackTraceElement location = (StackTraceElement) args[1]; final String fqcn = (String) args[2]; final Level level = (Level) args[3]; final Marker marker = (Marker) args[4]; final Message message = (Message) args[5]; final Throwable thrown = (Throwable) args[6]; // needs shallow copy to be fast (LOG4J2-154) final ContextStack contextStack = ThreadContext.getImmutableStack(); final Thread currentThread = Thread.currentThread(); final String threadName = THREAD_NAME_CACHING_STRATEGY.getThreadName(); event.setValues(asyncLogger, asyncLogger.getName(), marker, fqcn, level, message, thrown, // config properties are taken care of in the EventHandler thread // in the AsyncLogger#actualAsyncLog method CONTEXT_DATA_INJECTOR.injectContextData(null, (StringMap) event.getContextData()), contextStack, currentThread.getId(), threadName, currentThread.getPriority(), location, CLOCK, nanoClock); }
Example 2
Source File: Log4jLogEventTest.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Test public void testBuilderCorrectlyCopiesAllEventAttributes() { final StringMap contextData = ContextDataFactory.createContextData(); contextData.putValue("A", "B"); final ContextStack contextStack = ThreadContext.getImmutableStack(); final Exception exception = new Exception("test"); final Marker marker = MarkerManager.getMarker("EVENTTEST"); final Message message = new SimpleMessage("foo"); final StackTraceElement stackTraceElement = new StackTraceElement("A", "B", "file", 123); final String fqcn = "qualified"; final String name = "Ceci n'est pas une pipe"; final String threadName = "threadName"; final Log4jLogEvent event = Log4jLogEvent.newBuilder() // .setContextData(contextData) // .setContextStack(contextStack) // .setEndOfBatch(true) // .setIncludeLocation(true) // .setLevel(Level.FATAL) // .setLoggerFqcn(fqcn) // .setLoggerName(name) // .setMarker(marker) // .setMessage(message) // .setNanoTime(1234567890L) // .setSource(stackTraceElement) // .setThreadName(threadName) // .setThrown(exception) // .setTimeMillis(987654321L) .build(); assertEquals(contextData, event.getContextData()); assertSame(contextStack, event.getContextStack()); assertEquals(true, event.isEndOfBatch()); assertEquals(true, event.isIncludeLocation()); assertSame(Level.FATAL, event.getLevel()); assertSame(fqcn, event.getLoggerFqcn()); assertSame(name, event.getLoggerName()); assertSame(marker, event.getMarker()); assertSame(message, event.getMessage()); assertEquals(1234567890L, event.getNanoTime()); assertSame(stackTraceElement, event.getSource()); assertSame(threadName, event.getThreadName()); assertSame(exception, event.getThrown()); assertEquals(987654321L, event.getTimeMillis()); final LogEvent event2 = new Log4jLogEvent.Builder(event).build(); assertEquals("copy constructor builder", event2, event); assertEquals("same hashCode", event2.hashCode(), event.hashCode()); }
Example 3
Source File: Log4jLogEventTest.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Test public void testBuilderCorrectlyCopiesAllEventAttributesInclContextData() { final StringMap contextData = new SortedArrayStringMap(); contextData.putValue("A", "B"); final ContextStack contextStack = ThreadContext.getImmutableStack(); final Exception exception = new Exception("test"); final Marker marker = MarkerManager.getMarker("EVENTTEST"); final Message message = new SimpleMessage("foo"); final StackTraceElement stackTraceElement = new StackTraceElement("A", "B", "file", 123); final String fqcn = "qualified"; final String name = "Ceci n'est pas une pipe"; final String threadName = "threadName"; final Log4jLogEvent event = Log4jLogEvent.newBuilder() // .setContextData(contextData) // .setContextStack(contextStack) // .setEndOfBatch(true) // .setIncludeLocation(true) // .setLevel(Level.FATAL) // .setLoggerFqcn(fqcn) // .setLoggerName(name) // .setMarker(marker) // .setMessage(message) // .setNanoTime(1234567890L) // .setSource(stackTraceElement) // .setThreadName(threadName) // .setThrown(exception) // .setTimeMillis(987654321L) .build(); assertSame(contextData, event.getContextData()); assertSame(contextStack, event.getContextStack()); assertEquals(true, event.isEndOfBatch()); assertEquals(true, event.isIncludeLocation()); assertSame(Level.FATAL, event.getLevel()); assertSame(fqcn, event.getLoggerFqcn()); assertSame(name, event.getLoggerName()); assertSame(marker, event.getMarker()); assertSame(message, event.getMessage()); assertEquals(1234567890L, event.getNanoTime()); assertSame(stackTraceElement, event.getSource()); assertSame(threadName, event.getThreadName()); assertSame(exception, event.getThrown()); assertEquals(987654321L, event.getTimeMillis()); final LogEvent event2 = new Log4jLogEvent.Builder(event).build(); assertEquals("copy constructor builder", event2, event); assertEquals("same hashCode", event2.hashCode(), event.hashCode()); }
Example 4
Source File: Log4jLogEventTest.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Test public void testBuilderCorrectlyCopiesMutableLogEvent() throws Exception { final StringMap contextData = new SortedArrayStringMap(); contextData.putValue("A", "B"); final ContextStack contextStack = ThreadContext.getImmutableStack(); final Exception exception = new Exception("test"); final Marker marker = MarkerManager.getMarker("EVENTTEST"); final Message message = new SimpleMessage("foo"); new StackTraceElement("A", "B", "file", 123); final String fqcn = "qualified"; final String name = "Ceci n'est pas une pipe"; final String threadName = "threadName"; final MutableLogEvent event = new MutableLogEvent(); event.setContextData(contextData); event.setContextStack(contextStack); event.setEndOfBatch(true); event.setIncludeLocation(true); //event.setSource(stackTraceElement); // cannot be explicitly set event.setLevel(Level.FATAL); event.setLoggerFqcn(fqcn); event.setLoggerName(name); event.setMarker(marker); event.setMessage(message); event.setNanoTime(1234567890L); event.setThreadName(threadName); event.setThrown(exception); event.setTimeMillis(987654321L); assertSame(contextData, event.getContextData()); assertSame(contextStack, event.getContextStack()); assertEquals(true, event.isEndOfBatch()); assertEquals(true, event.isIncludeLocation()); assertSame(Level.FATAL, event.getLevel()); assertSame(fqcn, event.getLoggerFqcn()); assertSame(name, event.getLoggerName()); assertSame(marker, event.getMarker()); assertSame(message, event.getMessage()); assertEquals(1234567890L, event.getNanoTime()); //assertSame(stackTraceElement, event.getSource()); // don't invoke assertSame(threadName, event.getThreadName()); assertSame(exception, event.getThrown()); assertEquals(987654321L, event.getTimeMillis()); final LogEvent e2 = new Log4jLogEvent.Builder(event).build(); assertEquals(contextData, e2.getContextData()); assertSame(contextStack, e2.getContextStack()); assertEquals(true, e2.isEndOfBatch()); assertEquals(true, e2.isIncludeLocation()); assertSame(Level.FATAL, e2.getLevel()); assertSame(fqcn, e2.getLoggerFqcn()); assertSame(name, e2.getLoggerName()); assertSame(marker, e2.getMarker()); assertSame(message, e2.getMessage()); assertEquals(1234567890L, e2.getNanoTime()); //assertSame(stackTraceElement, e2.getSource()); // don't invoke assertSame(threadName, e2.getThreadName()); assertSame(exception, e2.getThrown()); assertEquals(987654321L, e2.getTimeMillis()); // use reflection to get value of source field in log event copy: // invoking the getSource() method would initialize the field final Field fieldSource = Log4jLogEvent.class.getDeclaredField("source"); fieldSource.setAccessible(true); final Object value = fieldSource.get(e2); assertNull("source in copy", value); }
Example 5
Source File: NoSqlDatabaseManagerTest.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Test public void testWriteInternal02() { given(connection.isClosed()).willReturn(false); given(message.getFormattedMessage()).willReturn("Another cool message 02."); try (final NoSqlDatabaseManager<?> manager = NoSqlDatabaseManager.getNoSqlDatabaseManager("name", 0, provider)) { manager.startup(); manager.connectAndStart(); then(provider).should().getConnection(); final RuntimeException exception = new RuntimeException("This is something cool!"); final Map<String, String> context = new HashMap<>(); context.put("hello", "world"); context.put("user", "pass"); ThreadContext.push("message1"); ThreadContext.push("stack2"); final ThreadContext.ContextStack stack = ThreadContext.getImmutableStack(); ThreadContext.clearStack(); final LogEvent event = Log4jLogEvent.newBuilder() .setLevel(Level.DEBUG) .setLoggerName("com.foo.NoSQLDbTest.testWriteInternal02") .setMessage(message) .setSource(new StackTraceElement("com.bar.Foo", "anotherMethod03", "Foo.java", 9)) .setMarker(MarkerManager.getMarker("LoneMarker")) .setThreadId(1L) .setThreadName("AnotherThread-B") .setThreadPriority(1) .setTimeMillis(987654321564L) .setThrown(exception) .setContextData(ContextDataFactory.createContextData(context)) .setContextStack(stack) .build(); manager.writeInternal(event, null); then(connection).should().insertObject(captor.capture()); final NoSqlObject<Map<String, Object>> inserted = captor.getValue(); assertNotNull("The inserted value should not be null.", inserted); final Map<String, Object> object = inserted.unwrap(); assertNotNull("The unwrapped object should not be null.", object); assertEquals("The level is not correct.", Level.DEBUG, object.get("level")); assertEquals("The logger is not correct.", "com.foo.NoSQLDbTest.testWriteInternal02", object.get("loggerName")); assertEquals("The message is not correct.", "Another cool message 02.", object.get("message")); assertEquals("The thread is not correct.", "AnotherThread-B", object.get("threadName")); assertEquals("The millis is not correct.", 987654321564L, object.get("millis")); assertEquals("The date is not correct.", 987654321564L, ((Date) object.get("date")).getTime()); assertTrue("The source should be a map.", object.get("source") instanceof Map); @SuppressWarnings("unchecked") final Map<String, Object> source = (Map<String, Object>) object.get("source"); assertEquals("The class is not correct.", "com.bar.Foo", source.get("className")); assertEquals("The method is not correct.", "anotherMethod03", source.get("methodName")); assertEquals("The file name is not correct.", "Foo.java", source.get("fileName")); assertEquals("The line number is not correct.", 9, source.get("lineNumber")); assertTrue("The marker should be a map.", object.get("marker") instanceof Map); @SuppressWarnings("unchecked") final Map<String, Object> marker = (Map<String, Object>) object.get("marker"); assertEquals("The marker name is not correct.", "LoneMarker", marker.get("name")); assertNull("The marker parent should be null.", marker.get("parent")); assertTrue("The thrown should be a map.", object.get("thrown") instanceof Map); @SuppressWarnings("unchecked") final Map<String, Object> thrown = (Map<String, Object>) object.get("thrown"); assertEquals("The thrown type is not correct.", "java.lang.RuntimeException", thrown.get("type")); assertEquals("The thrown message is not correct.", "This is something cool!", thrown.get("message")); assertTrue("The thrown stack trace should be a list.", thrown.get("stackTrace") instanceof List); @SuppressWarnings("unchecked") final List<Map<String, Object>> stackTrace = (List<Map<String, Object>>) thrown.get("stackTrace"); assertEquals("The thrown stack trace length is not correct.", exception.getStackTrace().length, stackTrace.size()); for (int i = 0; i < exception.getStackTrace().length; i++) { final StackTraceElement e1 = exception.getStackTrace()[i]; final Map<String, Object> e2 = stackTrace.get(i); assertEquals("Element class name [" + i + "] is not correct.", e1.getClassName(), e2.get("className")); assertEquals("Element method name [" + i + "] is not correct.", e1.getMethodName(), e2.get("methodName")); assertEquals("Element file name [" + i + "] is not correct.", e1.getFileName(), e2.get("fileName")); assertEquals("Element line number [" + i + "] is not correct.", e1.getLineNumber(), e2.get("lineNumber")); } assertNull("The thrown should have no cause.", thrown.get("cause")); assertTrue("The context map should be a map.", object.get("contextMap") instanceof Map); assertEquals("The context map is not correct.", context, object.get("contextMap")); assertTrue("The context stack should be list.", object.get("contextStack") instanceof List); assertEquals("The context stack is not correct.", stack.asList(), object.get("contextStack")); } }