本文整理汇总了Java中org.apache.logging.log4j.core.Layout.toByteArray方法的典型用法代码示例。如果您正苦于以下问题:Java Layout.toByteArray方法的具体用法?Java Layout.toByteArray怎么用?Java Layout.toByteArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.logging.log4j.core.Layout
的用法示例。
在下文中一共展示了Layout.toByteArray方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: append
import org.apache.logging.log4j.core.Layout; //导入方法依赖的package包/类
@Override
public synchronized void append(final LogEvent event) {
final Layout<? extends Serializable> layout = getLayout();
if (layout == null) {
if (event instanceof MutableLogEvent) {
// must take snapshot or subsequent calls to logger.log() will modify this event
events.add(((MutableLogEvent) event).createMemento());
} else {
events.add(event);
}
} else if (layout instanceof SerializedLayout) {
final byte[] header = layout.getHeader();
final byte[] content = layout.toByteArray(event);
final byte[] record = new byte[header.length + content.length];
System.arraycopy(header, 0, record, 0, header.length);
System.arraycopy(content, 0, record, header.length, content.length);
data.add(record);
} else {
write(layout.toByteArray(event));
}
if (countDownLatch != null) {
countDownLatch.countDown();
}
}
示例2: append
import org.apache.logging.log4j.core.Layout; //导入方法依赖的package包/类
@Override
public synchronized void append(final LogEvent event) {
final Layout<? extends Serializable> layout = getLayout();
if (layout == null) {
events.add(event);
} else if (layout instanceof SerializedLayout) {
final byte[] header = layout.getHeader();
final byte[] content = layout.toByteArray(event);
final byte[] record = new byte[header.length + content.length];
System.arraycopy(header, 0, record, 0, header.length);
System.arraycopy(content, 0, record, header.length, content.length);
data.add(record);
} else {
write(layout.toByteArray(event));
}
}
示例3: append
import org.apache.logging.log4j.core.Layout; //导入方法依赖的package包/类
@Override
public synchronized void append(final LogEvent event) {
final Layout<? extends Serializable> layout = getLayout();
final byte[] formattedMessage = layout.toByteArray(event);
if (manager.send(getLayout().toByteArray(event))) {
sendRcTrue++;
} else {
sendRcFalse++;
LOGGER.error("Appender {} could not send message {} to JeroMQ {}", getName(), sendRcFalse, formattedMessage);
}
}
示例4: tryAppend
import org.apache.logging.log4j.core.Layout; //导入方法依赖的package包/类
private void tryAppend(final LogEvent event) throws ExecutionException, InterruptedException, TimeoutException {
final Layout<? extends Serializable> layout = getLayout();
byte[] data;
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);
}
manager.send(data);
}
示例5: append
import org.apache.logging.log4j.core.Layout; //导入方法依赖的package包/类
@Override
public void append(LogEvent event) {
final Layout<? extends Serializable> layout = getLayout();
final String formattedMessage;
if (layout == null) {
formattedMessage = event.getMessage().getFormattedMessage();
} else {
formattedMessage = new String(layout.toByteArray(event), StandardCharsets.UTF_8);
}
final GelfMessageBuilder builder = new GelfMessageBuilder(formattedMessage, hostName)
.timestamp(event.getTimeMillis() / 1000d)
.level(GelfMessageLevel.fromNumericLevel(Severity.getSeverity(event.getLevel()).getCode()))
.additionalField("loggerName", event.getLoggerName())
.additionalField("threadName", event.getThreadName());
final Marker marker = event.getMarker();
if (marker != null) {
builder.additionalField("marker", marker.getName());
}
if (includeThreadContext) {
for (Map.Entry<String, String> entry : event.getContextMap().entrySet()) {
builder.additionalField(entry.getKey(), entry.getValue());
}
// Guard against https://issues.apache.org/jira/browse/LOG4J2-1530
final ThreadContext.ContextStack contextStack = event.getContextStack();
if (contextStack != null) {
final List<String> contextStackItems = contextStack.asList();
if (contextStackItems != null && !contextStackItems.isEmpty()) {
builder.additionalField("contextStack", contextStackItems.toString());
}
}
}
if (includeSource) {
final StackTraceElement source = event.getSource();
if (source != null) {
builder.additionalField("sourceFileName", source.getFileName());
builder.additionalField("sourceMethodName", source.getMethodName());
builder.additionalField("sourceClassName", source.getClassName());
builder.additionalField("sourceLineNumber", source.getLineNumber());
}
}
@SuppressWarnings("all")
final Throwable thrown = event.getThrown();
if (includeStackTrace && thrown != null) {
String stackTrace;
if (includeExceptionCause) {
final StringWriter stringWriter = new StringWriter();
final PrintWriter printWriter = new PrintWriter(stringWriter);
thrown.printStackTrace(printWriter);
stackTrace = stringWriter.toString();
} else {
stackTrace = getSimpleStacktraceAsString(thrown);
}
builder.additionalField("exceptionClass", thrown.getClass().getCanonicalName());
builder.additionalField("exceptionMessage", thrown.getMessage());
builder.additionalField("exceptionStackTrace", stackTrace);
builder.fullMessage(formattedMessage);
}
if (!additionalFields.isEmpty()) {
builder.additionalFields(additionalFields);
}
final GelfMessage gelfMessage = builder.build();
try {
final boolean sent = client.trySend(gelfMessage);
if (!sent) {
LOG.debug("Couldn't send message: {}", gelfMessage);
}
} catch (Exception e) {
throw new AppenderLoggingException("failed to write log event to GELF server: " + e.getMessage(), e);
}
}
示例6: send
import org.apache.logging.log4j.core.Layout; //导入方法依赖的package包/类
@Override
public void send(final Layout<?> layout, final LogEvent event) throws IOException {
final HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setAllowUserInteraction(false);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod(method);
if (connectTimeoutMillis > 0) {
urlConnection.setConnectTimeout(connectTimeoutMillis);
}
if (readTimeoutMillis > 0) {
urlConnection.setReadTimeout(readTimeoutMillis);
}
if (layout.getContentType() != null) {
urlConnection.setRequestProperty("Content-Type", layout.getContentType());
}
for (final Property header : headers) {
urlConnection.setRequestProperty(
header.getName(),
header.isValueNeedsLookup() ? getConfiguration().getStrSubstitutor().replace(event, header.getValue()) : header.getValue());
}
if (sslConfiguration != null) {
((HttpsURLConnection)urlConnection).setSSLSocketFactory(sslConfiguration.getSslSocketFactory());
}
if (isHttps && !verifyHostname) {
((HttpsURLConnection)urlConnection).setHostnameVerifier(LaxHostnameVerifier.INSTANCE);
}
final byte[] msg = layout.toByteArray(event);
urlConnection.setFixedLengthStreamingMode(msg.length);
urlConnection.connect();
try (OutputStream os = urlConnection.getOutputStream()) {
os.write(msg);
}
final byte[] buffer = new byte[1024];
try (InputStream is = urlConnection.getInputStream()) {
while (IOUtils.EOF != is.read(buffer)) {
// empty
}
} catch (final IOException e) {
final StringBuilder errorMessage = new StringBuilder();
try (InputStream es = urlConnection.getErrorStream()) {
errorMessage.append(urlConnection.getResponseCode());
if (urlConnection.getResponseMessage() != null) {
errorMessage.append(' ').append(urlConnection.getResponseMessage());
}
if (es != null) {
errorMessage.append(" - ");
int n;
while (IOUtils.EOF != (n = es.read(buffer))) {
errorMessage.append(new String(buffer, 0, n, CHARSET));
}
}
}
if (urlConnection.getResponseCode() > -1) {
throw new IOException(errorMessage.toString());
} else {
throw e;
}
}
}
示例7: formatEvent
import org.apache.logging.log4j.core.Layout; //导入方法依赖的package包/类
public String formatEvent(final LogEvent event, final Layout<?> layout) {
return new String(layout.toByteArray(event));
}