org.apache.log4j.spi.ThrowableInformation#getThrowableStrRep ( )源码实例Demo

下面列出了org.apache.log4j.spi.ThrowableInformation#getThrowableStrRep ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

private String getThrowableStr(LoggingEvent event) {
  ThrowableInformation throwable = event.getThrowableInformation();
  if (throwable == null) {
    return null;
  }
  StringBuilder sb = new StringBuilder();
  boolean isFirst = true;
  for (String s : throwable.getThrowableStrRep()) {
    if (isFirst) {
      isFirst = false;
    } else {
      sb.append(System.getProperty("line.separator"));
    }
    sb.append(s);
  }
  return sb.toString();
}
 
源代码2 项目: stendhal   文件: GameConsoleAppender.java
@Override
protected void append(final LoggingEvent loggingEvent) {
	final StringBuilder buf = new StringBuilder();
	buf.append(getLayout().format(loggingEvent));
	final ThrowableInformation ti = loggingEvent.getThrowableInformation();

	if (ti != null) {
		final String[] cause = ti.getThrowableStrRep();

		for (final String line : cause) {
			buf.append(line).append('\n');
		}
	}

	j2DClient.get().addEventLine(new HeaderLessEventLine(buf.toString(), NotificationType.CLIENT));
}
 
源代码3 项目: hadoop   文件: Log4Json.java
/**
 * 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;
}
 
源代码4 项目: bender   文件: BenderLayout.java
@Override
public String format(LoggingEvent event) {
  BenderLogEntry entry = new BenderLogEntry();
  entry.threadName = event.getThreadName();
  entry.posixTimestamp = event.getTimeStamp();
  entry.timestamp = FORMATTER.print(entry.posixTimestamp);
  entry.message = event.getRenderedMessage();
  entry.level = event.getLevel().toString();
  entry.logger = event.getLogger().getName();
  entry.alias = ALIAS;
  entry.version = VERSION;

  if (event.getThrowableInformation() != null) {
    final ThrowableInformation throwableInfo = event.getThrowableInformation();
    ExceptionLog ex = new ExceptionLog();

    if (throwableInfo.getThrowable().getClass().getCanonicalName() != null) {
      ex.clazz = throwableInfo.getThrowable().getClass().getCanonicalName();
    }
    if (throwableInfo.getThrowable().getMessage() != null) {
      ex.message = throwableInfo.getThrowable().getMessage();
    }
    if (throwableInfo.getThrowableStrRep() != null) {
      Arrays.asList(throwableInfo.getThrowableStrRep()).forEach(m -> {
        ex.stacktrace.add(m.replaceAll("\\t", "   "));
      });
    }
    entry.exception = ex;
  }

  LocationInfo locinfo = event.getLocationInformation();
  entry.file = locinfo.getFileName();
  entry.lineNumber = Integer.parseInt(locinfo.getLineNumber());
  entry.method = locinfo.getMethodName();
  entry.clazz = locinfo.getClassName();

  return GSON.toJson(entry) + "\n";
}
 
源代码5 项目: big-c   文件: Log4Json.java
/**
 * 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;
}
 
源代码6 项目: cacheonix-core   文件: Log4JLogRecord.java
/**
 * Set stack trace information associated with this Log4JLogRecord.
 * When this method is called, the stack trace in a
 * String-based format is made
 * available via the getThrownStackTrace() method.
 *
 * @param throwableInfo An org.apache.log4j.spi.ThrowableInformation to
 * associate with this Log4JLogRecord.
 * @see #getThrownStackTrace()
 */
public void setThrownStackTrace(ThrowableInformation throwableInfo) {
  String[] stackTraceArray = throwableInfo.getThrowableStrRep();

  StringBuffer stackTrace = new StringBuffer();
  String nextLine;

  for (int i = 0; i < stackTraceArray.length; i++) {
    nextLine = stackTraceArray[i] + "\n";
    stackTrace.append(nextLine);
  }

  _thrownStackTrace = stackTrace.toString();
}
 
源代码7 项目: samza   文件: LoggingEventJsonSerde.java
/**
 * 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;
}