Search in sources :

Example 81 with LogEvent

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

the class ThresholdFilterTest method testThresholds.

@Test
public void testThresholds() {
    final ThresholdFilter filter = ThresholdFilter.createFilter(Level.ERROR, 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 82 with LogEvent

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

the class TimeFilterTest method testTime.

@Test
public void testTime() {
    final TimeFilter filter = TimeFilter.createFilter("02:00:00", "03:00:00", "America/LosAngeles", null, null);
    filter.start();
    assertTrue(filter.isStarted());
    final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/LosAngeles"));
    cal.set(Calendar.HOUR_OF_DAY, 2);
    CLOCKTIME = cal.getTimeInMillis();
    LogEvent event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build();
    assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.ERROR, null, (Object) null, (Throwable) null));
    assertSame(Filter.Result.NEUTRAL, filter.filter(event));
    cal.roll(Calendar.DAY_OF_MONTH, true);
    CLOCKTIME = cal.getTimeInMillis();
    event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build();
    assertSame(Filter.Result.NEUTRAL, filter.filter(event));
    assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.ERROR, null, (Object) null, (Throwable) null));
    cal.set(Calendar.HOUR_OF_DAY, 4);
    CLOCKTIME = cal.getTimeInMillis();
    event = Log4jLogEvent.newBuilder().setTimeMillis(CLOCKTIME).build();
    assertSame(Filter.Result.DENY, filter.filter(null, Level.ERROR, null, (Object) null, (Throwable) null));
    assertSame(Filter.Result.DENY, filter.filter(event));
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Calendar(java.util.Calendar) Test(org.junit.Test) ClockFactoryTest(org.apache.logging.log4j.core.util.ClockFactoryTest)

Example 83 with LogEvent

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

the class Log4jLogEventTest method testToImmutableNotSame.

@Test
public void testToImmutableNotSame() {
    final LogEvent logEvent = new Log4jLogEvent.Builder().setMessage(new ReusableObjectMessage()).build();
    LogEvent immutable = logEvent.toImmutable();
    Assert.assertSame(logEvent, immutable);
    Assert.assertFalse(immutable.getMessage() instanceof ReusableMessage);
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) ReusableMessage(org.apache.logging.log4j.message.ReusableMessage) ReusableObjectMessage(org.apache.logging.log4j.message.ReusableObjectMessage) Test(org.junit.Test) ClockFactoryTest(org.apache.logging.log4j.core.util.ClockFactoryTest)

Example 84 with LogEvent

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

the class Log4jLogEventTest method testEquals.

@SuppressWarnings("deprecation")
@Test
public void testEquals() {
    final Map<String, String> contextMap = new HashMap<>();
    contextMap.put("A", "B");
    ThreadContext.push("first");
    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().setContextMap(//
    contextMap).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(contextMap, event.getContextMap());
    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 = builder(event).build();
    assertEquals("copy constructor builder", event2, event);
    assertEquals("same hashCode", event2.hashCode(), event.hashCode());
    assertEquals(contextMap, event2.getContextMap());
    assertSame(contextStack, event2.getContextStack());
    assertEquals(true, event2.isEndOfBatch());
    assertEquals(true, event2.isIncludeLocation());
    assertSame(Level.FATAL, event2.getLevel());
    assertSame(fqcn, event2.getLoggerFqcn());
    assertSame(name, event2.getLoggerName());
    assertSame(marker, event2.getMarker());
    assertSame(message, event2.getMessage());
    assertEquals(1234567890L, event2.getNanoTime());
    assertSame(stackTraceElement, event2.getSource());
    assertSame(threadName, event2.getThreadName());
    assertSame(exception, event2.getThrown());
    assertEquals(987654321L, event2.getTimeMillis());
    final Map<String, String> differentMap = Collections.emptyMap();
    different("different contextMap", builder(event).setContextMap(differentMap), event);
    different("null contextMap", builder(event).setContextMap(null), event);
    ThreadContext.push("abc");
    final ContextStack contextStack2 = ThreadContext.getImmutableStack();
    different("different contextStack", builder(event).setContextStack(contextStack2), event);
    different("null contextStack", builder(event).setContextStack(null), event);
    different("different EndOfBatch", builder(event).setEndOfBatch(false), event);
    different("different IncludeLocation", builder(event).setIncludeLocation(false), event);
    different("different level", builder(event).setLevel(Level.INFO), event);
    different("null level", builder(event).setLevel(null), event);
    different("different fqcn", builder(event).setLoggerFqcn("different"), event);
    different("null fqcn", builder(event).setLoggerFqcn(null), event);
    different("different name", builder(event).setLoggerName("different"), event);
    try {
        // TODO null logger name throws NPE in equals. Use Objects.requireNonNull in constructor?
        different("null name", builder(event).setLoggerName(null), event);
        fail("Expected NullPointerException");
    } catch (final NullPointerException ok) {
    }
    different("different marker", builder(event).setMarker(MarkerManager.getMarker("different")), event);
    different("null marker", builder(event).setMarker(null), event);
    different("different message", builder(event).setMessage(new ObjectMessage("different")), event);
    try {
        // TODO null message throws NPE in equals(). Use Objects.requireNonNull in constructor?
        different("null message", builder(event).setMessage(null), event);
        fail("Expected NullPointerException");
    } catch (final NullPointerException ok) {
    }
    different("different nanoTime", builder(event).setNanoTime(135), event);
    different("different milliTime", builder(event).setTimeMillis(137), event);
    final StackTraceElement stack2 = new StackTraceElement("XXX", "YYY", "file", 123);
    different("different source", builder(event).setSource(stack2), event);
    different("null source", builder(event).setSource(null), event);
    different("different threadname", builder(event).setThreadName("different"), event);
    different("null threadname", builder(event).setThreadName(null), event);
    different("different exception", builder(event).setThrown(new Error("Boo!")), event);
    different("null exception", builder(event).setThrown(null), event);
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ReusableObjectMessage(org.apache.logging.log4j.message.ReusableObjectMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) ReusableMessage(org.apache.logging.log4j.message.ReusableMessage) Message(org.apache.logging.log4j.message.Message) HashMap(java.util.HashMap) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Marker(org.apache.logging.log4j.Marker) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) IOException(java.io.IOException) ReusableObjectMessage(org.apache.logging.log4j.message.ReusableObjectMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) Test(org.junit.Test) ClockFactoryTest(org.apache.logging.log4j.core.util.ClockFactoryTest)

Example 85 with LogEvent

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

the class Log4jLogEventTest method different.

private void different(final String reason, final Log4jLogEvent.Builder builder, final LogEvent event) {
    final LogEvent other = builder.build();
    assertNotEquals(reason, other, event);
    assertNotEquals(reason + " hashCode", other.hashCode(), event.hashCode());
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent)

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