All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.hcp.cf.log4j2.converter.JsonMessageConverter Maven / Gradle / Ivy

There is a newer version: 3.8.4
Show newest version
package com.sap.hcp.cf.log4j2.converter;

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.pattern.ConverterKeys;
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;

import com.sap.hcp.cf.logging.common.converter.DefaultMessageConverter;

/**
 * A simple {@link LogEventPatternConverter} that converts a message into a JSON
 * message.
 * 

* The main point are that we may need to do escaping and/or flattening * depending on the context. Escaping means that we write the message as * a quoted string and thus need to escape properly within the message * string. If a message is flattened, objects or arrays are turned into a * list of fields or values. * */ @Plugin(name = "JsonMessageConverter", category = "Converter") @ConverterKeys({ "jsonmsg" }) public class JsonMessageConverter extends LogEventPatternConverter { public static final String WORD = "jsonmsg"; public static final String OPT_ESCAPE = "escape"; public static final String OPT_FLATTEN = "flatten"; private final DefaultMessageConverter converter = new DefaultMessageConverter(); public JsonMessageConverter(String[] options) { super(WORD, WORD); if (options != null) { for (String option: options) { if (OPT_FLATTEN.equalsIgnoreCase(option)) { converter.setFlatten(true); } else if (OPT_ESCAPE.equalsIgnoreCase(option)) { converter.setEscape(true); } } } } public static JsonMessageConverter newInstance(final String[] options) { return new JsonMessageConverter(options); } @Override public void format(LogEvent event, StringBuilder toAppendTo) { converter.convert(event.getMessage().getFormattedMessage(), toAppendTo); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy