Search in sources :

Example 1 with ThrowableInformation

use of org.apache.log4j.spi.ThrowableInformation in project hadoop by apache.

the class TestLog4Json method testNestedException.

@Test
public void testNestedException() throws Throwable {
    Exception e = new NoRouteToHostException("that box caught fire 3 years ago");
    Exception ioe = new IOException("Datacenter problems", e);
    ThrowableInformation ti = new ThrowableInformation(ioe);
    Log4Json l4j = new Log4Json();
    long timeStamp = Time.now();
    String outcome = l4j.toJson(new StringWriter(), "testNestedException", timeStamp, "INFO", "quoted\"", "new line\n and {}", ti).toString();
    println("testNestedException", outcome);
    ContainerNode rootNode = Log4Json.parse(outcome);
    assertEntryEquals(rootNode, Log4Json.LEVEL, "INFO");
    assertEntryEquals(rootNode, Log4Json.NAME, "testNestedException");
    assertEntryEquals(rootNode, Log4Json.TIME, timeStamp);
    assertEntryEquals(rootNode, Log4Json.EXCEPTION_CLASS, ioe.getClass().getName());
    JsonNode node = assertNodeContains(rootNode, Log4Json.STACK);
    assertTrue("Not an array: " + node, node.isArray());
    node = assertNodeContains(rootNode, Log4Json.DATE);
    assertTrue("Not a string: " + node, node.isTextual());
    //rather than try and make assertions about the format of the text
    //message equalling another ISO date, this test asserts that the hypen
    //and colon characters are in the string.
    String dateText = node.textValue();
    assertTrue("No '-' in " + dateText, dateText.contains("-"));
    assertTrue("No '-' in " + dateText, dateText.contains(":"));
}
Also used : StringWriter(java.io.StringWriter) ThrowableInformation(org.apache.log4j.spi.ThrowableInformation) ContainerNode(com.fasterxml.jackson.databind.node.ContainerNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) NoRouteToHostException(java.net.NoRouteToHostException) IOException(java.io.IOException) NoRouteToHostException(java.net.NoRouteToHostException) Test(org.junit.Test)

Example 2 with ThrowableInformation

use of org.apache.log4j.spi.ThrowableInformation in project hadoop by apache.

the class TestLog4Json method testException.

@Test
public void testException() throws Throwable {
    Exception e = new NoRouteToHostException("that box caught fire 3 years ago");
    ThrowableInformation ti = new ThrowableInformation(e);
    Log4Json l4j = new Log4Json();
    long timeStamp = Time.now();
    String outcome = l4j.toJson(new StringWriter(), "testException", timeStamp, "INFO", "quoted\"", "new line\n and {}", ti).toString();
    println("testException", outcome);
}
Also used : StringWriter(java.io.StringWriter) ThrowableInformation(org.apache.log4j.spi.ThrowableInformation) NoRouteToHostException(java.net.NoRouteToHostException) IOException(java.io.IOException) NoRouteToHostException(java.net.NoRouteToHostException) Test(org.junit.Test)

Example 3 with ThrowableInformation

use of org.apache.log4j.spi.ThrowableInformation in project samza by apache.

the class LoggingEventJsonSerde method encodeToMap.

/**
 * Encodes a LoggingEvent into a HashMap using the logstash JSON format.
 *
 * @param loggingEvent
 *          The LoggingEvent to encode.
 * @param includeLocationInfo
 *          Whether to include LocationInfo in the map, or not.
 * @return A Map representing the LoggingEvent, which is suitable to be
 *         serialized by a JSON encoder such as Jackson.
 */
@SuppressWarnings("rawtypes")
public static Map<String, Object> encodeToMap(LoggingEvent loggingEvent, boolean includeLocationInfo) {
    Map<String, Object> logstashEvent = new LoggingEventMap();
    String threadName = loggingEvent.getThreadName();
    long timestamp = loggingEvent.getTimeStamp();
    HashMap<String, Object> exceptionInformation = new HashMap<String, Object>();
    Map mdc = loggingEvent.getProperties();
    String ndc = loggingEvent.getNDC();
    logstashEvent.put("@version", VERSION);
    logstashEvent.put("@timestamp", dateFormat(timestamp));
    logstashEvent.put("source_host", getHostname());
    logstashEvent.put("message", loggingEvent.getRenderedMessage());
    if (loggingEvent.getThrowableInformation() != null) {
        final ThrowableInformation throwableInformation = loggingEvent.getThrowableInformation();
        if (throwableInformation.getThrowable().getClass().getCanonicalName() != null) {
            exceptionInformation.put("exception_class", throwableInformation.getThrowable().getClass().getCanonicalName());
        }
        if (throwableInformation.getThrowable().getMessage() != null) {
            exceptionInformation.put("exception_message", throwableInformation.getThrowable().getMessage());
        }
        if (throwableInformation.getThrowableStrRep() != null) {
            StringBuilder stackTrace = new StringBuilder();
            for (String line : throwableInformation.getThrowableStrRep()) {
                stackTrace.append(line);
                stackTrace.append("\n");
            }
            exceptionInformation.put("stacktrace", stackTrace);
        }
        logstashEvent.put("exception", exceptionInformation);
    }
    if (includeLocationInfo) {
        LocationInfo info = loggingEvent.getLocationInformation();
        logstashEvent.put("file", info.getFileName());
        logstashEvent.put("line_number", info.getLineNumber());
        logstashEvent.put("class", info.getClassName());
        logstashEvent.put("method", info.getMethodName());
    }
    logstashEvent.put("logger_name", loggingEvent.getLoggerName());
    logstashEvent.put("mdc", mdc);
    logstashEvent.put("ndc", ndc);
    logstashEvent.put("level", loggingEvent.getLevel().toString());
    logstashEvent.put("thread_name", threadName);
    return logstashEvent;
}
Also used : HashMap(java.util.HashMap) ThrowableInformation(org.apache.log4j.spi.ThrowableInformation) Map(java.util.Map) HashMap(java.util.HashMap) LocationInfo(org.apache.log4j.spi.LocationInfo)

Example 4 with ThrowableInformation

use of org.apache.log4j.spi.ThrowableInformation in project ChatGameFontificator by GlitchCog.

the class DebugAppender method append.

@Override
public void append(LoggingEvent event) {
    debugLogBox.log(this.layout.format(event));
    ThrowableInformation info = event.getThrowableInformation();
    if (info != null && info.getThrowable() != null) {
        Throwable t = info.getThrowable();
        debugLogBox.log(throwableToString(t));
    }
}
Also used : ThrowableInformation(org.apache.log4j.spi.ThrowableInformation)

Example 5 with ThrowableInformation

use of org.apache.log4j.spi.ThrowableInformation in project commons by twitter.

the class JULBridgeHandler method toLoggingEvent.

/**
 * Converts a JUL log record into a Log4J logging event.
 *
 * @param record the JUL log record to convert
 * @param logger the Log4J logger to use for the logging event
 * @param level the Log4J level to use for the logging event
 * @param useExtendedLocationInfo if false, do no try to get source file and line informations
 * @return a Log4J logging event
 */
static LoggingEvent toLoggingEvent(LogRecord record, Logger logger, Level level, boolean useExtendedLocationInfo) {
    LocationInfo locationInfo = useExtendedLocationInfo ? new LocationInfo(new Throwable(), record.getSourceClassName()) : new LocationInfo("?", record.getSourceClassName(), record.getSourceMethodName(), "?");
    // Getting thread name from thread id? complicated...
    String threadName = String.valueOf(record.getThreadID());
    ThrowableInformation throwableInformation = record.getThrown() == null ? null : new ThrowableInformation(record.getThrown());
    return new LoggingEvent(record.getSourceClassName(), logger, record.getMillis(), level, formatMessage(record), threadName, throwableInformation, null, /* ndc */
    locationInfo, null);
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) ThrowableInformation(org.apache.log4j.spi.ThrowableInformation) LocationInfo(org.apache.log4j.spi.LocationInfo)

Aggregations

ThrowableInformation (org.apache.log4j.spi.ThrowableInformation)21 LocationInfo (org.apache.log4j.spi.LocationInfo)5 StringWriter (java.io.StringWriter)3 Date (java.util.Date)3 Level (org.apache.log4j.Level)3 LoggingEvent (org.apache.log4j.spi.LoggingEvent)3 IOException (java.io.IOException)2 NoRouteToHostException (java.net.NoRouteToHostException)2 Map (java.util.Map)2 Test (org.junit.Test)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ContainerNode (com.fasterxml.jackson.databind.node.ContainerNode)1 IdeaPluginDescriptor (com.intellij.ide.plugins.IdeaPluginDescriptor)1 IdeaApplication (com.intellij.idea.IdeaApplication)1 Application (com.intellij.openapi.application.Application)1 ErrorLogger (com.intellij.openapi.diagnostic.ErrorLogger)1 IdeaLoggingEvent (com.intellij.openapi.diagnostic.IdeaLoggingEvent)1 LogEvent (io.fabric8.insight.log.LogEvent)1 PrintWriter (java.io.PrintWriter)1 Instant (java.time.Instant)1