Search in sources :

Example 91 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class NoSqlDatabaseManagerTest method testWriteInternal02.

@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).setContextMap(context).setContextStack(stack).build();
        manager.writeInternal(event);
        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"));
    }
}
Also used : HashMap(java.util.HashMap) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) ThreadContext(org.apache.logging.log4j.ThreadContext) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 92 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class AbstractLoggerTest method testGlobalLogger.

@Test
public void testGlobalLogger() throws Exception {
    final Logger root = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
    root.info("Test info message");
    root.config("Test info message");
    root.fine("Test info message");
    final List<LogEvent> events = eventAppender.getEvents();
    assertThat(events, hasSize(3));
    for (final LogEvent event : events) {
        final String message = event.getMessage().getFormattedMessage();
        assertThat(message, equalTo("Test info message"));
    }
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) Logger(java.util.logging.Logger) Test(org.junit.Test)

Example 93 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class AbstractLoggerTest method testFlowMessages.

@Test
public void testFlowMessages() {
    final Logger flowLogger = Logger.getLogger("TestFlow");
    flowLogger.entering("com.example.TestSourceClass1", "testSourceMethod1(String)");
    flowLogger.entering("com.example.TestSourceClass2", "testSourceMethod2(String)", "TestParam");
    flowLogger.entering("com.example.TestSourceClass3", "testSourceMethod3(String)", new Object[] { "TestParam0", "TestParam1" });
    final List<LogEvent> events = flowAppender.getEvents();
    assertThat(events, hasSize(3));
    assertEquals("Enter", events.get(0).getMessage().getFormattedMessage());
    assertEquals("Enter params(TestParam)", events.get(1).getMessage().getFormattedMessage());
    assertEquals("Enter params(TestParam0, TestParam1)", events.get(2).getMessage().getFormattedMessage());
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) Logger(java.util.logging.Logger) Test(org.junit.Test)

Example 94 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class BracketInNotInterpolatedMessageTest method noInterpolation.

@Test
public void noInterpolation() {
    final Logger logger = Logger.getLogger("Test");
    logger.info("{raw}");
    // should lead to the same as previous but was not the case LOG4J2-1251
    logger.log(new LogRecord(INFO, "{raw}"));
    final List<LogEvent> events = ListAppender.getListAppender("TestAppender").getEvents();
    assertThat(events, hasSize(2));
    assertEquals("{raw}", events.get(0).getMessage().getFormattedMessage());
    assertEquals("{raw}", events.get(1).getMessage().getFormattedMessage());
}
Also used : LogRecord(java.util.logging.LogRecord) LogEvent(org.apache.logging.log4j.core.LogEvent) Logger(java.util.logging.Logger) Test(org.junit.Test)

Example 95 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class AbstractLoggerTest method testLogParamMarkers.

@Test
public void testLogParamMarkers() {
    final Logger flowLogger = Logger.getLogger("TestFlow");
    flowLogger.logp(java.util.logging.Level.FINER, "sourceClass", "sourceMethod", "ENTER {0}", "params");
    final List<LogEvent> events = flowAppender.getEvents();
    assertEquals("ENTER params", events.get(0).getMessage().getFormattedMessage());
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) Logger(java.util.logging.Logger) Test(org.junit.Test)

Aggregations

LogEvent (org.apache.logging.log4j.core.LogEvent)189 Test (org.junit.Test)150 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)127 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)94 Message (org.apache.logging.log4j.message.Message)33 Marker (org.apache.logging.log4j.Marker)16 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)11 HashMap (java.util.HashMap)9 LoggerContext (org.apache.logging.log4j.core.LoggerContext)8 ClockFactoryTest (org.apache.logging.log4j.core.util.ClockFactoryTest)8 File (java.io.File)7 IOException (java.io.IOException)7 Level (org.apache.logging.log4j.Level)7 Appender (org.apache.logging.log4j.core.Appender)6 MapMessage (org.apache.logging.log4j.message.MapMessage)6 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)5 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)5 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)5