Search in sources :

Example 36 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.jupiter.api.Test)

Example 37 with SimpleMessage

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

Example 38 with SimpleMessage

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

the class MutableLogEventTest method testJavaIoSerializableWithThrown.

@Test
public void testJavaIoSerializableWithThrown() throws Exception {
    final MutableLogEvent evt = new MutableLogEvent();
    evt.setContextData(CONTEXT_DATA);
    evt.setContextStack(STACK);
    evt.setEndOfBatch(true);
    evt.setIncludeLocation(true);
    evt.setLevel(Level.WARN);
    evt.setLoggerFqcn(getClass().getName());
    evt.setLoggerName("loggername");
    evt.setMarker(MarkerManager.getMarker("marked man"));
    // evt.setMessage(new ParameterizedMessage("message in a {}", "bottle")); // TODO ParameterizedMessage serialization
    evt.setMessage(new SimpleMessage("peace for all"));
    evt.setNanoTime(1234);
    evt.setThreadId(987);
    evt.setThreadName("ito");
    evt.setThreadPriority(9);
    evt.setThrown(new Exception());
    evt.setTimeMillis(56789);
    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.getContextData(), evt2.getContextData());
    assertEquals(evt.getContextStack(), evt2.getContextStack());
    assertEquals(evt.getMessage(), evt2.getMessage());
    assertNotNull(evt2.getSource());
    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());
    // nano time is transient in log4j log event
    assertNotEquals(evt.getNanoTime(), evt2.getNanoTime());
    assertEquals(0, evt2.getNanoTime());
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ReusableSimpleMessage(org.apache.logging.log4j.message.ReusableSimpleMessage) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 39 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.jupiter.api.Test)

Example 40 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.jupiter.api.Test)

Aggregations

SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)198 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)148 LogEvent (org.apache.logging.log4j.core.LogEvent)142 Test (org.junit.jupiter.api.Test)128 Message (org.apache.logging.log4j.message.Message)43 Marker (org.apache.logging.log4j.Marker)23 Test (org.junit.Test)21 StringMap (org.apache.logging.log4j.util.StringMap)20 Level (org.apache.logging.log4j.Level)17 IOException (java.io.IOException)13 SortedArrayStringMap (org.apache.logging.log4j.util.SortedArrayStringMap)12 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)11 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)10 LoggerContext (org.apache.logging.log4j.core.LoggerContext)10 ClockFactoryTest (org.apache.logging.log4j.core.time.ClockFactoryTest)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4