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));
}
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));
}
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);
}
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);
}
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());
}
Aggregations