Java Code Examples for org.apache.log4j.spi.ThrowableInformation#getThrowable()

The following examples show how to use org.apache.log4j.spi.ThrowableInformation#getThrowable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DbEventRequestProcessor.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
private String getLoggingMesage( LoggingEvent event ) {

        Throwable throwable = null;
        ThrowableInformation throwableInfo = event.getThrowableInformation();
        if (throwableInfo != null && throwableInfo.getThrowable() != null) {
            // logging through methods like error(new Exception);
            throwable = throwableInfo.getThrowable();
        } else if (event.getMessage() instanceof Throwable) {
            // logging through methods like error("some message", new Exception);
            throwable = (Throwable) event.getMessage();
        }

        // first format the message using the layout
        String message = layout.format(event);
        // then append the exception stack trace
        if (throwable != null) {
            message = getExceptionMsg(throwable, message);
        }

        return message;

    }
 
Example 2
Source File: GelfMessageFactory.java    From xian with Apache License 2.0 5 votes vote down vote up
private static String extractStacktrace(ThrowableInformation throwableInformation) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    Throwable t = throwableInformation.getThrowable();
    if (t != null) {
        t.printStackTrace(pw);
        return sw.toString();
    } else {
        return null;
    }
}
 
Example 3
Source File: Log4jLogEvent.java    From xian with Apache License 2.0 5 votes vote down vote up
@Override
public Throwable getThrowable() {
    ThrowableInformation ti = loggingEvent.getThrowableInformation();
    if (ti != null) {
        return ti.getThrowable();
    }

    return null;
}
 
Example 4
Source File: Log4Json.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Build a JSON entry from the parameters. This is public for testing.
 *
 * @param writer destination
 * @param loggerName logger name
 * @param timeStamp time_t value
 * @param level level string
 * @param threadName name of the thread
 * @param message rendered message
 * @param ti nullable thrown information
 * @return the writer
 * @throws IOException on any problem
 */
public Writer toJson(final Writer writer,
                     final String loggerName,
                     final long timeStamp,
                     final String level,
                     final String threadName,
                     final String message,
                     final ThrowableInformation ti) throws IOException {
  JsonGenerator json = factory.createJsonGenerator(writer);
  json.writeStartObject();
  json.writeStringField(NAME, loggerName);
  json.writeNumberField(TIME, timeStamp);
  Date date = new Date(timeStamp);
  json.writeStringField(DATE, dateFormat.format(date));
  json.writeStringField(LEVEL, level);
  json.writeStringField(THREAD, threadName);
  json.writeStringField(MESSAGE, message);
  if (ti != null) {
    //there is some throwable info, but if the log event has been sent over the wire,
    //there may not be a throwable inside it, just a summary.
    Throwable thrown = ti.getThrowable();
    String eclass = (thrown != null) ?
        thrown.getClass().getName()
        : "";
    json.writeStringField(EXCEPTION_CLASS, eclass);
    String[] stackTrace = ti.getThrowableStrRep();
    json.writeArrayFieldStart(STACK);
    for (String row : stackTrace) {
      json.writeString(row);
    }
    json.writeEndArray();
  }
  json.writeEndObject();
  json.flush();
  json.close();
  return writer;
}
 
Example 5
Source File: Log4Json.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Build a JSON entry from the parameters. This is public for testing.
 *
 * @param writer destination
 * @param loggerName logger name
 * @param timeStamp time_t value
 * @param level level string
 * @param threadName name of the thread
 * @param message rendered message
 * @param ti nullable thrown information
 * @return the writer
 * @throws IOException on any problem
 */
public Writer toJson(final Writer writer,
                     final String loggerName,
                     final long timeStamp,
                     final String level,
                     final String threadName,
                     final String message,
                     final ThrowableInformation ti) throws IOException {
  JsonGenerator json = factory.createJsonGenerator(writer);
  json.writeStartObject();
  json.writeStringField(NAME, loggerName);
  json.writeNumberField(TIME, timeStamp);
  Date date = new Date(timeStamp);
  json.writeStringField(DATE, dateFormat.format(date));
  json.writeStringField(LEVEL, level);
  json.writeStringField(THREAD, threadName);
  json.writeStringField(MESSAGE, message);
  if (ti != null) {
    //there is some throwable info, but if the log event has been sent over the wire,
    //there may not be a throwable inside it, just a summary.
    Throwable thrown = ti.getThrowable();
    String eclass = (thrown != null) ?
        thrown.getClass().getName()
        : "";
    json.writeStringField(EXCEPTION_CLASS, eclass);
    String[] stackTrace = ti.getThrowableStrRep();
    json.writeArrayFieldStart(STACK);
    for (String row : stackTrace) {
      json.writeString(row);
    }
    json.writeEndArray();
  }
  json.writeEndObject();
  json.flush();
  json.close();
  return writer;
}
 
Example 6
Source File: DebugAppender.java    From ChatGameFontificator with The Unlicense 5 votes vote down vote up
@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));
    }
}
 
Example 7
Source File: Console.java    From olca-app with Mozilla Public License 2.0 5 votes vote down vote up
private void tryPrintMessage(String message, ThrowableInformation info) {
	try {
		stream.println(message);
		if (info != null) {
			Throwable throwable = info.getThrowable();
			if (throwable != null) {
				stream.println(throwable.getMessage());
				throwable.printStackTrace(new PrintStream(stream));
			}
		}
	} catch (Exception e) {
	}
}
 
Example 8
Source File: GlogLayout.java    From xio with Apache License 2.0 4 votes vote down vote up
@Override
public Throwable getThrowable(LoggingEvent record) {
  ThrowableInformation throwableInformation = record.getThrowableInformation();
  return throwableInformation != null ? throwableInformation.getThrowable() : null;
}