Search in sources :

Example 86 with LogEvent

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

the class LevelRangeFilterTest method testLevels.

@Test
public void testLevels() {
    final LevelRangeFilter filter = LevelRangeFilter.createFilter(Level.ERROR, Level.INFO, null, null);
    filter.start();
    assertTrue(filter.isStarted());
    assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null));
    assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.ERROR, null, (Object) null, (Throwable) null));
    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));
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 87 with LogEvent

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

the class MarkerFilterTest method testMarkers.

@Test
public void testMarkers() {
    final Marker parent = MarkerManager.getMarker("Parent");
    final Marker child = MarkerManager.getMarker("Child").setParents(parent);
    final Marker grandChild = MarkerManager.getMarker("GrandChild").setParents(child);
    final Marker sibling = MarkerManager.getMarker("Sibling").setParents(parent);
    final Marker stranger = MarkerManager.getMarker("Stranger");
    MarkerFilter filter = MarkerFilter.createFilter("Parent", null, null);
    filter.start();
    assertTrue(filter.isStarted());
    assertSame(Filter.Result.DENY, filter.filter(null, null, stranger, (Object) null, (Throwable) null));
    assertSame(Filter.Result.NEUTRAL, filter.filter(null, null, child, (Object) null, (Throwable) null));
    assertSame(Filter.Result.NEUTRAL, filter.filter(null, null, grandChild, (Object) null, (Throwable) null));
    filter.stop();
    LogEvent event = //
    Log4jLogEvent.newBuilder().setMarker(//
    grandChild).setLevel(//
    Level.DEBUG).setMessage(new SimpleMessage("Hello, world!")).build();
    assertSame(Filter.Result.NEUTRAL, filter.filter(event));
    filter = MarkerFilter.createFilter("Child", null, null);
    filter.start();
    assertSame(Filter.Result.NEUTRAL, filter.filter(event));
    event = //
    Log4jLogEvent.newBuilder().setMarker(//
    sibling).setLevel(//
    Level.DEBUG).setMessage(new SimpleMessage("Hello, world!")).build();
    assertSame(Filter.Result.DENY, filter.filter(event));
    filter.stop();
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Marker(org.apache.logging.log4j.Marker) Test(org.junit.Test)

Example 88 with LogEvent

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

the class RegexFilterTest method testThresholds.

@Test
public void testThresholds() throws Exception {
    RegexFilter filter = RegexFilter.createFilter(".* test .*", null, false, null, null);
    filter.start();
    assertTrue(filter.isStarted());
    assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.DEBUG, null, (Object) "This is a test message", (Throwable) null));
    assertSame(Filter.Result.DENY, filter.filter(null, Level.ERROR, null, (Object) "This is not a test", (Throwable) null));
    LogEvent event = //
    Log4jLogEvent.newBuilder().setLevel(//
    Level.DEBUG).setMessage(//
    new SimpleMessage("Another test message")).build();
    assertSame(Filter.Result.NEUTRAL, filter.filter(event));
    event = //
    Log4jLogEvent.newBuilder().setLevel(//
    Level.ERROR).setMessage(//
    new SimpleMessage("test")).build();
    assertSame(Filter.Result.DENY, filter.filter(event));
    filter = RegexFilter.createFilter(null, null, false, null, null);
    assertNull(filter);
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 89 with LogEvent

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

the class DynamicThresholdFilterTest method testFilter.

@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();
}
Also used : KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 90 with LogEvent

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

the class NoSqlDatabaseManagerTest method testWriteInternal03.

@Test
public void testWriteInternal03() {
    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 IOException exception1 = new IOException("This is the cause.");
        final SQLException exception2 = new SQLException("This is the result.", exception1);
        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("AnotherMarker").addParents(MarkerManager.getMarker("Parent1").addParents(MarkerManager.getMarker("GrandParent1")), MarkerManager.getMarker("Parent2"))).setThreadId(1L).setThreadName("AnotherThread-B").setThreadPriority(1).setTimeMillis(987654321564L).setThrown(exception2).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.", "AnotherMarker", marker.get("name"));
        assertTrue("The marker parents should be a list.", marker.get("parents") instanceof List);
        @SuppressWarnings("unchecked") final List<Object> markerParents = (List<Object>) marker.get("parents");
        assertEquals("The marker parents should contain two parents", 2, markerParents.size());
        assertTrue("The marker parents[0] should be a map.", markerParents.get(0) instanceof Map);
        @SuppressWarnings("unchecked") final Map<String, Object> parent1 = (Map<String, Object>) markerParents.get(0);
        assertEquals("The first marker parent name is not correct.", "Parent1", parent1.get("name"));
        assertTrue("The marker parents[1] should be a map.", markerParents.get(1) instanceof Map);
        @SuppressWarnings("unchecked") final Map<String, Object> parent2 = (Map<String, Object>) markerParents.get(1);
        assertEquals("The second marker parent name is not correct.", "Parent2", parent2.get("name"));
        assertNull("The second marker should have no parent.", parent2.get("parent"));
        assertTrue("The parent1 parents should be a list.", parent1.get("parents") instanceof List);
        @SuppressWarnings("unchecked") final List<Object> parent1Parents = (List<Object>) parent1.get("parents");
        assertEquals("The parent1 parents should have only one parent", 1, parent1Parents.size());
        assertTrue("The parent1Parents[0] should be a map.", parent1Parents.get(0) instanceof Map);
        @SuppressWarnings("unchecked") final Map<String, Object> parent1parent = (Map<String, Object>) parent1Parents.get(0);
        assertEquals("The first parent1 parent name is not correct.", "GrandParent1", parent1parent.get("name"));
        assertNull("The parent1parent marker should have no parent.", parent1parent.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.sql.SQLException", thrown.get("type"));
        assertEquals("The thrown message is not correct.", "This is the result.", 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.", exception2.getStackTrace().length, stackTrace.size());
        for (int i = 0; i < exception2.getStackTrace().length; i++) {
            final StackTraceElement e1 = exception2.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"));
        }
        assertTrue("The thrown cause should be a map.", thrown.get("cause") instanceof Map);
        @SuppressWarnings("unchecked") final Map<String, Object> cause = (Map<String, Object>) thrown.get("cause");
        assertEquals("The cause type is not correct.", "java.io.IOException", cause.get("type"));
        assertEquals("The cause message is not correct.", "This is the cause.", cause.get("message"));
        assertTrue("The cause stack trace should be a list.", cause.get("stackTrace") instanceof List);
        @SuppressWarnings("unchecked") final List<Map<String, Object>> causeStackTrace = (List<Map<String, Object>>) cause.get("stackTrace");
        assertEquals("The cause stack trace length is not correct.", exception1.getStackTrace().length, causeStackTrace.size());
        for (int i = 0; i < exception1.getStackTrace().length; i++) {
            final StackTraceElement e1 = exception1.getStackTrace()[i];
            final Map<String, Object> e2 = causeStackTrace.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 cause should have no cause.", cause.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 : SQLException(java.sql.SQLException) 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) IOException(java.io.IOException) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) 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