Search in sources :

Example 1 with ParameterVisitable

use of org.apache.logging.log4j.message.ParameterVisitable in project logging-log4j2 by apache.

the class MessageParameterResolver method resolve.

@Override
public void resolve(final LogEvent logEvent, final JsonWriter jsonWriter) {
    // If possible, perform a garbage-free resolution.
    final Message message = logEvent.getMessage();
    if (message instanceof ParameterVisitable) {
        final ParameterVisitable parameterVisitable = (ParameterVisitable) message;
        resolve(parameterVisitable, jsonWriter);
        return;
    }
    // Short-circuit if there are no parameters.
    final Object[] parameters = message.getParameters();
    if (parameters == null || parameters.length == 0 || index >= parameters.length) {
        if (index < 0) {
            jsonWriter.writeArrayStart();
            jsonWriter.writeArrayEnd();
        } else {
            jsonWriter.writeNull();
        }
        return;
    }
    // Resolve all parameters.
    if (index < 0) {
        jsonWriter.writeArrayStart();
        for (int i = 0; i < parameters.length; i++) {
            if (i > 0) {
                jsonWriter.writeSeparator();
            }
            final Object parameter = parameters[i];
            if (stringified) {
                final String stringifiedParameter = String.valueOf(parameter);
                jsonWriter.writeString(stringifiedParameter);
            } else {
                jsonWriter.writeValue(parameter);
            }
        }
        jsonWriter.writeArrayEnd();
    } else // Resolve a single parameter.
    {
        final Object parameter = parameters[index];
        if (stringified) {
            final String stringifiedParameter = String.valueOf(parameter);
            jsonWriter.writeString(stringifiedParameter);
        } else {
            jsonWriter.writeValue(parameter);
        }
    }
}
Also used : ParameterVisitable(org.apache.logging.log4j.message.ParameterVisitable) Message(org.apache.logging.log4j.message.Message)

Aggregations

Message (org.apache.logging.log4j.message.Message)1 ParameterVisitable (org.apache.logging.log4j.message.ParameterVisitable)1