Repository: logging-log4j-plugins Updated Branches: refs/heads/avro-layout 9ed7256c1 -> 727411b62
Move classes to correct place Project: http://git-wip-us.apache.org/repos/asf/logging-log4j-plugins/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j-plugins/commit/727411b6 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j-plugins/tree/727411b6 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j-plugins/diff/727411b6 Branch: refs/heads/avro-layout Commit: 727411b6218367f537b5950ebc12813d3b4fb85e Parents: 9ed7256 Author: Mikael Ståldal <mik...@staldal.nu> Authored: Tue Jan 16 22:22:02 2018 +0100 Committer: Mikael Ståldal <mik...@staldal.nu> Committed: Tue Jan 16 22:22:02 2018 +0100 ---------------------------------------------------------------------- .../apache/logging/log4j/avro/AvroLayout.java | 198 +++++++++++++++++++ .../logging/log4j/avro/AvroLogEventParser.java | 48 +++++ .../apache/logging/log4j/avro/package-info.java | 1 - .../apache/logging/log4j/avro/AvroAppender.java | 5 - .../apache/logging/log4j/avro/AvroLayout.java | 198 ------------------- .../logging/log4j/avro/AvroLogEventParser.java | 48 ----- 6 files changed, 246 insertions(+), 252 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j-plugins/blob/727411b6/log4j-avro/src/main/java/org/apache/logging/log4j/avro/AvroLayout.java ---------------------------------------------------------------------- diff --git a/log4j-avro/src/main/java/org/apache/logging/log4j/avro/AvroLayout.java b/log4j-avro/src/main/java/org/apache/logging/log4j/avro/AvroLayout.java new file mode 100644 index 0000000..ec511e7 --- /dev/null +++ b/log4j-avro/src/main/java/org/apache/logging/log4j/avro/AvroLayout.java @@ -0,0 +1,198 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.avro; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.logging.log4j.ThreadContext; +import org.apache.logging.log4j.core.Layout; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.Node; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute; +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory; +import org.apache.logging.log4j.core.layout.AbstractLayout; +import org.apache.logging.log4j.core.layout.ByteBufferDestination; +import org.apache.logging.log4j.util.ReadOnlyStringMap; +import org.apache.logging.log4j.util.TriConsumer; + +/** + * Lays out events in Apache Avro binary format. + * + * @see <a href="https://avro.apache.org/docs/current/spec.html">Avro specification</a> + */ +@Plugin(name = "AvroLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true) +public final class AvroLayout extends AbstractLayout<org.apache.logging.log4j.avro.LogEvent> { + + private final boolean locationInfo; + private final boolean includeStacktrace; + + public static class Builder<B extends Builder<B>> extends AbstractLayout.Builder<B> + implements org.apache.logging.log4j.core.util.Builder<AvroLayout> { + + @PluginBuilderAttribute + private boolean locationInfo; + + @PluginBuilderAttribute + private boolean includeStacktrace = true; + + @Override + public AvroLayout build() { + return new AvroLayout(getConfiguration(), includeStacktrace, locationInfo); + } + + public boolean isLocationInfo() { + return locationInfo; + } + + public boolean isIncludeStacktrace() { + return includeStacktrace; + } + + /** + * Whether to include stacktrace of logged Throwables (optional, default to true). + * + * If set to false, the Throwable will be omitted. + * + * @return this builder + */ + public B setLocationInfo(final boolean locationInfo) { + this.locationInfo = locationInfo; + return asBuilder(); + } + + /** + * Whether to include the location information (optional, defaults to false). + * + * Generating location information is an expensive operation and may impact performance. + * Use with caution. + * + * @return this builder + */ + public B setIncludeStacktrace(final boolean includeStacktrace) { + this.includeStacktrace = includeStacktrace; + return asBuilder(); + } + } + + private AvroLayout(final Configuration config, + final boolean locationInfo, + final boolean includeStacktrace) { + super(config, null, null); + this.locationInfo = locationInfo; + this.includeStacktrace = includeStacktrace; + } + + @PluginBuilderFactory + public static <B extends Builder<B>> B newBuilder() { + return new Builder<B>().asBuilder(); + } + + @Override + public Map<String, String> getContentFormat() { + return Collections.emptyMap(); + } + + @Override + public String getContentType() { + return "application/octet-stream"; + } + + @Override + public byte[] toByteArray(final LogEvent event) { + ByteBuffer byteBuffer = toByteBuffer(event); + if (byteBuffer.hasArray()) { + return byteBuffer.array(); + } else { + byteBuffer.flip(); + byte[] array = new byte[byteBuffer.remaining()]; + byteBuffer.get(array); + return array; + } + } + + @Override + public void encode(final LogEvent event, final ByteBufferDestination destination) { + destination.writeBytes(toByteBuffer(event)); + } + + private ByteBuffer toByteBuffer(final LogEvent event) { + org.apache.logging.log4j.avro.LogEvent datum = toSerializable(event); + + ByteBuffer byteBuffer; + try { + byteBuffer = datum.toByteBuffer(); + } catch (IOException e) { + throw new RuntimeException(e); + } + return byteBuffer; + } + + @Override + public org.apache.logging.log4j.avro.LogEvent toSerializable(final LogEvent event) { + org.apache.logging.log4j.avro.LogEvent datum = + new org.apache.logging.log4j.avro.LogEvent(); + + ReadOnlyStringMap contextData = event.getContextData(); + if (!contextData.isEmpty()) { + Map<CharSequence, CharSequence> contextMap = new HashMap<>(contextData.size()); + contextData.forEach(PUT_ALL, contextMap); + datum.setContextMap(contextMap); + } + + ThreadContext.ContextStack contextStack = event.getContextStack(); + if (!contextStack.isEmpty()) { + List<CharSequence> list = new ArrayList<>(contextStack.getDepth()); + list.addAll(contextStack); + datum.setContextStack(list); + } + + datum.setLoggerFqcn(event.getLoggerFqcn()); + datum.setLevel(event.getLevel().name()); + datum.setLoggerName(event.getLoggerName()); + // TODO marker + datum.setMessage(event.getMessage().getFormattedMessage()); + datum.setTimeMillis(event.getTimeMillis()); + if (locationInfo) { + // TODO source + } + datum.setThread(event.getThreadName()); + datum.setThreadId(event.getThreadId()); + datum.setThreadPriority(event.getThreadPriority()); + if (includeStacktrace) { + // TODO thrown + } + datum.setEndOfBatch(event.isEndOfBatch()); + datum.setNanoTime(event.getNanoTime()); + return datum; + } + + private static TriConsumer<String, String, Map<CharSequence, CharSequence>> PUT_ALL = + new TriConsumer<String, String, Map<CharSequence, CharSequence>>() { + @Override + public void accept(final String key, final String value, final Map<CharSequence, CharSequence> stringStringMap) { + stringStringMap.put(key, value); + } + }; + +} http://git-wip-us.apache.org/repos/asf/logging-log4j-plugins/blob/727411b6/log4j-avro/src/main/java/org/apache/logging/log4j/avro/AvroLogEventParser.java ---------------------------------------------------------------------- diff --git a/log4j-avro/src/main/java/org/apache/logging/log4j/avro/AvroLogEventParser.java b/log4j-avro/src/main/java/org/apache/logging/log4j/avro/AvroLogEventParser.java new file mode 100644 index 0000000..b724118 --- /dev/null +++ b/log4j-avro/src/main/java/org/apache/logging/log4j/avro/AvroLogEventParser.java @@ -0,0 +1,48 @@ +package org.apache.logging.log4j.avro; + +import java.io.IOException; +import java.nio.ByteBuffer; +import org.apache.avro.AvroRuntimeException; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.impl.Log4jLogEvent; +import org.apache.logging.log4j.core.parser.LogEventParser; +import org.apache.logging.log4j.core.parser.ParseException; + +/** + * Parses the output from {@link AvroLayout} into instances of {@link LogEvent}. + */ +public class AvroLogEventParser implements LogEventParser { + + @Override + public LogEvent parseFrom(final byte[] input) throws ParseException { + org.apache.logging.log4j.avro.LogEvent datum; + try { + datum = org.apache.logging.log4j.avro.LogEvent.getDecoder().decode(input); + } catch (IOException | AvroRuntimeException e) { + throw new ParseException(e); + } + + return parseFrom(datum); + } + + @Override + public LogEvent parseFrom(final byte[] input, final int offset, final int length) + throws ParseException { + org.apache.logging.log4j.avro.LogEvent datum; + try { + ByteBuffer byteBuffer = ByteBuffer.wrap(input, offset, length); + datum = org.apache.logging.log4j.avro.LogEvent.getDecoder().decode(byteBuffer); + } catch (IOException | AvroRuntimeException e) { + throw new ParseException(e); + } + + return parseFrom(datum); + } + + private LogEvent parseFrom(final org.apache.logging.log4j.avro.LogEvent datum) { + Log4jLogEvent.Builder builder = Log4jLogEvent.newBuilder(); + // TODO + return builder.build(); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j-plugins/blob/727411b6/log4j-avro/src/main/java/org/apache/logging/log4j/avro/package-info.java ---------------------------------------------------------------------- diff --git a/log4j-avro/src/main/java/org/apache/logging/log4j/avro/package-info.java b/log4j-avro/src/main/java/org/apache/logging/log4j/avro/package-info.java index 3f3bed1..06bf5e1 100644 --- a/log4j-avro/src/main/java/org/apache/logging/log4j/avro/package-info.java +++ b/log4j-avro/src/main/java/org/apache/logging/log4j/avro/package-info.java @@ -18,7 +18,6 @@ * Log4j plugins for Apache Avro. * * @see <a href="https://logging.apache.org/log4j/2.x/manual/layouts.html#AvroLayout">Avro Layout manual</a> - * @see <a href="https://logging.apache.org/log4j/2.x/manual/appenders.html#AvroAppender">Avro Appender manual</a> * @since 2.11.0 */ package org.apache.logging.log4j.avro; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j-plugins/blob/727411b6/log4j-avro/src/test/java/org/apache/logging/log4j/avro/AvroAppender.java ---------------------------------------------------------------------- diff --git a/log4j-avro/src/test/java/org/apache/logging/log4j/avro/AvroAppender.java b/log4j-avro/src/test/java/org/apache/logging/log4j/avro/AvroAppender.java deleted file mode 100644 index 87a1e4d..0000000 --- a/log4j-avro/src/test/java/org/apache/logging/log4j/avro/AvroAppender.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.apache.logging.log4j.avro; - -public class AvroAppender { - // TODO -} http://git-wip-us.apache.org/repos/asf/logging-log4j-plugins/blob/727411b6/log4j-avro/src/test/java/org/apache/logging/log4j/avro/AvroLayout.java ---------------------------------------------------------------------- diff --git a/log4j-avro/src/test/java/org/apache/logging/log4j/avro/AvroLayout.java b/log4j-avro/src/test/java/org/apache/logging/log4j/avro/AvroLayout.java deleted file mode 100644 index ec511e7..0000000 --- a/log4j-avro/src/test/java/org/apache/logging/log4j/avro/AvroLayout.java +++ /dev/null @@ -1,198 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache license, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the license for the specific language governing permissions and - * limitations under the license. - */ -package org.apache.logging.log4j.avro; - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.apache.logging.log4j.ThreadContext; -import org.apache.logging.log4j.core.Layout; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.config.Configuration; -import org.apache.logging.log4j.core.config.Node; -import org.apache.logging.log4j.core.config.plugins.Plugin; -import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute; -import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory; -import org.apache.logging.log4j.core.layout.AbstractLayout; -import org.apache.logging.log4j.core.layout.ByteBufferDestination; -import org.apache.logging.log4j.util.ReadOnlyStringMap; -import org.apache.logging.log4j.util.TriConsumer; - -/** - * Lays out events in Apache Avro binary format. - * - * @see <a href="https://avro.apache.org/docs/current/spec.html">Avro specification</a> - */ -@Plugin(name = "AvroLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true) -public final class AvroLayout extends AbstractLayout<org.apache.logging.log4j.avro.LogEvent> { - - private final boolean locationInfo; - private final boolean includeStacktrace; - - public static class Builder<B extends Builder<B>> extends AbstractLayout.Builder<B> - implements org.apache.logging.log4j.core.util.Builder<AvroLayout> { - - @PluginBuilderAttribute - private boolean locationInfo; - - @PluginBuilderAttribute - private boolean includeStacktrace = true; - - @Override - public AvroLayout build() { - return new AvroLayout(getConfiguration(), includeStacktrace, locationInfo); - } - - public boolean isLocationInfo() { - return locationInfo; - } - - public boolean isIncludeStacktrace() { - return includeStacktrace; - } - - /** - * Whether to include stacktrace of logged Throwables (optional, default to true). - * - * If set to false, the Throwable will be omitted. - * - * @return this builder - */ - public B setLocationInfo(final boolean locationInfo) { - this.locationInfo = locationInfo; - return asBuilder(); - } - - /** - * Whether to include the location information (optional, defaults to false). - * - * Generating location information is an expensive operation and may impact performance. - * Use with caution. - * - * @return this builder - */ - public B setIncludeStacktrace(final boolean includeStacktrace) { - this.includeStacktrace = includeStacktrace; - return asBuilder(); - } - } - - private AvroLayout(final Configuration config, - final boolean locationInfo, - final boolean includeStacktrace) { - super(config, null, null); - this.locationInfo = locationInfo; - this.includeStacktrace = includeStacktrace; - } - - @PluginBuilderFactory - public static <B extends Builder<B>> B newBuilder() { - return new Builder<B>().asBuilder(); - } - - @Override - public Map<String, String> getContentFormat() { - return Collections.emptyMap(); - } - - @Override - public String getContentType() { - return "application/octet-stream"; - } - - @Override - public byte[] toByteArray(final LogEvent event) { - ByteBuffer byteBuffer = toByteBuffer(event); - if (byteBuffer.hasArray()) { - return byteBuffer.array(); - } else { - byteBuffer.flip(); - byte[] array = new byte[byteBuffer.remaining()]; - byteBuffer.get(array); - return array; - } - } - - @Override - public void encode(final LogEvent event, final ByteBufferDestination destination) { - destination.writeBytes(toByteBuffer(event)); - } - - private ByteBuffer toByteBuffer(final LogEvent event) { - org.apache.logging.log4j.avro.LogEvent datum = toSerializable(event); - - ByteBuffer byteBuffer; - try { - byteBuffer = datum.toByteBuffer(); - } catch (IOException e) { - throw new RuntimeException(e); - } - return byteBuffer; - } - - @Override - public org.apache.logging.log4j.avro.LogEvent toSerializable(final LogEvent event) { - org.apache.logging.log4j.avro.LogEvent datum = - new org.apache.logging.log4j.avro.LogEvent(); - - ReadOnlyStringMap contextData = event.getContextData(); - if (!contextData.isEmpty()) { - Map<CharSequence, CharSequence> contextMap = new HashMap<>(contextData.size()); - contextData.forEach(PUT_ALL, contextMap); - datum.setContextMap(contextMap); - } - - ThreadContext.ContextStack contextStack = event.getContextStack(); - if (!contextStack.isEmpty()) { - List<CharSequence> list = new ArrayList<>(contextStack.getDepth()); - list.addAll(contextStack); - datum.setContextStack(list); - } - - datum.setLoggerFqcn(event.getLoggerFqcn()); - datum.setLevel(event.getLevel().name()); - datum.setLoggerName(event.getLoggerName()); - // TODO marker - datum.setMessage(event.getMessage().getFormattedMessage()); - datum.setTimeMillis(event.getTimeMillis()); - if (locationInfo) { - // TODO source - } - datum.setThread(event.getThreadName()); - datum.setThreadId(event.getThreadId()); - datum.setThreadPriority(event.getThreadPriority()); - if (includeStacktrace) { - // TODO thrown - } - datum.setEndOfBatch(event.isEndOfBatch()); - datum.setNanoTime(event.getNanoTime()); - return datum; - } - - private static TriConsumer<String, String, Map<CharSequence, CharSequence>> PUT_ALL = - new TriConsumer<String, String, Map<CharSequence, CharSequence>>() { - @Override - public void accept(final String key, final String value, final Map<CharSequence, CharSequence> stringStringMap) { - stringStringMap.put(key, value); - } - }; - -} http://git-wip-us.apache.org/repos/asf/logging-log4j-plugins/blob/727411b6/log4j-avro/src/test/java/org/apache/logging/log4j/avro/AvroLogEventParser.java ---------------------------------------------------------------------- diff --git a/log4j-avro/src/test/java/org/apache/logging/log4j/avro/AvroLogEventParser.java b/log4j-avro/src/test/java/org/apache/logging/log4j/avro/AvroLogEventParser.java deleted file mode 100644 index b724118..0000000 --- a/log4j-avro/src/test/java/org/apache/logging/log4j/avro/AvroLogEventParser.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.apache.logging.log4j.avro; - -import java.io.IOException; -import java.nio.ByteBuffer; -import org.apache.avro.AvroRuntimeException; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.impl.Log4jLogEvent; -import org.apache.logging.log4j.core.parser.LogEventParser; -import org.apache.logging.log4j.core.parser.ParseException; - -/** - * Parses the output from {@link AvroLayout} into instances of {@link LogEvent}. - */ -public class AvroLogEventParser implements LogEventParser { - - @Override - public LogEvent parseFrom(final byte[] input) throws ParseException { - org.apache.logging.log4j.avro.LogEvent datum; - try { - datum = org.apache.logging.log4j.avro.LogEvent.getDecoder().decode(input); - } catch (IOException | AvroRuntimeException e) { - throw new ParseException(e); - } - - return parseFrom(datum); - } - - @Override - public LogEvent parseFrom(final byte[] input, final int offset, final int length) - throws ParseException { - org.apache.logging.log4j.avro.LogEvent datum; - try { - ByteBuffer byteBuffer = ByteBuffer.wrap(input, offset, length); - datum = org.apache.logging.log4j.avro.LogEvent.getDecoder().decode(byteBuffer); - } catch (IOException | AvroRuntimeException e) { - throw new ParseException(e); - } - - return parseFrom(datum); - } - - private LogEvent parseFrom(final org.apache.logging.log4j.avro.LogEvent datum) { - Log4jLogEvent.Builder builder = Log4jLogEvent.newBuilder(); - // TODO - return builder.build(); - } - -}