org.apache.logging.log4j.core.layout
Class JSONLayout

java.lang.Object
  extended by org.apache.logging.log4j.core.layout.AbstractLayout<String>
      extended by org.apache.logging.log4j.core.layout.AbstractStringLayout
          extended by org.apache.logging.log4j.core.layout.JSONLayout
All Implemented Interfaces:
Layout<String>

public class JSONLayout
extends AbstractStringLayout

Appends a series of JSON events as strings serialized as bytes.

Complete well-formed JSON vs. fragment JSON

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.

Encoding

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.

Pretty vs. compact XML

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.


Field Summary
 
Fields inherited from class org.apache.logging.log4j.core.layout.AbstractLayout
footer, header, LOGGER
 
Constructor Summary
protected JSONLayout(boolean locationInfo, boolean properties, boolean complete, boolean compact, Charset charset)
           
 
Method Summary
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.
 
Methods inherited from class org.apache.logging.log4j.core.layout.AbstractStringLayout
getCharset, toByteArray
 
Methods inherited from class org.apache.logging.log4j.core.layout.AbstractLayout
setFooter, setHeader
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JSONLayout

protected JSONLayout(boolean locationInfo,
                     boolean properties,
                     boolean complete,
                     boolean compact,
                     Charset charset)
Method Detail

toSerializable

public String toSerializable(LogEvent event)
Formats a LogEvent in conformance with the log4j.dtd.

Parameters:
event - The LogEvent.
Returns:
The XML representation of the LogEvent.

getHeader

public byte[] getHeader()
Returns appropriate JSON headers.

Specified by:
getHeader in interface Layout<String>
Overrides:
getHeader in class AbstractLayout<String>
Returns:
a byte array containing the header, opening the JSON array.

getFooter

public byte[] getFooter()
Returns appropriate JSON footer.

Specified by:
getFooter in interface Layout<String>
Overrides:
getFooter in class AbstractLayout<String>
Returns:
a byte array containing the footer, closing the JSON array.

getContentFormat

public Map<String,String> getContentFormat()
XMLLayout's content format is specified by:

Key: "dtd" Value: "log4j-events.dtd"

Key: "version" Value: "2.0"

Returns:
Map of content format keys supporting XMLLayout

getContentType

public String getContentType()
Description copied from interface: Layout
Returns the content type output by this layout. The base class returns "text/plain".

Specified by:
getContentType in interface Layout<String>
Overrides:
getContentType in class AbstractStringLayout
Returns:
The default content type for Strings.

createLayout

public static JSONLayout createLayout(String locationInfo,
                                      String properties,
                                      String completeStr,
                                      String compactStr,
                                      String charsetName)
Creates an XML Layout.

Parameters:
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".
Returns:
An XML Layout.


Copyright © 1999-2013 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.