Java Code Examples for org.apache.logging.log4j.core.Layout#toByteArray()
The following examples show how to use
org.apache.logging.log4j.core.Layout#toByteArray() .
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 |
@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: PulsarAppender.java From pulsar with Apache License 2.0 | 5 votes |
private void tryAppend(final LogEvent event) { 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); }
Example 3
Source File: JeroMqAppender.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@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); } }
Example 4
Source File: KafkaAppender.java From logging-log4j2 with Apache License 2.0 | 5 votes |
private void tryAppend(final LogEvent event) throws ExecutionException, InterruptedException, TimeoutException { final Layout<? extends Serializable> layout = getLayout(); byte[] data; Long eventTimestamp; data = layout.toByteArray(event); eventTimestamp = event.getTimeMillis(); manager.send(data, eventTimestamp); }
Example 5
Source File: GelfAppender.java From log4j2-gelf with Apache License 2.0 | 4 votes |
@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); } }
Example 6
Source File: HttpURLConnectionManager.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@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; } } }
Example 7
Source File: PatternSelectorTest.java From logging-log4j2 with Apache License 2.0 | 4 votes |
public String formatEvent(final LogEvent event, final Layout<?> layout) { return new String(layout.toByteArray(event)); }
Example 8
Source File: PatternLayoutTest.java From logging-log4j2 with Apache License 2.0 | 4 votes |
public String formatEvent(final LogEvent event, final Layout<?> layout) { return new String(layout.toByteArray(event)); }