public class Log4Json extends org.apache.log4j.Layout
{"name":"test","time":1318429136789,"date":"2011-10-12 15:18:56,789","level":"INFO","thread":"main","message":"test message"}An event with an error will contain data similar to that below (which has been reformatted to be multi-line).
{ "name":"testException", "time":1318429136789, "date":"2011-10-12 15:18:56,789", "level":"INFO", "thread":"quoted\"", "message":"new line\n and {}", "exceptionclass":"java.net.NoRouteToHostException", "stack":[ "java.net.NoRouteToHostException: that box caught fire 3 years ago", "\tat org.apache.hadoop.log.TestLog4Json.testException(TestLog4Json.java:49)", "\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)", "\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)", "\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)", "\tat java.lang.reflect.Method.invoke(Method.java:597)", "\tat junit.framework.TestCase.runTest(TestCase.java:168)", "\tat junit.framework.TestCase.runBare(TestCase.java:134)", "\tat junit.framework.TestResult$1.protect(TestResult.java:110)", "\tat junit.framework.TestResult.runProtected(TestResult.java:128)", "\tat junit.framework.TestResult.run(TestResult.java:113)", "\tat junit.framework.TestCase.run(TestCase.java:124)", "\tat junit.framework.TestSuite.runTest(TestSuite.java:232)", "\tat junit.framework.TestSuite.run(TestSuite.java:227)", "\tat org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)", "\tat org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59)", "\tat org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:120)", "\tat org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:145)", "\tat org.apache.maven.surefire.Surefire.run(Surefire.java:104)", "\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)", "\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)", "\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)", "\tat java.lang.reflect.Method.invoke(Method.java:597)", "\tat org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:290)", "\tat org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1017)" ] }
Modifier and Type | Field and Description |
---|---|
static String |
DATE |
static String |
EXCEPTION_CLASS |
static String |
JSON_TYPE |
static String |
LEVEL |
static String |
MESSAGE |
static String |
NAME |
static String |
STACK |
static String |
THREAD |
static String |
TIME |
Constructor and Description |
---|
Log4Json() |
Modifier and Type | Method and Description |
---|---|
void |
activateOptions()
Do nothing
|
String |
format(org.apache.log4j.spi.LoggingEvent event) |
String |
getContentType() |
boolean |
ignoresThrowable()
This appender does not ignore throwables
|
static org.codehaus.jackson.node.ContainerNode |
parse(String json)
For use in tests
|
String |
toJson(org.apache.log4j.spi.LoggingEvent event)
Convert an event to JSON
|
Writer |
toJson(Writer writer,
org.apache.log4j.spi.LoggingEvent event)
Convert an event to JSON
|
Writer |
toJson(Writer writer,
String loggerName,
long timeStamp,
String level,
String threadName,
String message,
org.apache.log4j.spi.ThrowableInformation ti)
Build a JSON entry from the parameters.
|
public static final String DATE
public static final String EXCEPTION_CLASS
public static final String LEVEL
public static final String MESSAGE
public static final String NAME
public static final String STACK
public static final String THREAD
public static final String TIME
public static final String JSON_TYPE
public Log4Json()
public String getContentType()
getContentType
in class org.apache.log4j.Layout
public String format(org.apache.log4j.spi.LoggingEvent event)
format
in class org.apache.log4j.Layout
public String toJson(org.apache.log4j.spi.LoggingEvent event) throws IOException
event
- the event -must not be nullIOException
- on problems generating the JSONpublic Writer toJson(Writer writer, org.apache.log4j.spi.LoggingEvent event) throws IOException
writer
- the destination writerevent
- the event -must not be nullIOException
- on problems generating the JSONpublic Writer toJson(Writer writer, String loggerName, long timeStamp, String level, String threadName, String message, org.apache.log4j.spi.ThrowableInformation ti) throws IOException
writer
- destinationloggerName
- logger nametimeStamp
- time_t valuelevel
- level stringthreadName
- name of the threadmessage
- rendered messageti
- nullable thrown informationIOException
- on any problempublic boolean ignoresThrowable()
ignoresThrowable
in class org.apache.log4j.Layout
public void activateOptions()
public static org.codehaus.jackson.node.ContainerNode parse(String json) throws IOException
json
- incoming JSON to parseIOException
- on any parsing problemsCopyright © 2017 Apache Software Foundation. All Rights Reserved.