Search in sources :

Example 61 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage 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 62 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage 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 63 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class Log4jLogEventTest method testJavaIoSerializableWithThrown.

@Test
public void testJavaIoSerializableWithThrown() throws Exception {
    final Error thrown = new InternalError("test error");
    final Log4jLogEvent evt = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    "some.test").setLoggerFqcn(//
    Strings.EMPTY).setLevel(//
    Level.INFO).setMessage(//
    new SimpleMessage("abc")).setThrown(//
    thrown).build();
    final byte[] binary = serialize(evt);
    final Log4jLogEvent evt2 = deserialize(binary);
    assertEquals(evt.getTimeMillis(), evt2.getTimeMillis());
    assertEquals(evt.getLoggerFqcn(), evt2.getLoggerFqcn());
    assertEquals(evt.getLevel(), evt2.getLevel());
    assertEquals(evt.getLoggerName(), evt2.getLoggerName());
    assertEquals(evt.getMarker(), evt2.getMarker());
    assertEquals(evt.getContextMap(), evt2.getContextMap());
    assertEquals(evt.getContextData(), evt2.getContextData());
    assertEquals(evt.getContextStack(), evt2.getContextStack());
    assertEquals(evt.getMessage(), evt2.getMessage());
    assertEquals(evt.getSource(), evt2.getSource());
    assertEquals(evt.getThreadName(), evt2.getThreadName());
    assertNull(evt2.getThrown());
    assertNotNull(evt2.getThrownProxy());
    assertEquals(evt.getThrownProxy(), evt2.getThrownProxy());
    assertEquals(evt.isEndOfBatch(), evt2.isEndOfBatch());
    assertEquals(evt.isIncludeLocation(), evt2.isIncludeLocation());
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test) ClockFactoryTest(org.apache.logging.log4j.core.util.ClockFactoryTest)

Example 64 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage 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 65 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage 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)

Aggregations

SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)130 Test (org.junit.Test)98 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)96 LogEvent (org.apache.logging.log4j.core.LogEvent)93 Message (org.apache.logging.log4j.message.Message)34 Marker (org.apache.logging.log4j.Marker)19 Level (org.apache.logging.log4j.Level)11 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)10 IOException (java.io.IOException)7 LoggerContext (org.apache.logging.log4j.core.LoggerContext)7 ClockFactoryTest (org.apache.logging.log4j.core.util.ClockFactoryTest)6 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)6 HashMap (java.util.HashMap)5 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ObjectInputStream (java.io.ObjectInputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)4