org.apache.logging.log4j.core.layout.ByteBufferDestination Java Examples

The following examples show how to use org.apache.logging.log4j.core.layout.ByteBufferDestination. 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: JsonTemplateLayoutConcurrentEncodeTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private void produce(final AtomicReference<Exception> encodeFailureRef) {
    final int threadCount = 10;
    final JsonTemplateLayout layout = createLayout();
    final ByteBufferDestination destination =
            new ConcurrentAccessDetectingByteBufferDestination();
    final AtomicLong encodeCounter = new AtomicLong(0);
    final List<Thread> workers = IntStream
            .range(0, threadCount)
            .mapToObj((final int threadIndex) ->
                    createWorker(
                            layout,
                            destination,
                            encodeFailureRef,
                            encodeCounter,
                            threadIndex))
            .collect(Collectors.toList());
    workers.forEach(Thread::start);
    workers.forEach((final Thread worker) -> {
        try {
            worker.join();
        } catch (final InterruptedException ignored) {
            System.err.format("join to %s interrupted%n", worker.getName());
        }
    });
}
 
Example #2
Source File: AbstractStringLayoutStringEncodingBenchmark.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) {
    final StringBuilder sb = getStringBuilder();
    ((StringBuilderFormattable) event.getMessage()).formatTo(sb);
    final Encoder<StringBuilder> helper = getStringBuilderEncoder();
    helper.encode(sb, destination);
}
 
Example #3
Source File: NoGcLayout.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) {
    final StringBuilder text = toText(event, getCachedStringBuilder());

    final Encoder<StringBuilder> helper = getCachedHelper();
    helper.encode(text, destination);
}
 
Example #4
Source File: JsonTemplateLayoutBenchmark.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private static int benchmark(
        final Layout<String> layout,
        final List<LogEvent> logEvents,
        final ByteBufferDestination destination) {
    // noinspection ForLoopReplaceableByForEach (avoid iterator instantiation)
    for (int logEventIndex = 0; logEventIndex < logEvents.size(); logEventIndex++) {
        LogEvent logEvent = logEvents.get(logEventIndex);
        layout.encode(logEvent, destination);
    }
    final ByteBuffer byteBuffer = destination.getByteBuffer();
    final int position = byteBuffer.position();
    byteBuffer.clear();
    return position;
}
 
Example #5
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 #6
Source File: JsonTemplateLayoutConcurrentEncodeTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private Thread createWorker(
        final JsonTemplateLayout layout,
        final ByteBufferDestination destination,
        final AtomicReference<Exception> encodeFailureRef,
        final AtomicLong encodeCounter,
        final int threadIndex) {
    final int maxEncodeCount = 1_000;
    final String threadName = String.format("Worker-%d", threadIndex);
    return new Thread(
            () -> {
                try {
                    for (int logEventIndex = threadIndex % LOG_EVENTS.length;
                         encodeFailureRef.get() == null && encodeCounter.incrementAndGet() < maxEncodeCount;
                         logEventIndex = (logEventIndex + 1) % LOG_EVENTS.length) {
                        final LogEvent logEvent = LOG_EVENTS[logEventIndex];
                        layout.encode(logEvent, destination);
                    }
                } catch (final Exception error) {
                    final boolean succeeded = encodeFailureRef.compareAndSet(null, error);
                    if (succeeded) {
                        System.err.format("%s failed%n", threadName);
                        error.printStackTrace(System.err);
                    }
                }
            },
            threadName);
}
 
Example #7
Source File: EcsLayout.java    From ecs-logging-java with Apache License 2.0 4 votes vote down vote up
@Override
public void encode(LogEvent event, ByteBufferDestination destination) {
    final StringBuilder text = toText(event, getStringBuilder(), true);
    final Encoder<StringBuilder> helper = getStringBuilderEncoder();
    helper.encode(text, destination);
}
 
Example #8
Source File: JsonTemplateLayoutBenchmarkState.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
ByteBufferDestination getByteBufferDestination() {
    return byteBufferDestination;
}
 
Example #9
Source File: LayoutAdapter.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public void encode(LogEvent event, ByteBufferDestination destination) {
    final byte[] data = toByteArray(event);
    destination.writeBytes(data, 0, data.length);
}
 
Example #10
Source File: Log4j1XmlLayout.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public void encode(final LogEvent event, final ByteBufferDestination destination) {
    final StringBuilder text = getStringBuilder();
    formatTo(event, text);
    getStringBuilderEncoder().encode(text, destination);
}
 
Example #11
Source File: LayoutAdapter.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public void encode(LogEvent event, ByteBufferDestination destination) {
    final byte[] data = toByteArray(event);
    destination.writeBytes(data, 0, data.length);
}
 
Example #12
Source File: Log4j1XmlLayout.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public void encode(final LogEvent event, final ByteBufferDestination destination) {
    final StringBuilder text = getStringBuilder();
    formatTo(event, text);
    getStringBuilderEncoder().encode(text, destination);
}
 
Example #13
Source File: ItemAppenderFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 2 votes vote down vote up
@Override
public void encode(Object source, ByteBufferDestination destination) {

}