下面列出了org.apache.log4j.spi.ThrowableInformation#getThrowable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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;
}
}
@Override
public Throwable getThrowable() {
ThrowableInformation ti = loggingEvent.getThrowableInformation();
if (ti != null) {
return ti.getThrowable();
}
return null;
}
/**
* 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;
}
/**
* 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;
}
@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));
}
}
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) {
}
}
@Override
public Throwable getThrowable(LoggingEvent record) {
ThrowableInformation throwableInformation = record.getThrowableInformation();
return throwableInformation != null ? throwableInformation.getThrowable() : null;
}