use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class ThrowablePatternConverterTest method testShortMessage.
@Test
public void testShortMessage() {
final String[] options = { "short.message" };
final ThrowablePatternConverter converter = ThrowablePatternConverter.newInstance(null, options);
final Throwable cause = new NullPointerException("null pointer");
final Throwable parent = new IllegalArgumentException("IllegalArgument", cause);
final LogEvent event = //
Log4jLogEvent.newBuilder().setLoggerName(//
"testLogger").setLoggerFqcn(//
this.getClass().getName()).setLevel(//
Level.DEBUG).setMessage(//
new SimpleMessage("test exception")).setThrown(parent).build();
final StringBuilder sb = new StringBuilder();
converter.format(event, sb);
final String result = sb.toString();
assertEquals("The messages should be same", "IllegalArgument", result);
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class ThrowablePatternConverterTest method testShortClassName.
@Test
public void testShortClassName() {
final String packageName = "org.apache.logging.log4j.core.pattern.";
final String[] options = { "short.className" };
final ThrowablePatternConverter converter = ThrowablePatternConverter.newInstance(null, options);
final Throwable cause = new NullPointerException("null pointer");
final Throwable parent = new IllegalArgumentException("IllegalArgument", cause);
final LogEvent event = //
Log4jLogEvent.newBuilder().setLoggerName(//
"testLogger").setLoggerFqcn(//
this.getClass().getName()).setLevel(//
Level.DEBUG).setMessage(//
new SimpleMessage("test exception")).setThrown(parent).build();
final StringBuilder sb = new StringBuilder();
converter.format(event, sb);
final String result = sb.toString();
assertEquals("The class names should be same", packageName + "ThrowablePatternConverterTest", result);
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class ScriptFilter method filter.
@Override
public Result filter(final Logger logger, final Level level, final Marker marker, final String msg, final Object... params) {
final SimpleBindings bindings = new SimpleBindings();
bindings.put("logger", logger);
bindings.put("level", level);
bindings.put("marker", marker);
bindings.put("message", new SimpleMessage(msg));
bindings.put("parameters", params);
bindings.put("throwable", null);
bindings.putAll(configuration.getProperties());
bindings.put("substitutor", configuration.getStrSubstitutor());
final Object object = configuration.getScriptManager().execute(script.getName(), bindings);
return object == null || !Boolean.TRUE.equals(object) ? onMismatch : onMatch;
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class Log4jLogEventTest method testBuilderCorrectlyCopiesAllEventAttributesInclContextData.
@Test
public void testBuilderCorrectlyCopiesAllEventAttributesInclContextData() {
final StringMap contextData = new SortedArrayStringMap();
contextData.putValue("A", "B");
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().setContextData(//
contextData).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();
assertSame(contextData, event.getContextData());
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 = new Log4jLogEvent.Builder(event).build();
assertEquals("copy constructor builder", event2, event);
assertEquals("same hashCode", event2.hashCode(), event.hashCode());
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class MutableLogEventTest method testJavaIoSerializable.
@Test
public void testJavaIoSerializable() 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.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.getContextMap(), evt2.getContextMap());
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());
assertNull(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());
}
Aggregations