use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method test_maxStringLength.
@Test
void test_maxStringLength() {
// Create the log event.
final int maxStringLength = 30;
final String excessiveMessageString = Strings.repeat("m", maxStringLength) + 'M';
final SimpleMessage message = new SimpleMessage(excessiveMessageString);
final Throwable thrown = new RuntimeException();
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setLevel(Level.INFO).setMessage(message).setThrown(thrown).build();
// Create the event template node with map values.
final String messageKey = "message";
final String excessiveKey = Strings.repeat("k", maxStringLength) + 'K';
final String excessiveValue = Strings.repeat("v", maxStringLength) + 'V';
final String nullValueKey = "nullValueKey";
final String eventTemplate = writeJson(asMap(messageKey, asMap("$resolver", "message"), excessiveKey, excessiveValue, nullValueKey, asMap("$resolver", "exception", "field", "message")));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).setMaxStringLength(maxStringLength).build();
// Check serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
final String truncatedStringSuffix = JsonTemplateLayoutDefaults.getTruncatedStringSuffix();
final String truncatedMessageString = excessiveMessageString.substring(0, maxStringLength) + truncatedStringSuffix;
assertThat(accessor.getString(messageKey)).isEqualTo(truncatedMessageString);
final String truncatedKey = excessiveKey.substring(0, maxStringLength) + truncatedStringSuffix;
final String truncatedValue = excessiveValue.substring(0, maxStringLength) + truncatedStringSuffix;
assertThat(accessor.getString(truncatedKey)).isEqualTo(truncatedValue);
assertThat(accessor.getString(nullValueKey)).isNull();
});
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method test_exception_resolvers_against_no_exceptions.
@Test
void test_exception_resolvers_against_no_exceptions() {
// Create the log event.
final SimpleMessage message = new SimpleMessage("Hello, World!");
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setMessage(message).build();
// Create the event template.
final String eventTemplate = writeJson(asMap("exStackTrace", asMap("$resolver", "exception", "field", "stackTrace"), "exStackTraceString", asMap("$resolver", "exception", "field", "stackTrace", "stackTrace", asMap("stringified", true)), "exRootCauseStackTrace", asMap("$resolver", "exceptionRootCause", "field", "stackTrace"), "exRootCauseStackTraceString", asMap("$resolver", "exceptionRootCause", "field", "stackTrace", "stackTrace", asMap("stringified", true)), "requiredFieldTriggeringError", true));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).setStackTraceEnabled(true).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
assertThat(accessor.getObject("exStackTrace")).isNull();
assertThat(accessor.getObject("exStackTraceString")).isNull();
assertThat(accessor.getObject("exRootCauseStackTrace")).isNull();
assertThat(accessor.getObject("exRootCauseStackTraceString")).isNull();
assertThat(accessor.getBoolean("requiredFieldTriggeringError")).isTrue();
});
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class LogEventFixture method createFullLogEvent.
private static LogEvent createFullLogEvent(final String id, final long timeMillis) {
// Create exception.
final Exception sourceHelper = new Exception();
sourceHelper.fillInStackTrace();
final Exception cause = new NullPointerException("testNPEx-" + id);
sourceHelper.fillInStackTrace();
final StackTraceElement source = sourceHelper.getStackTrace()[0];
final IOException ioException = new IOException("testIOEx-" + id, cause);
ioException.addSuppressed(new IndexOutOfBoundsException("I am suppressed exception 1" + id));
ioException.addSuppressed(new IndexOutOfBoundsException("I am suppressed exception 2" + id));
// Create rest of the event attributes.
final SimpleMessage message = new SimpleMessage("full LogEvent message " + id);
final StringMap contextData = createContextData(id);
final ThreadContextStack contextStack = createContextStack(id);
final int threadId = id.hashCode();
final String threadName = "MyThreadName" + id;
final int threadPriority = threadId % 10;
final Level level = Level.DEBUG;
final String loggerFqcn = "f.q.c.n" + id;
final String loggerName = "a.B" + id;
final long nanoTime = timeMillis * 2;
// Create the event.
return Log4jLogEvent.newBuilder().setLoggerName(loggerName).setLoggerFqcn(loggerFqcn).setLevel(level).setMessage(message).setThrown(ioException).setContextData(contextData).setContextStack(contextStack).setThreadId(threadId).setThreadName(threadName).setThreadPriority(threadPriority).setSource(source).setTimeMillis(timeMillis).setNanoTime(nanoTime).build();
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method test_custom_resolver.
@Test
void test_custom_resolver() {
// Create the event template.
final String eventTemplate = writeJson(asMap("customField", asMap("$resolver", "custom")));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
// Create the log event.
final SimpleMessage message = new SimpleMessage("foo");
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setMessage(message).build();
// Check the serialized log event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> Assertions.assertThat(accessor.getString("customField")).matches("CustomValue-[0-9]+"));
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method test_property_injection.
@Test
void test_property_injection() {
// Create the log event.
final SimpleMessage message = new SimpleMessage("Hello, World");
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setLevel(Level.INFO).setMessage(message).build();
// Create the event template with property.
final String propertyName = "propertyName";
final String eventTemplate = writeJson(asMap(propertyName, "${" + propertyName + "}"));
// Create the layout with property.
final String propertyValue = "propertyValue";
final Configuration config = ConfigurationBuilderFactory.newConfigurationBuilder().addProperty(propertyName, propertyValue).build();
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(config).setEventTemplate(eventTemplate).build();
// Check the serialized event.
usingSerializedLogEventAccessor(layout, logEvent, accessor -> assertThat(accessor.getString(propertyName)).isEqualTo(propertyValue));
}
Aggregations