Class AbstractLayout<T extends Serializable>
java.lang.Object
org.apache.logging.log4j.core.layout.AbstractLayout<T>
- Type Parameters:
T- The Class that the Layout will format the LogEvent into.
- Direct Known Subclasses:
AbstractStringLayout,MessageLayout,SerializedLayout
Abstract base class for Layouts.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAbstractLayout.Builder<B extends AbstractLayout.Builder<B>>Subclasses can extend this abstract Builder. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ConfigurationThe current Configuration.protected longThe number of events successfully processed by this layout.protected final byte[]The footer to add when the stream is closed.protected final byte[]The header to include when the stream is opened.protected static final LoggerAllow subclasses access to the status logger without creating another instance.Fields inherited from interface org.apache.logging.log4j.core.Layout
ELEMENT_TYPE -
Constructor Summary
ConstructorsConstructorDescriptionAbstractLayout(byte[] header, byte[] footer) Deprecated.AbstractLayout(Configuration configuration, byte[] header, byte[] footer) Constructs a layout with an optional header and footer. -
Method Summary
Modifier and TypeMethodDescriptionvoidencode(LogEvent event, ByteBufferDestination destination) Encodes the specified source LogEvent to some binary representation and writes the result to the specified destination.Returns a description of the content format.byte[]Returns the footer, if one is available.byte[]Returns the header, if one is available.protected voidMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.logging.log4j.core.Layout
getContentType, toByteArray, toSerializable
-
Field Details
-
LOGGER
Allow subclasses access to the status logger without creating another instance. -
configuration
The current Configuration. -
eventCount
protected long eventCountThe number of events successfully processed by this layout. -
header
protected final byte[] headerThe header to include when the stream is opened. May be null.
-
-
Constructor Details
-
AbstractLayout
Deprecated.Constructs a layout with an optional header and footer.- Parameters:
header- The header to include when the stream is opened. May be null.footer- The footer to add when the stream is closed. May be null.
-
AbstractLayout
Constructs a layout with an optional header and footer.- Parameters:
configuration- The configuration. May be null.header- The header to include when the stream is opened. May be null.footer- The footer to add when the stream is closed. May be null.
-
-
Method Details
-
getConfiguration
-
getContentFormat
Description copied from interface:LayoutReturns a description of the content format.- Specified by:
getContentFormatin interfaceLayout<T extends Serializable>- Returns:
- a Map of key/value pairs describing the Layout-specific content format, or an empty Map if no content format descriptors are specified.
-
getHeader
public byte[] getHeader()Returns the header, if one is available.- Specified by:
getHeaderin interfaceLayout<T extends Serializable>- Returns:
- A byte array containing the header.
-
markEvent
protected void markEvent() -
encode
Encodes the specified source LogEvent to some binary representation and writes the result to the specified destination.The default implementation of this method delegates to the
Layout.toByteArray(LogEvent)method which allocates temporary objects.Subclasses can override this method to provide a garbage-free implementation. For text-based layouts,
AbstractStringLayoutprovides various convenience methods to help with this:@Plugin(name = "MyLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true) public final class MyLayout extends AbstractStringLayout { @Override public void encode(LogEvent event, ByteBufferDestination destination) { StringBuilder text = getStringBuilder(); convertLogEventToText(event, text); getStringBuilderEncoder().encode(text, destination); } private void convertLogEventToText(LogEvent event, StringBuilder destination) { ... // append a text representation of the log event to the StringBuilder } }- Specified by:
encodein interfaceEncoder<T extends Serializable>- Parameters:
event- the LogEvent to encode.destination- holds the ByteBuffer to write into.- See Also:
-
AbstractLayout(Configuration, byte[], byte[])