use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class RewriteAppenderTest method rewriteTest.
@Test
public void rewriteTest() {
final StructuredDataMessage msg = new StructuredDataMessage("Test", "This is a test", "Service");
msg.put("Key1", "Value1");
msg.put("Key2", "Value2");
EventLogger.logEvent(msg);
final List<LogEvent> list = app.getEvents();
assertNotNull("No events generated", list);
assertTrue("Incorrect number of events. Expected 1, got " + list.size(), list.size() == 1);
final LogEvent event = list.get(0);
final Message m = event.getMessage();
assertTrue("Message is not a MapMessage", m instanceof MapMessage);
final MapMessage message = (MapMessage) m;
final Map<String, String> map = message.getData();
assertNotNull("No Map", map);
assertTrue("Incorrect number of map entries, expected 3 got " + map.size(), map.size() == 3);
final String value = map.get("Key1");
assertEquals("Apache", value);
}
use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class MessageAttributeConverterTest method testConvert01.
@Test
public void testConvert01() {
final Message message = LOGGER.getMessageFactory().newMessage("Message #{} said [{}].", 3, "Hello");
final String converted = this.converter.convertToDatabaseColumn(message);
assertNotNull("The converted value should not be null.", converted);
assertEquals("The converted value is not correct.", "Message #3 said [Hello].", converted);
final Message reversed = this.converter.convertToEntityAttribute(converted);
assertNotNull("The reversed value should not be null.", reversed);
assertEquals("The reversed value is not correct.", "Message #3 said [Hello].", reversed.getFormattedMessage());
}
use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class MapRewritePolicy method rewrite.
/**
* Rewrite the event.
* @param source a logging event that may be returned or
* used to create a new logging event.
* @return The LogEvent after rewriting.
*/
@Override
public LogEvent rewrite(final LogEvent source) {
final Message msg = source.getMessage();
if (msg == null || !(msg instanceof MapMessage)) {
return source;
}
final Map<String, String> newMap = new HashMap<>(((MapMessage) msg).getData());
switch(mode) {
case Add:
{
newMap.putAll(map);
break;
}
default:
{
for (final Map.Entry<String, String> entry : map.entrySet()) {
if (newMap.containsKey(entry.getKey())) {
newMap.put(entry.getKey(), entry.getValue());
}
}
}
}
final MapMessage message = ((MapMessage) msg).newInstance(newMap);
final LogEvent result = new Log4jLogEvent.Builder(source).setMessage(message).build();
return result;
}
use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class GelfLayout method toText.
private StringBuilder toText(final LogEvent event, final StringBuilder builder, final boolean gcFree) {
builder.append('{');
builder.append("\"version\":\"1.1\",");
builder.append("\"host\":\"");
JsonUtils.quoteAsString(toNullSafeString(host), builder);
builder.append(QC);
builder.append("\"timestamp\":").append(formatTimestamp(event.getTimeMillis())).append(C);
builder.append("\"level\":").append(formatLevel(event.getLevel())).append(C);
if (event.getThreadName() != null) {
builder.append("\"_thread\":\"");
JsonUtils.quoteAsString(event.getThreadName(), builder);
builder.append(QC);
}
if (event.getLoggerName() != null) {
builder.append("\"_logger\":\"");
JsonUtils.quoteAsString(event.getLoggerName(), builder);
builder.append(QC);
}
if (additionalFields.length > 0) {
final StrSubstitutor strSubstitutor = getConfiguration().getStrSubstitutor();
for (final KeyValuePair additionalField : additionalFields) {
builder.append(QU);
JsonUtils.quoteAsString(additionalField.getKey(), builder);
builder.append("\":\"");
final String value = valueNeedsLookup(additionalField.getValue()) ? strSubstitutor.replace(event, additionalField.getValue()) : additionalField.getValue();
JsonUtils.quoteAsString(toNullSafeString(value), builder);
builder.append(QC);
}
}
if (includeThreadContext) {
event.getContextData().forEach(WRITE_KEY_VALUES_INTO, builder);
}
if (event.getThrown() != null) {
builder.append("\"full_message\":\"");
if (includeStacktrace) {
JsonUtils.quoteAsString(formatThrowable(event.getThrown()), builder);
} else {
JsonUtils.quoteAsString(event.getThrown().toString(), builder);
}
builder.append(QC);
}
builder.append("\"short_message\":\"");
final Message message = event.getMessage();
if (message instanceof CharSequence) {
JsonUtils.quoteAsString(((CharSequence) message), builder);
} else if (gcFree && message instanceof StringBuilderFormattable) {
final StringBuilder messageBuffer = getMessageStringBuilder();
try {
((StringBuilderFormattable) message).formatTo(messageBuffer);
JsonUtils.quoteAsString(messageBuffer, builder);
} finally {
trimToMaxSize(messageBuffer);
}
} else {
JsonUtils.quoteAsString(toNullSafeString(message.getFormattedMessage()), builder);
}
builder.append(Q);
builder.append('}');
if (includeNullDelimiter) {
builder.append('\0');
}
return builder;
}
use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.
the class Rfc5424Layout method appendStructuredElements.
private void appendStructuredElements(final StringBuilder buffer, final LogEvent event) {
final Message message = event.getMessage();
final boolean isStructured = message instanceof StructuredDataMessage;
if (!isStructured && (fieldFormatters != null && fieldFormatters.isEmpty()) && !includeMdc) {
buffer.append('-');
return;
}
final Map<String, StructuredDataElement> sdElements = new HashMap<>();
final Map<String, String> contextMap = event.getContextData().toMap();
if (mdcRequired != null) {
checkRequired(contextMap);
}
if (fieldFormatters != null) {
for (final Map.Entry<String, FieldFormatter> sdElement : fieldFormatters.entrySet()) {
final String sdId = sdElement.getKey();
final StructuredDataElement elem = sdElement.getValue().format(event);
sdElements.put(sdId, elem);
}
}
if (includeMdc && contextMap.size() > 0) {
final String mdcSdIdStr = mdcSdId.toString();
final StructuredDataElement union = sdElements.get(mdcSdIdStr);
if (union != null) {
union.union(contextMap);
sdElements.put(mdcSdIdStr, union);
} else {
final StructuredDataElement formattedContextMap = new StructuredDataElement(contextMap, false);
sdElements.put(mdcSdIdStr, formattedContextMap);
}
}
if (isStructured) {
final StructuredDataMessage data = (StructuredDataMessage) message;
final Map<String, String> map = data.getData();
final StructuredDataId id = data.getId();
final String sdId = getId(id);
if (sdElements.containsKey(sdId)) {
final StructuredDataElement union = sdElements.get(id.toString());
union.union(map);
sdElements.put(sdId, union);
} else {
final StructuredDataElement formattedData = new StructuredDataElement(map, false);
sdElements.put(sdId, formattedData);
}
}
if (sdElements.isEmpty()) {
buffer.append('-');
return;
}
for (final Map.Entry<String, StructuredDataElement> entry : sdElements.entrySet()) {
formatStructuredElement(entry.getKey(), mdcPrefix, entry.getValue(), buffer, listChecker);
}
}
Aggregations