@Test
  public void testAppendThrowable() throws SQLException {
    ILoggingEvent event = createLoggingEvent();

    appender.append(event);

    Statement stmt = connectionSource.getConnection().createStatement();
    ResultSet rs = null;
    rs = stmt.executeQuery("SELECT * FROM LOGGING_EVENT_EXCEPTION where EVENT_ID = 0");

    rs.next();
    String expected = "java.lang.Exception: test Ex";
    String firstLine = rs.getString(3);
    assertTrue(
        "[" + firstLine + "] does not match [" + expected + "]", firstLine.contains(expected));

    int i = 0;
    while (rs.next()) {
      expected = event.getThrowableProxy().getStackTraceElementProxyArray()[i].toString();
      String st = rs.getString(3);
      assertTrue("[" + st + "] does not match [" + expected + "]", st.contains(expected));
      i++;
    }
    assertTrue(i != 0);
    rs.close();
    stmt.close();
  }
  private String getStackTraceFromEvent(ILoggingEvent logEvent) {
    IThrowableProxy throwable = logEvent.getThrowableProxy();
    if (null != throwable && null != throwable.getStackTraceElementProxyArray()) {
      StringBuilder sb = new StringBuilder();
      for (StackTraceElementProxy elt : throwable.getStackTraceElementProxyArray()) {
        sb.append(elt.toString());
        sb.append("\n");
      }
      return sb.toString();
    }

    if (logEvent.getCallerData() != null) {
      if (debug) {
        System.err.println(String.format("%s.append: falling back to appender stacktrace: ", this));
      }
      StringBuilder sb = new StringBuilder();
      for (StackTraceElement elt : logEvent.getCallerData()) {
        sb.append(elt);
        sb.append("\n");
      }
      return sb.toString();
    }

    return null;
  }
  @Override
  protected void append(ILoggingEvent p) {
    if (!enable || eventWriter == null || !eventWriter.isEnable()) {
      return;
    }

    Level logLevel = p.getLevel();
    if (logLevel.toInt() < minimumLevel) {
      return;
    }

    RecordEventSummary eventInfo = new RecordEventSummary(-1);

    eventInfo.setEventType(eventType);
    eventInfo.setThreadName(p.getThreadName());
    eventInfo.setEventDate(new Date(p.getTimeStamp()));
    eventInfo.setEventClassName(p.getLoggerName());
    eventInfo.setEventMethodName(logLevel.toString().toLowerCase());

    // use message instead of formattedMessage for header!!
    // => use "... {0} .. {1} .." to compress event summary encoding
    eventInfo.setEventMethodDetail(p.getMessage());

    if (!eventWriter.isEnable(eventInfo)) {
      return;
    }

    LogbackEventData eventData = new LogbackEventData();
    eventData.setLevel(eventInfo, logLevel.toString());
    eventData.setFormattedMessage(p.getFormattedMessage());
    eventData.setArgumentArray(p.getArgumentArray());

    if (p.getThrowableProxy() != null) {
      IThrowableProxy throwableProxy = p.getThrowableProxy();
      // throwableProxy.getClassName()
      // throwableProxy.getMessage()
      StackTraceElementProxy[] traceElts = throwableProxy.getStackTraceElementProxyArray();
      eventData.setStackTraceElements(traceElts);
    }

    Map<String, String> mdcPropertyMap = p.getMDCPropertyMap();
    if (mdcPropertyMap != null && !mdcPropertyMap.isEmpty()) {
      eventData.setMDCPropertyMap(mdcPropertyMap);
    }

    eventWriter.addEvent(eventInfo, eventData, null);
  }
Esempio n. 4
0
  private Throwable getThrowable(ILoggingEvent logEvent) {
    if (logEvent.getThrowableProxy() instanceof ThrowableProxy) {
      return ((ThrowableProxy) logEvent.getThrowableProxy()).getThrowable();
    }

    Object[] args = logEvent.getArgumentArray();
    if (args == null || args.length == 0) {
      return null;
    }

    Object lastObject = args[args.length - 1];
    if (lastObject instanceof Throwable) {
      return (Throwable) lastObject;
    }

    return null;
  }
Esempio n. 5
0
 private void stackTraceField(Map<String, Object> map, ILoggingEvent eventObject) {
   IThrowableProxy throwableProxy = eventObject.getThrowableProxy();
   if (throwableProxy != null) {
     StackTraceElementProxy[] proxyStackTraces = throwableProxy.getStackTraceElementProxyArray();
     if (proxyStackTraces != null && proxyStackTraces.length > 0) {
       StackTraceElement[] callStackTraces = eventObject.getCallerData();
       if (callStackTraces.length > 0) {
         StackTraceElement lastStack = callStackTraces[0];
         map.put("file", lastStack.getFileName());
         map.put("line", String.valueOf(lastStack.getLineNumber()));
       }
     }
   }
 }
Esempio n. 6
0
  private String createStackTrace(final ILoggingEvent eventObject) {

    final IThrowableProxy tp = eventObject.getThrowableProxy();
    if (tp != null) {
      final StringBuilder stringBuilder =
          new StringBuilder(tp.getClassName()).append(": ").append(tp.getMessage());
      for (final StackTraceElementProxy stackTraceLine : tp.getStackTraceElementProxyArray()) {
        stringBuilder.append(stackTraceLine.toString()).append("\n");
      }

      return stringBuilder.toString();
    }
    return null;
  }