@Plugin(name="YamlLayout", category="Core", elementType="layout", printObject=true) public final class YamlLayout extends AbstractStringLayout
A YAML log event follows this pattern:
--- timeMillis: 1 thread: "MyThreadName" level: "DEBUG" loggerName: "a.B" marker: name: "Marker1" parents: - name: "ParentMarker1" parents: - name: "GrandMotherMarker" - name: "GrandFatherMarker" - name: "ParentMarker2" message: "Msg" thrown: commonElementCount: 0 localizedMessage: "testIOEx" message: "testIOEx" name: "java.io.IOException" cause: commonElementCount: 27 localizedMessage: "testNPEx" message: "testNPEx" name: "java.lang.NullPointerException" extendedStackTrace: - class: "org.apache.logging.log4j.core.layout.LogEventFixtures" method: "createLogEvent" file: "LogEventFixtures.java" line: 52 exact: false location: "test-classes/" version: "?" extendedStackTrace: - class: "org.apache.logging.log4j.core.layout.LogEventFixtures" method: "createLogEvent" file: "LogEventFixtures.java" line: 55 exact: true location: "test-classes/" version: "?" - class: "org.apache.logging.log4j.core.layout.YamlLayoutTest" method: "testAllFeatures" file: "YamlLayoutTest.java" line: 109 exact: true location: "test-classes/" version: "?" - class: "org.apache.logging.log4j.core.layout.YamlLayoutTest" method: "testLocationOnCompactOffEventEolOffMdcOn" file: "YamlLayoutTest.java" line: 280 exact: true location: "test-classes/" version: "?" - class: "sun.reflect.NativeMethodAccessorImpl" method: "invoke0" file: "NativeMethodAccessorImpl.java" line: -2 exact: false location: "?" version: "1.7.0_79" - class: "sun.reflect.NativeMethodAccessorImpl" method: "invoke" file: "NativeMethodAccessorImpl.java" line: 57 exact: false location: "?" version: "1.7.0_79" - class: "sun.reflect.DelegatingMethodAccessorImpl" method: "invoke" file: "DelegatingMethodAccessorImpl.java" line: 43 exact: false location: "?" version: "1.7.0_79" - class: "java.lang.reflect.Method" method: "invoke" file: "Method.java" line: 606 exact: false location: "?" version: "1.7.0_79" - class: "org.junit.runners.model.FrameworkMethod$1" method: "runReflectiveCall" file: "FrameworkMethod.java" line: 50 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.internal.runners.model.ReflectiveCallable" method: "run" file: "ReflectiveCallable.java" line: 12 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.model.FrameworkMethod" method: "invokeExplosively" file: "FrameworkMethod.java" line: 47 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.internal.runners.statements.InvokeMethod" method: "evaluate" file: "InvokeMethod.java" line: 17 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner" method: "runLeaf" file: "ParentRunner.java" line: 325 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.BlockJUnit4ClassRunner" method: "runChild" file: "BlockJUnit4ClassRunner.java" line: 78 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.BlockJUnit4ClassRunner" method: "runChild" file: "BlockJUnit4ClassRunner.java" line: 57 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner$3" method: "run" file: "ParentRunner.java" line: 290 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner$1" method: "schedule" file: "ParentRunner.java" line: 71 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner" method: "runChildren" file: "ParentRunner.java" line: 288 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner" method: "access$000" file: "ParentRunner.java" line: 58 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner$2" method: "evaluate" file: "ParentRunner.java" line: 268 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.internal.runners.statements.RunBefores" method: "evaluate" file: "RunBefores.java" line: 26 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.internal.runners.statements.RunAfters" method: "evaluate" file: "RunAfters.java" line: 27 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner" method: "run" file: "ParentRunner.java" line: 363 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference" method: "run" file: "JUnit4TestReference.java" line: 86 exact: true location: ".cp/" version: "?" - class: "org.eclipse.jdt.internal.junit.runner.TestExecution" method: "run" file: "TestExecution.java" line: 38 exact: true location: ".cp/" version: "?" - class: "org.eclipse.jdt.internal.junit.runner.RemoteTestRunner" method: "runTests" file: "RemoteTestRunner.java" line: 459 exact: true location: ".cp/" version: "?" - class: "org.eclipse.jdt.internal.junit.runner.RemoteTestRunner" method: "runTests" file: "RemoteTestRunner.java" line: 675 exact: true location: ".cp/" version: "?" - class: "org.eclipse.jdt.internal.junit.runner.RemoteTestRunner" method: "run" file: "RemoteTestRunner.java" line: 382 exact: true location: ".cp/" version: "?" - class: "org.eclipse.jdt.internal.junit.runner.RemoteTestRunner" method: "main" file: "RemoteTestRunner.java" line: 192 exact: true location: ".cp/" version: "?" suppressed: - commonElementCount: 0 localizedMessage: "I am suppressed exception 1" message: "I am suppressed exception 1" name: "java.lang.IndexOutOfBoundsException" extendedStackTrace: - class: "org.apache.logging.log4j.core.layout.LogEventFixtures" method: "createLogEvent" file: "LogEventFixtures.java" line: 56 exact: true location: "test-classes/" version: "?" - class: "org.apache.logging.log4j.core.layout.YamlLayoutTest" method: "testAllFeatures" file: "YamlLayoutTest.java" line: 109 exact: true location: "test-classes/" version: "?" - class: "org.apache.logging.log4j.core.layout.YamlLayoutTest" method: "testLocationOnCompactOffEventEolOffMdcOn" file: "YamlLayoutTest.java" line: 280 exact: true location: "test-classes/" version: "?" - class: "sun.reflect.NativeMethodAccessorImpl" method: "invoke0" file: "NativeMethodAccessorImpl.java" line: -2 exact: false location: "?" version: "1.7.0_79" - class: "sun.reflect.NativeMethodAccessorImpl" method: "invoke" file: "NativeMethodAccessorImpl.java" line: 57 exact: false location: "?" version: "1.7.0_79" - class: "sun.reflect.DelegatingMethodAccessorImpl" method: "invoke" file: "DelegatingMethodAccessorImpl.java" line: 43 exact: false location: "?" version: "1.7.0_79" - class: "java.lang.reflect.Method" method: "invoke" file: "Method.java" line: 606 exact: false location: "?" version: "1.7.0_79" - class: "org.junit.runners.model.FrameworkMethod$1" method: "runReflectiveCall" file: "FrameworkMethod.java" line: 50 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.internal.runners.model.ReflectiveCallable" method: "run" file: "ReflectiveCallable.java" line: 12 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.model.FrameworkMethod" method: "invokeExplosively" file: "FrameworkMethod.java" line: 47 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.internal.runners.statements.InvokeMethod" method: "evaluate" file: "InvokeMethod.java" line: 17 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner" method: "runLeaf" file: "ParentRunner.java" line: 325 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.BlockJUnit4ClassRunner" method: "runChild" file: "BlockJUnit4ClassRunner.java" line: 78 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.BlockJUnit4ClassRunner" method: "runChild" file: "BlockJUnit4ClassRunner.java" line: 57 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner$3" method: "run" file: "ParentRunner.java" line: 290 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner$1" method: "schedule" file: "ParentRunner.java" line: 71 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner" method: "runChildren" file: "ParentRunner.java" line: 288 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner" method: "access$000" file: "ParentRunner.java" line: 58 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner$2" method: "evaluate" file: "ParentRunner.java" line: 268 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.internal.runners.statements.RunBefores" method: "evaluate" file: "RunBefores.java" line: 26 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.internal.runners.statements.RunAfters" method: "evaluate" file: "RunAfters.java" line: 27 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner" method: "run" file: "ParentRunner.java" line: 363 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference" method: "run" file: "JUnit4TestReference.java" line: 86 exact: true location: ".cp/" version: "?" - class: "org.eclipse.jdt.internal.junit.runner.TestExecution" method: "run" file: "TestExecution.java" line: 38 exact: true location: ".cp/" version: "?" - class: "org.eclipse.jdt.internal.junit.runner.RemoteTestRunner" method: "runTests" file: "RemoteTestRunner.java" line: 459 exact: true location: ".cp/" version: "?" - class: "org.eclipse.jdt.internal.junit.runner.RemoteTestRunner" method: "runTests" file: "RemoteTestRunner.java" line: 675 exact: true location: ".cp/" version: "?" - class: "org.eclipse.jdt.internal.junit.runner.RemoteTestRunner" method: "run" file: "RemoteTestRunner.java" line: 382 exact: true location: ".cp/" version: "?" - class: "org.eclipse.jdt.internal.junit.runner.RemoteTestRunner" method: "main" file: "RemoteTestRunner.java" line: 192 exact: true location: ".cp/" version: "?" - commonElementCount: 0 localizedMessage: "I am suppressed exception 2" message: "I am suppressed exception 2" name: "java.lang.IndexOutOfBoundsException" extendedStackTrace: - class: "org.apache.logging.log4j.core.layout.LogEventFixtures" method: "createLogEvent" file: "LogEventFixtures.java" line: 57 exact: true location: "test-classes/" version: "?" - class: "org.apache.logging.log4j.core.layout.YamlLayoutTest" method: "testAllFeatures" file: "YamlLayoutTest.java" line: 109 exact: true location: "test-classes/" version: "?" - class: "org.apache.logging.log4j.core.layout.YamlLayoutTest" method: "testLocationOnCompactOffEventEolOffMdcOn" file: "YamlLayoutTest.java" line: 280 exact: true location: "test-classes/" version: "?" - class: "sun.reflect.NativeMethodAccessorImpl" method: "invoke0" file: "NativeMethodAccessorImpl.java" line: -2 exact: false location: "?" version: "1.7.0_79" - class: "sun.reflect.NativeMethodAccessorImpl" method: "invoke" file: "NativeMethodAccessorImpl.java" line: 57 exact: false location: "?" version: "1.7.0_79" - class: "sun.reflect.DelegatingMethodAccessorImpl" method: "invoke" file: "DelegatingMethodAccessorImpl.java" line: 43 exact: false location: "?" version: "1.7.0_79" - class: "java.lang.reflect.Method" method: "invoke" file: "Method.java" line: 606 exact: false location: "?" version: "1.7.0_79" - class: "org.junit.runners.model.FrameworkMethod$1" method: "runReflectiveCall" file: "FrameworkMethod.java" line: 50 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.internal.runners.model.ReflectiveCallable" method: "run" file: "ReflectiveCallable.java" line: 12 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.model.FrameworkMethod" method: "invokeExplosively" file: "FrameworkMethod.java" line: 47 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.internal.runners.statements.InvokeMethod" method: "evaluate" file: "InvokeMethod.java" line: 17 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner" method: "runLeaf" file: "ParentRunner.java" line: 325 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.BlockJUnit4ClassRunner" method: "runChild" file: "BlockJUnit4ClassRunner.java" line: 78 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.BlockJUnit4ClassRunner" method: "runChild" file: "BlockJUnit4ClassRunner.java" line: 57 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner$3" method: "run" file: "ParentRunner.java" line: 290 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner$1" method: "schedule" file: "ParentRunner.java" line: 71 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner" method: "runChildren" file: "ParentRunner.java" line: 288 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner" method: "access$000" file: "ParentRunner.java" line: 58 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner$2" method: "evaluate" file: "ParentRunner.java" line: 268 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.internal.runners.statements.RunBefores" method: "evaluate" file: "RunBefores.java" line: 26 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.internal.runners.statements.RunAfters" method: "evaluate" file: "RunAfters.java" line: 27 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.junit.runners.ParentRunner" method: "run" file: "ParentRunner.java" line: 363 exact: true location: "junit-4.12.jar" version: "4.12" - class: "org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference" method: "run" file: "JUnit4TestReference.java" line: 86 exact: true location: ".cp/" version: "?" - class: "org.eclipse.jdt.internal.junit.runner.TestExecution" method: "run" file: "TestExecution.java" line: 38 exact: true location: ".cp/" version: "?" - class: "org.eclipse.jdt.internal.junit.runner.RemoteTestRunner" method: "runTests" file: "RemoteTestRunner.java" line: 459 exact: true location: ".cp/" version: "?" - class: "org.eclipse.jdt.internal.junit.runner.RemoteTestRunner" method: "runTests" file: "RemoteTestRunner.java" line: 675 exact: true location: ".cp/" version: "?" - class: "org.eclipse.jdt.internal.junit.runner.RemoteTestRunner" method: "run" file: "RemoteTestRunner.java" line: 382 exact: true location: ".cp/" version: "?" - class: "org.eclipse.jdt.internal.junit.runner.RemoteTestRunner" method: "main" file: "RemoteTestRunner.java" line: 192 exact: true location: ".cp/" version: "?" contextStack: - "stack_msg1" - "stack_msg2" endOfBatch: false loggerFqcn: "f.q.c.n" contextMap: MDC.B: "B_Value" MDC.A: "A_Value" threadId: 1 threadPriority: 5 source: class: "org.apache.logging.log4j.core.layout.LogEventFixtures" method: "createLogEvent" file: "LogEventFixtures.java" line: 53
This approach enforces the independence of the YamlLayout and the appender where you embed it.
Appenders using this layout should have their charset
set to UTF-8
or UTF-16
, otherwise
events containing non ASCII characters could result in corrupted log files.
Modifier and Type | Class and Description |
---|---|
static class |
AbstractJacksonLayout.Builder<B extends AbstractJacksonLayout.Builder<B>> |
AbstractStringLayout.Serializer, AbstractStringLayout.Serializer2
Modifier and Type | Field and Description |
---|---|
protected boolean |
compact |
protected static String |
COMPACT_EOL |
protected boolean |
complete |
protected static String |
DEFAULT_EOL |
protected String |
eol |
protected com.fasterxml.jackson.databind.ObjectWriter |
objectWriter |
DEFAULT_STRING_BUILDER_SIZE, MAX_STRING_BUILDER_SIZE
configuration, eventCount, footer, header, LOGGER
ELEMENT_TYPE
Modifier | Constructor and Description |
---|---|
protected |
YamlLayout(Configuration config,
boolean locationInfo,
boolean properties,
boolean complete,
boolean compact,
boolean eventEol,
String headerPattern,
String footerPattern,
Charset charset,
boolean includeStacktrace) |
Modifier and Type | Method and Description |
---|---|
static org.apache.logging.log4j.core.layout.AbstractJacksonLayout |
createDefaultLayout()
Creates a YAML Layout using the default settings.
|
static org.apache.logging.log4j.core.layout.AbstractJacksonLayout |
createLayout(Configuration config,
boolean locationInfo,
boolean properties,
String headerPattern,
String footerPattern,
Charset charset,
boolean includeStacktrace)
Creates a YAML Layout.
|
Map<String,String> |
getContentFormat()
Returns a description of the content format.
|
String |
getContentType()
Returns the content type output by this layout.
|
byte[] |
getFooter()
Returns appropriate YAML footer.
|
byte[] |
getHeader()
Returns appropriate YAML header.
|
String |
toSerializable(LogEvent event)
Formats a
LogEvent . |
void |
toSerializable(LogEvent event,
Writer writer) |
getBytes, getCharset, getFooterSerializer, getHeaderSerializer, getStringBuilder, getStringBuilderEncoder, serializeToBytes, serializeToString, toByteArray, trimToMaxSize
encode, getConfiguration, markEvent, writeTo
protected static final String DEFAULT_EOL
protected static final String COMPACT_EOL
protected final String eol
protected final com.fasterxml.jackson.databind.ObjectWriter objectWriter
protected final boolean compact
protected final boolean complete
protected YamlLayout(Configuration config, boolean locationInfo, boolean properties, boolean complete, boolean compact, boolean eventEol, String headerPattern, String footerPattern, Charset charset, boolean includeStacktrace)
public byte[] getHeader()
getHeader
in interface Layout<String>
getHeader
in class AbstractStringLayout
public byte[] getFooter()
getFooter
in interface Layout<String>
getFooter
in class AbstractStringLayout
public Map<String,String> getContentFormat()
Layout
getContentFormat
in interface Layout<String>
getContentFormat
in class AbstractLayout<String>
public String getContentType()
Layout
getContentType
in interface Layout<String>
getContentType
in class AbstractStringLayout
@PluginFactory public static org.apache.logging.log4j.core.layout.AbstractJacksonLayout createLayout(@PluginConfiguration Configuration config, @PluginAttribute(value="locationInfo") boolean locationInfo, @PluginAttribute(value="properties") boolean properties, @PluginAttribute(value="header",defaultString="") String headerPattern, @PluginAttribute(value="footer",defaultString="") String footerPattern, @PluginAttribute(value="charset",defaultString="UTF-8") Charset charset, @PluginAttribute(value="includeStacktrace",defaultBoolean=true) boolean includeStacktrace)
config
- The plugin configuration.locationInfo
- If "true", includes the location information in the generated YAML.properties
- If "true", includes the thread context map in the generated YAML.headerPattern
- The header pattern, defaults to ""
if null.footerPattern
- The header pattern, defaults to ""
if null.charset
- The character set to use, if null
, uses "UTF-8".includeStacktrace
- If "true", includes the stacktrace of any Throwable in the generated YAML, defaults to "true".public static org.apache.logging.log4j.core.layout.AbstractJacksonLayout createDefaultLayout()
public void toSerializable(LogEvent event, Writer writer) throws IOException
IOException
Copyright © 1999-2017 The Apache Software Foundation. All Rights Reserved.
Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, and the Apache Log4j logo are trademarks of The Apache Software Foundation.