org.apache.hadoop.log
Class Log4Json

java.lang.Object
  extended by org.apache.log4j.Layout
      extended by org.apache.hadoop.log.Log4Json
All Implemented Interfaces:
org.apache.log4j.spi.OptionHandler

public class Log4Json
extends org.apache.log4j.Layout

This offers a log layout for JSON, with some test entry points. It's purpose is to allow Log4J to generate events that are easy for other programs to parse, but which are somewhat human-readable. Some features.

  1. Every event is a standalone JSON clause
  2. Time is published as a time_t event since 1/1/1970 -this is the fastest to generate.
  3. An ISO date is generated, but this is cached and will only be accurate to within a second
  4. the stack trace is included as an array
A simple log event will resemble the following
     {"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)"
         ]
     }
 


Field Summary
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
           
 
Fields inherited from class org.apache.log4j.Layout
LINE_SEP, LINE_SEP_LEN
 
Constructor Summary
Log4Json()
           
 
Method Summary
 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.
 
Methods inherited from class org.apache.log4j.Layout
getFooter, getHeader
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DATE

public static final String DATE
See Also:
Constant Field Values

EXCEPTION_CLASS

public static final String EXCEPTION_CLASS
See Also:
Constant Field Values

LEVEL

public static final String LEVEL
See Also:
Constant Field Values

MESSAGE

public static final String MESSAGE
See Also:
Constant Field Values

NAME

public static final String NAME
See Also:
Constant Field Values

STACK

public static final String STACK
See Also:
Constant Field Values

THREAD

public static final String THREAD
See Also:
Constant Field Values

TIME

public static final String TIME
See Also:
Constant Field Values

JSON_TYPE

public static final String JSON_TYPE
See Also:
Constant Field Values
Constructor Detail

Log4Json

public Log4Json()
Method Detail

getContentType

public String getContentType()
Overrides:
getContentType in class org.apache.log4j.Layout
Returns:
the mime type of JSON

format

public String format(org.apache.log4j.spi.LoggingEvent event)
Specified by:
format in class org.apache.log4j.Layout

toJson

public String toJson(org.apache.log4j.spi.LoggingEvent event)
              throws IOException
Convert an event to JSON

Parameters:
event - the event -must not be null
Returns:
a string value
Throws:
IOException - on problems generating the JSON

toJson

public Writer toJson(Writer writer,
                     org.apache.log4j.spi.LoggingEvent event)
              throws IOException
Convert an event to JSON

Parameters:
writer - the destination writer
event - the event -must not be null
Returns:
the writer
Throws:
IOException - on problems generating the JSON

toJson

public Writer toJson(Writer writer,
                     String loggerName,
                     long timeStamp,
                     String level,
                     String threadName,
                     String message,
                     org.apache.log4j.spi.ThrowableInformation ti)
              throws IOException
Build a JSON entry from the parameters. This is public for testing.

Parameters:
writer - destination
loggerName - logger name
timeStamp - time_t value
level - level string
threadName - name of the thread
message - rendered message
ti - nullable thrown information
Returns:
the writer
Throws:
IOException - on any problem

ignoresThrowable

public boolean ignoresThrowable()
This appender does not ignore throwables

Specified by:
ignoresThrowable in class org.apache.log4j.Layout
Returns:
false, always

activateOptions

public void activateOptions()
Do nothing


parse

public static org.codehaus.jackson.node.ContainerNode parse(String json)
                                                     throws IOException
For use in tests

Parameters:
json - incoming JSON to parse
Returns:
a node tree
Throws:
IOException - on any parsing problems


Copyright © 2013 Apache Software Foundation. All Rights Reserved.