public class JSONLayout extends AbstractStringLayout
If you configure complete="true"
, the appender outputs a well-formed JSON document.
By default, with complete="false"
, you should include the
output as an external file in a separate file to form a well-formed JSON document.
A well-formed JSON document follows this pattern:
[ { "logger":"com.foo.Bar", "timestamp":"1376681196470", "level":"INFO", "thread":"main", "message":"Message flushed with immediate flush=true" }, { "logger":"com.foo.Bar", "timestamp":"1376681196471", "level":"ERROR", "thread":"main", "message":"Message flushed with immediate flush=true", "throwable":"java.lang.IllegalArgumentException: badarg\\n\\tat org.apache.logging.log4j.core.appender.JSONCompleteFileAppenderTest.testFlushAtEndOfBatch(JSONCompleteFileAppenderTest.java:54)\\n\\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\\n\\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)\\n\\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\\n\\tat java.lang.reflect.Method.invoke(Method.java:606)\\n\\tat org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)\\n\\tat org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)\\n\\tat org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)\\n\\tat org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)\\n\\tat org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)\\n\\tat org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)\\n\\tat org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)\\n\\tat org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)\\n\\tat org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)\\n\\tat org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)\\n\\tat org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)\\n\\tat org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)\\n\\tat org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)\\n\\tat org.junit.runners.ParentRunner.run(ParentRunner.java:309)\\n\\tat org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)\\n\\tat org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)\\n\\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)\\n\\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)\\n\\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)\\n\\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)\\n" } ]
If complete="false"
, the appender does not write the JSON open array character "[" at the start of the document.
and "]" and the end.
This approach enforces the independence of the JSONLayout 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.
By default, the JSON layout is not compact (a.k.a. not "pretty") with compact="false"
, which means the
appender uses end-of-line characters and indents lines to format the text. If compact="true"
, then no
end-of-line or indentation is used. Message content may contain, of course, escaped end-of-lines.
footer, header, LOGGER
Modifier | Constructor and Description |
---|---|
protected |
JSONLayout(boolean locationInfo,
boolean properties,
boolean complete,
boolean compact,
Charset charset) |
Modifier and Type | Method and Description |
---|---|
static JSONLayout |
createLayout(String locationInfo,
String properties,
String completeStr,
String compactStr,
String charsetName)
Creates an XML Layout.
|
Map<String,String> |
getContentFormat()
XMLLayout's content format is specified by:
Key: "dtd" Value: "log4j-events.dtd"
Key: "version" Value: "2.0"
|
String |
getContentType()
Returns the content type output by this layout.
|
byte[] |
getFooter()
Returns appropriate JSON footer.
|
byte[] |
getHeader()
Returns appropriate JSON headers.
|
String |
toSerializable(LogEvent event)
Formats a
LogEvent in conformance with the log4j.dtd. |
getCharset, toByteArray
setFooter, setHeader
protected JSONLayout(boolean locationInfo, boolean properties, boolean complete, boolean compact, Charset charset)
public String toSerializable(LogEvent event)
LogEvent
in conformance with the log4j.dtd.event
- The LogEvent.public byte[] getHeader()
public byte[] getFooter()
public Map<String,String> getContentFormat()
public String getContentType()
Layout
getContentType
in interface Layout<String>
getContentType
in class AbstractStringLayout
public static JSONLayout createLayout(String locationInfo, String properties, String completeStr, String compactStr, String charsetName)
locationInfo
- If "true", includes the location information in the generated JSON.properties
- If "true", includes the thread context in the generated JSON.completeStr
- If "true", includes the JSON header and footer, defaults to "false".compactStr
- If "true", does not use end-of-lines and indentation, defaults to "false".charsetName
- The character set to use, if null
, uses "UTF-8".Copyright © 1999-2014 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.