org.apache.logging.log4j.core.util.StringEncoder Java Examples

The following examples show how to use org.apache.logging.log4j.core.util.StringEncoder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AdvancedKafkaAppender.java    From summerframework with Apache License 2.0 6 votes vote down vote up
@Override
public void append(final LogEvent event) {
    try {
        final Layout<? extends Serializable> layout = getLayout();
        byte[] data;
        if (layout != null) {
            if (layout instanceof SerializedLayout) {
                final byte[] header = layout.getHeader();
                final byte[] body = layout.toByteArray(event);
                data = new byte[header.length + body.length];
                System.arraycopy(header, 0, data, 0, header.length);
                System.arraycopy(body, 0, data, header.length, body.length);
            } else {
                data = layout.toByteArray(event);
            }
        } else {
            data = StringEncoder.toBytes(event.getMessage().getFormattedMessage(), StandardCharsets.UTF_8);
        }
        manager.send(topic, data);
    } catch (final Exception e) {
        LOGGER.error("Unable to write to Kafka [{}] for appender [{}].", manager.getName(), getName(), e);
        throw new AppenderLoggingException("Unable to write to Kafka in appender: " + e.getMessage(), e);
    }
}
 
Example #2
Source File: JsonTemplateLayout.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(final LogEvent event, final ByteBufferDestination destination) {

    // Acquire a context.
    final Context context = acquireContext();
    final JsonWriter jsonWriter = context.jsonWriter;
    final StringBuilder stringBuilder = jsonWriter.getStringBuilder();
    final Encoder<StringBuilder> encoder = context.encoder;

    try {

        // Render the JSON.
        eventResolver.resolve(event, jsonWriter);
        stringBuilder.append(eventDelimiter);

        // Write to the destination.
        if (encoder == null) {
            final String eventJson = stringBuilder.toString();
            final byte[] eventJsonBytes = StringEncoder.toBytes(eventJson, charset);
            destination.writeBytes(eventJsonBytes, 0, eventJsonBytes.length);
        } else {
            encoder.encode(stringBuilder, destination);
        }

    }

    // Release the context.
    finally {
        contextRecycler.release(context);
    }

}
 
Example #3
Source File: AbstractStringLayout.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
protected byte[] getBytes(final String s) {
    if (useCustomEncoding) { // rely on branch prediction to eliminate this check if false
        return StringEncoder.encodeSingleByteChars(s);
    }
    try { // LOG4J2-935: String.getBytes(String) gives better performance
        return s.getBytes(charsetName);
    } catch (final UnsupportedEncodingException e) {
        return s.getBytes(charset);
    }
}
 
Example #4
Source File: AbstractStringLayout.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
protected byte[] serializeToBytes(final Serializer serializer, final byte[] defaultValue) {
    final String serializable = serializeToString(serializer);
    if (serializable == null) {
        return defaultValue;
    }
    return StringEncoder.toBytes(serializable, getCharset());
}
 
Example #5
Source File: JsonTemplateLayout.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] toByteArray(final LogEvent event) {
    final String eventJson = toSerializable(event);
    return StringEncoder.toBytes(eventJson, charset);
}