下面列出了怎么用org.apache.logging.log4j.core.layout.ByteBufferDestination的API类实例代码及写法,或者点击链接到github查看源代码。
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());
}
});
}
@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);
}
@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);
}
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;
}
@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);
}
}
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);
}
@Override
public void encode(LogEvent event, ByteBufferDestination destination) {
final StringBuilder text = toText(event, getStringBuilder(), true);
final Encoder<StringBuilder> helper = getStringBuilderEncoder();
helper.encode(text, destination);
}
ByteBufferDestination getByteBufferDestination() {
return byteBufferDestination;
}
@Override
public void encode(LogEvent event, ByteBufferDestination destination) {
final byte[] data = toByteArray(event);
destination.writeBytes(data, 0, data.length);
}
@Override
public void encode(final LogEvent event, final ByteBufferDestination destination) {
final StringBuilder text = getStringBuilder();
formatTo(event, text);
getStringBuilderEncoder().encode(text, destination);
}
@Override
public void encode(LogEvent event, ByteBufferDestination destination) {
final byte[] data = toByteArray(event);
destination.writeBytes(data, 0, data.length);
}
@Override
public void encode(final LogEvent event, final ByteBufferDestination destination) {
final StringBuilder text = getStringBuilder();
formatTo(event, text);
getStringBuilderEncoder().encode(text, destination);
}
@Override
public void encode(Object source, ByteBufferDestination destination) {
}