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(":"));
}
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);
}
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;
}
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));
}
}
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);
}
Aggregations