public String fullDump(ILoggingEvent evt) { try { IThrowableProxy proxy = evt.getThrowableProxy(); if(proxy == null) return null; StringBuilder builder = new StringBuilder(); for (StackTraceElementProxy step : proxy .getStackTraceElementProxyArray()) { String string = step.toString(); builder.append(CoreConstants.TAB).append(string); ThrowableProxyUtil.subjoinPackagingData(builder, step); builder.append(CoreConstants.LINE_SEPARATOR); } return builder.toString(); } catch(Exception e) { addError("exception trying to log exception", e); return "exception parsing exception"; } }
private void appendThrowable(IThrowableProxy proxy, ILoggingEvent loggingEvent) { //Append Exception Message appendToLog(loggingEvent, String.format("%s: %s", proxy.getClassName(), proxy.getMessage())); //Append Exception Stack Trace for (StackTraceElementProxy element : loggingEvent.getThrowableProxy().getStackTraceElementProxyArray()) { appendToLog(loggingEvent, "\t" + element.getSTEAsString()); } if (proxy.getSuppressed().length > 0) { appendToLog(loggingEvent, "Suppressed Exceptions:"); for (IThrowableProxy p : proxy.getSuppressed()) { appendThrowable(p, loggingEvent); } } if (proxy.getCause() != null) { appendToLog(loggingEvent, "Cause:"); appendThrowable(proxy.getCause(), loggingEvent); } }
void render(StringBuilder sbuf, IThrowableProxy tp) { printFirstLine(sbuf, tp); int commonFrames = tp.getCommonFrames(); StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray(); for (int i = 0; i < stepArray.length - commonFrames; i++) { StackTraceElementProxy step = stepArray[i]; sbuf.append(TRACE_PREFIX); sbuf.append(Transform.escapeTags(step.toString())); sbuf.append(CoreConstants.LINE_SEPARATOR); } if (commonFrames > 0) { sbuf.append(TRACE_PREFIX); sbuf.append("\t... ").append(commonFrames).append(" common frames omitted").append(CoreConstants.LINE_SEPARATOR); } }
private String transformStackTrace(ILoggingEvent event) { IThrowableProxy throwableProxy = event.getThrowableProxy(); StackTraceElement stackTraceElement = throwableProxy.getStackTraceElementProxyArray()[0].getStackTraceElement(); String time = DEFAULT_DATE_FORMAT.format(new Date(event.getTimeStamp())); String threadName = event.getThreadName(); String level = event.getLevel().toString().substring(0, 1); String logger = event.getLoggerName(); String exception = throwableProxy.getClassName(); String msg = throwableProxy.getMessage(); String className = stackTraceElement.getClassName(); String method = stackTraceElement.getMethodName(); int lineNumber = stackTraceElement.getLineNumber(); return String.format(FORMAT_MESSAGE, title, time, threadName, level, logger, exception, msg, className, method, lineNumber); }
@Override protected void append(ILoggingEvent event) { if (event == null) { return; } if (event.getLevel() == Level.ERROR || event.getLevel() == Level.WARN) { IThrowableProxy throwableProxy = event.getThrowableProxy(); if (throwableProxy != null) { //接口名 String errorClassName = throwableProxy.getClassName(); if (errorClassName != null && !"".equals(errorClassName.trim())) { //写入AtomicLongMap并计数 ERROR_NAME_VALUE_MAP.getAndIncrement(errorClassName); } } } }
protected void recursiveAppendRootCauseFirst(StringBuilder sb, String prefix, int indent, IThrowableProxy tp) { if (tp.getCause() != null) { recursiveAppendRootCauseFirst(sb, prefix, indent, tp.getCause()); prefix = null; // to avoid adding it more than once } ThrowableProxyUtil.indent(sb, indent - 1); if (prefix != null) { sb.append(prefix); } ThrowableProxyUtil.subjoinFirstLineRootCauseFirst(sb, tp); sb.append(CoreConstants.LINE_SEPARATOR); subjoinSTEPArray(sb, indent, tp); IThrowableProxy[] suppressed = tp.getSuppressed(); if(suppressed != null) { for(IThrowableProxy current : suppressed) { recursiveAppendRootCauseFirst(sb, CoreConstants.SUPPRESSED, indent + ThrowableProxyUtil.SUPPRESSED_EXCEPTION_INDENT, current); } } }
void render(StringBuilder sbuf, IThrowableProxy tp) { printFirstLine(sbuf, tp); int commonFrames = tp.getCommonFrames(); StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray(); for (int i = 0; i < stepArray.length - commonFrames; i++) { StackTraceElementProxy step = stepArray[i]; sbuf.append(TRACE_PREFIX); sbuf.append(Transform.escapeTags(step.toString())); sbuf.append(CoreConstants.LINE_SEPARATOR); } if (commonFrames > 0) { sbuf.append(TRACE_PREFIX); sbuf.append("\t... ").append(commonFrames).append(" common frames omitted") .append(CoreConstants.LINE_SEPARATOR); } }
void subjoinThrowableProxy(StringBuilder buf, IThrowableProxy tp) { subjoinFirstLine(buf, tp); buf.append(CoreConstants.LINE_SEPARATOR); final StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray(); final int commonFrames = tp.getCommonFrames(); int maxIndex = stepArray.length; if (commonFrames > 0) { maxIndex -= commonFrames; } for (int i = 0; i < maxIndex; i++) { final String string = stepArray[i].toString(); buf.append(PREFIX); buf.append(string); extraData(buf, stepArray[i]); // allow other data to be added buf.append(CoreConstants.LINE_SEPARATOR); } if (commonFrames > 0) { buf.append("!... ").append(tp.getCommonFrames()).append( " common frames omitted").append(CoreConstants.LINE_SEPARATOR); } }
private List<StackTraceVO> convertToStackTraces(final IThrowableProxy throwableProxy) { final StackTraceElementProxy[] stackTraceElementProxies = throwableProxy.getStackTraceElementProxyArray(); final List<StackTraceVO> stackTraces = Lists.newArrayList(); for (final StackTraceElementProxy stackTraceElementProxy : stackTraceElementProxies) { final StackTraceElement stackTraceElement = stackTraceElementProxy.getStackTraceElement(); final StackTraceVO stackTrace = new StackTraceVO(); stackTrace.setMethod(stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName()); stackTrace.setLineNumber(stackTraceElement.getLineNumber()); stackTrace.setFile(stackTraceElement.getFileName()); stackTrace.setInProject(configuration.isInProject(stackTraceElement.getClassName())); stackTraces.add(stackTrace); } return stackTraces; }
/** * Builds an exception causation string by following the exception caused-by chain. * @param exception The exception to process. * @return A string describing all exceptions in the chain. */ private static String buildCausalString(IThrowableProxy exception) { final StringBuilder buff = new StringBuilder(); buff.append(exception.getClassName()); if (exception.getMessage() != null) { buff.append(": ").append(exception.getMessage()); } if (exception.getCause() != null) { buff.append("; caused by: ").append(buildCausalString(exception.getCause())); } return buff.toString(); }
/** * fluentdサーバに転送するログデータに格納する例外情報を生成する。 * * @param th 例外データ。 * @return 例外情報。 */ private Map<String, Object> createThrowableData(IThrowableProxy th) { Map<String, Object> data = new LinkedHashMap<>(); data.put("className", th.getClassName()); data.put("message", th.getMessage()); List<String> stackTrace = new ArrayList<>(); for (StackTraceElementProxy ste : th.getStackTraceElementProxyArray()) { stackTrace.add(ste.toString()); } data.put("stackTrace", stackTrace); if (th.getCause() != null) { data.put("cause", createThrowableData(th.getCause())); } return data; }
private static Proto.Throwable toProto(IThrowableProxy t) { Proto.Throwable.Builder builder = Proto.Throwable.newBuilder() .setClassName(t.getClassName()); String message = t.getMessage(); if (message != null) { builder.setMessage(message); } for (StackTraceElementProxy element : t.getStackTraceElementProxyArray()) { builder.addStackTraceElement(ErrorMessage.toProto(element.getStackTraceElement())); } builder.setFramesInCommonWithEnclosing(t.getCommonFrames()); IThrowableProxy cause = t.getCause(); if (cause != null) { builder.setCause(toProto(cause)); } for (IThrowableProxy suppressed : t.getSuppressed()) { builder.addSuppressed(toProto(suppressed)); } return builder.build(); }
@Test @SuppressFBWarnings(value = "SIC_INNER_SHOULD_BE_STATIC_ANON") public void testEncodeArrayWithNullSuppressedException() throws Exception { final LoggingEvent event = new LoggingEvent(); event.setLevel(Level.INFO); event.setMarker(StenoMarker.ARRAY_MARKER); event.setMessage("logEvent"); event.setLoggerContextRemoteView(_context.getLoggerContextRemoteView()); event.setTimeStamp(0); event.setThrowableProxy(new ThrowableProxy(new NullPointerException("npe!")) { @Override @SuppressFBWarnings(value = "PZLA_PREFER_ZERO_LENGTH_ARRAYS") public IThrowableProxy[] getSuppressed() { return null; } }); final Object[] argArray = new Object[2]; argArray[0] = new String[]{}; argArray[1] = new Object[]{}; event.setArgumentArray(argArray); // CHECKSTYLE.OFF: IllegalInstantiation - This is valid case. final String logOutput = new String(_encoder.encode(event), _encoder.getCharset()); // CHECKSTYLE.ON: IllegalInstantiation assertOutput("StenoEncoderTest.testEncodeArrayWithNullSuppressedException.json", logOutput); assertMatchesJsonSchema(logOutput); }
@Override protected void append(ILoggingEvent event) { final IThrowableProxy throwableProxy = event.getThrowableProxy(); final Map<String, String> mdc = event.getMDCPropertyMap(); if ((throwableProxy != null) && (throwableProxy instanceof ThrowableProxy)) { ThrowableProxy proxy = (ThrowableProxy) throwableProxy; if (proxy.getThrowable() != null) { Map<String, String> params = new HashMap<>(mdc); if (!mdc.containsKey(MESSAGE_PARAM)) { params.put(MESSAGE_PARAM, event.getFormattedMessage()); } noticeError(proxy.getThrowable(), params); return; } } noticeError(event.getFormattedMessage(), mdc); }
@Override protected void append(ILoggingEvent event) { event.prepareForDeferredProcessing(); //todo do more elegant streaming approach to publish logs if (!event.getLevel().equals(Level.ALL) && // OFF AND ALL are not loggable levels !event.getLevel().equals(Level.OFF)) { final IThrowableProxy throwableProxy = event.getThrowableProxy(); final LogEntry entry = throwableProxy != null ? new LogEntry(serverId, event.getFormattedMessage(), Instant.ofEpochMilli(event.getTimeStamp()).toString(), event.getLevel().toString(), event.getLoggerName(), event.getMDCPropertyMap(), event.getThreadName() != null ? event.getThreadName() : null, new Exception(throwableProxy.getMessage(), toStringArray(throwableProxy.getStackTraceElementProxyArray()) , throwableProxy.getClassName())) : new LogEntry(serverId, event.getFormattedMessage(), Instant.ofEpochMilli(event.getTimeStamp()).toString(), event.getLevel().toString(), event.getLoggerName(), event.getMDCPropertyMap(), event.getThreadName() != null ? event.getThreadName() : null); producer.send(new ProducerRecord<>(kafkaTopic, new Gson().toJson(entry))); } }
@Override public FilterReply decide(ILoggingEvent event) { final IThrowableProxy throwableProxy = event.getThrowableProxy(); if (throwableProxy == null) { return FilterReply.NEUTRAL; } if (!(throwableProxy instanceof ThrowableProxy)) { return FilterReply.NEUTRAL; } final ThrowableProxy throwableProxyImpl = (ThrowableProxy) throwableProxy; final Throwable throwable = throwableProxyImpl.getThrowable(); if (java.nio.channels.ClosedChannelException.class.isInstance(throwable)) { return FilterReply.DENY; } return FilterReply.NEUTRAL; }
/** * Pull out details of exception in a Hashmap (if they exist) * * @param loggingEvent * @return */ protected void exceptionInformation( final ILoggingEvent loggingEvent) { final IThrowableProxy throwableInformation = loggingEvent .getThrowableProxy(); if (throwableInformation != null) { if (throwableInformation.getClassName() != null) { ExceptionField.put(EXCEPTION.CLASS, throwableInformation.getClassName()); } if (throwableInformation.getMessage() != null) { ExceptionField.put(EXCEPTION.MESSAGE, throwableInformation.getMessage()); } if (throwableInformation.getStackTraceElementProxyArray() != null) { final String stackTrace = StringUtils.join( throwableInformation.getStackTraceElementProxyArray(), "\n"); ExceptionField.put(EXCEPTION.STACKTRACE, stackTrace); } } }
@Override protected void append(ILoggingEvent eventObject) { final IThrowableProxy exception = getException(eventObject); for (String exceptionName : exceptionAndMessage.keySet()) { if (exception != null && exception.getClassName().endsWith(exceptionName) && containsMessage(exception, exceptionName)) { aai.appendLoopOnAppenders(new ExpectedExceptionEventWrapper(eventObject)); return; } } aai.appendLoopOnAppenders(eventObject); }
private IThrowableProxy getException(ILoggingEvent event) { final IThrowableProxy throwableProxy = event.getThrowableProxy(); if (throwableProxy != null) { final IThrowableProxy cause = throwableProxy.getCause(); if (cause != null) { return cause; } } return null; }
private boolean containsMessage(IThrowableProxy exception, String exceptionName) { final String message = exception.getMessage(); if (message != null && message.contains(exceptionAndMessage.get(exceptionName))) { return true; } return false; }
public void render(StringBuilder sbuf, ILoggingEvent event) { IThrowableProxy tp = event.getThrowableProxy(); while (tp != null) { render(sbuf, tp); tp = tp.getCause(); } }
public void printFirstLine(StringBuilder sb, IThrowableProxy tp) { int commonFrames = tp.getCommonFrames(); if (commonFrames > 0) { sb.append("\t").append(CoreConstants.CAUSED_BY); } sb.append(tp.getClassName()).append(": ").append(Transform.escapeTags(tp.getMessage())); sb.append(CoreConstants.LINE_SEPARATOR); }
private void appendEvent(E eventObject) { //init Event Object if (!(eventObject instanceof LoggingEvent)) { return; } LoggingEvent event = (LoggingEvent) eventObject; List<LogItem> logItems = new ArrayList<LogItem>(); LogItem item = new LogItem(); logItems.add(item); item.SetTime((int) (event.getTimeStamp() / 1000)); //item.PushBack("time", formatter.format(new Date(event.getTimeStamp()))); item.PushBack("level", event.getLevel().toString()); item.PushBack("thread", event.getThreadName()); StackTraceElement[] caller = event.getCallerData(); if (caller != null && caller.length > 0) { item.PushBack("location", caller[0].toString()); } String message = event.getFormattedMessage(); IThrowableProxy iThrowableProxy = event.getThrowableProxy(); if (iThrowableProxy != null) { message += CoreConstants.LINE_SEPARATOR; message += getExceptionInfo(iThrowableProxy); message += fullDump(event.getThrowableProxy().getStackTraceElementProxyArray()); } item.PushBack("message", message); producer.send(projectConfig.projectName, logstore, topic, null, logItems, new LoghubAppenderCallback<E>(this, projectConfig.projectName, logstore, topic, null, logItems)); }
@Override protected void append(ILoggingEvent event) { StringBuilder message = new StringBuilder(event.getFormattedMessage()); IThrowableProxy throwableProxy = event.getThrowableProxy(); while (throwableProxy != null) { message.append("\n").append(dump(throwableProxy)); throwableProxy = throwableProxy.getCause(); if (throwableProxy != null) { message.append("\nCaused by:"); } } String account = event.getMDCPropertyMap().get(CloudwatchLogsMDCPropertyNames.ACCOUNT); String action = event.getMDCPropertyMap().get(CloudwatchLogsMDCPropertyNames.ACTION); String user = event.getMDCPropertyMap().get(CloudwatchLogsMDCPropertyNames.USER); String session = event.getMDCPropertyMap().get(CloudwatchLogsMDCPropertyNames.SESSION); String request = event.getMDCPropertyMap().get(CloudwatchLogsMDCPropertyNames.REQUEST); Marker marker = event.getMarker(); String eventId = marker == null ? null : marker.getName(); CloudwatchLogsLogEvent logEvent = new CloudwatchLogsLogEvent(event.getLevel().toString(), event.getLoggerName(), eventId, message.toString(), event.getTimeStamp(), event.getThreadName(), account, action, user, session, request); while (!eventQueue.offer(logEvent)) { // Discard old logging messages while queue is full. eventQueue.poll(); discardedCount.incrementAndGet(); } processedCount.incrementAndGet(); }
private String dump(IThrowableProxy throwableProxy) { StringBuilder builder = new StringBuilder(); builder.append(throwableProxy.getClassName()).append(": ").append(throwableProxy.getMessage()).append(CoreConstants.LINE_SEPARATOR); for (StackTraceElementProxy step : throwableProxy.getStackTraceElementProxyArray()) { String string = step.toString(); builder.append(CoreConstants.TAB).append(string); ThrowableProxyUtil.subjoinPackagingData(builder, step); builder.append(CoreConstants.LINE_SEPARATOR); } return builder.toString(); }
DefaultLogThrowable(IThrowableProxy throwableProxy) { this.className = throwableProxy.getClassName(); this.message = throwableProxy.getMessage(); StackTraceElementProxy[] stackTraceElementProxyArray = throwableProxy.getStackTraceElementProxyArray(); this.stackTraces = new StackTraceElement[stackTraceElementProxyArray.length]; for (int i = 0; i < stackTraceElementProxyArray.length; i++) { stackTraces[i] = stackTraceElementProxyArray[i].getStackTraceElement(); } cause = (throwableProxy.getCause() == null) ? null : new DefaultLogThrowable(throwableProxy.getCause()); }
private Map<String, Object> buildRootExceptionData(final IThrowableProxy throwableProxy) { final IThrowableProxy rootException = getRootException(throwableProxy); if (rootException == null) { return Collections.emptyMap(); } final Map<String, Object> exceptionDataMap = new HashMap<>(2); exceptionDataMap.put("root_cause_class_name", rootException.getClassName()); exceptionDataMap.put("root_cause_message", rootException.getMessage()); return exceptionDataMap; }
private IThrowableProxy getRootException(final IThrowableProxy throwableProxy) { if (throwableProxy == null) { return null; } IThrowableProxy rootCause = throwableProxy; while (rootCause.getCause() != null) { rootCause = rootCause.getCause(); } return rootCause; }
@Override public String convert(ILoggingEvent event) { StringBuilder appendTo = new StringBuilder(); IThrowableProxy tProxy = event.getThrowableProxy(); if (tProxy != null && ThrowableProxy.class.isAssignableFrom(tProxy.getClass())) { StacktraceConverter.CONVERTER.convert(((ThrowableProxy) tProxy).getThrowable(), appendTo); return appendTo.toString(); } return null; }
@Override public String convert(ILoggingEvent event) { final IThrowableProxy tp = event.getThrowableProxy(); if (tp == null) { return CoreConstants.EMPTY_STRING; } try { IStacktrace st = Stacktrace.getInstance(new ThrowableProxyThrowable(tp), includeLineNumber); return exHash.hash(st); } catch (StacktraceHashException e) { addError(e.getMessage(), e); return CoreConstants.EMPTY_STRING; } }
Event(ILoggingEvent event) { this.marker = event.getMarker(); this.level = event.getLevel().toString(); this.message = event.getMessage(); this.arguments = event.getArgumentArray(); final IThrowableProxy proxy = event.getThrowableProxy(); this.thrown = proxy == null ? null : proxy.getClassName() + ": " + proxy.getMessage(); }
private void recursiveAppend(StringBuilder sb, String prefix, int indent, IThrowableProxy tp) { if(tp == null) return; subjoinFirstLine(sb, prefix, indent, tp); sb.append(CoreConstants.LINE_SEPARATOR); subjoinSTEPArray(sb, indent, tp); IThrowableProxy[] suppressed = tp.getSuppressed(); if(suppressed != null) { for(IThrowableProxy current : suppressed) { recursiveAppend(sb, CoreConstants.SUPPRESSED, indent + ThrowableProxyUtil.SUPPRESSED_EXCEPTION_INDENT, current); } } recursiveAppend(sb, CoreConstants.CAUSED_BY, indent, tp.getCause()); }
private void subjoinFirstLine(StringBuilder buf, String prefix, int indent, IThrowableProxy tp) { ThrowableProxyUtil.indent(buf, indent - 1); if (prefix != null) { buf.append(prefix); } subjoinExceptionMessage(buf, tp); }
protected void subjoinSTEPArray(StringBuilder buf, int indent, IThrowableProxy tp) { StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray(); int commonFrames = tp.getCommonFrames(); boolean unrestrictedPrinting = lengthOption > stepArray.length; int maxIndex = (unrestrictedPrinting) ? stepArray.length : lengthOption; if (commonFrames > 0 && unrestrictedPrinting) { maxIndex -= commonFrames; } int ignoredCount = 0; for (int i = 0; i < maxIndex; i++) { StackTraceElementProxy element = stepArray[i]; if (!isIgnoredStackTraceLine(element.toString())) { ThrowableProxyUtil.indent(buf, indent); printStackLine(buf, ignoredCount, element); ignoredCount = 0; buf.append(CoreConstants.LINE_SEPARATOR); } else { ++ignoredCount; if (maxIndex < stepArray.length) { ++maxIndex; } } } if (ignoredCount > 0) { printIgnoredCount(buf, ignoredCount); buf.append(CoreConstants.LINE_SEPARATOR); } if (commonFrames > 0 && unrestrictedPrinting) { ThrowableProxyUtil.indent(buf, indent); buf.append("... ").append(tp.getCommonFrames()).append( " common frames omitted").append(CoreConstants.LINE_SEPARATOR); } }
@Override protected void postProcess(Object eventObject, OutputStream sw) { if (throwableExcluded) return; ILoggingEvent event = (ILoggingEvent) eventObject; IThrowableProxy tp = event.getThrowableProxy(); if (tp == null) return; String stackTracePrefix = stackTraceLayout.doLayout(event); boolean isRootException = true; while (tp != null) { StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray(); try { handleThrowableFirstLine(sw, tp, stackTracePrefix, isRootException); isRootException = false; for (StackTraceElementProxy step : stepArray) { StringBuilder sb = new StringBuilder(); sb.append(stackTracePrefix).append(step); sw.write(sb.toString().getBytes()); sw.flush(); } } catch (IOException e) { break; } tp = tp.getCause(); } }
private void handleThrowableFirstLine(OutputStream sw, IThrowableProxy tp, String stackTracePrefix, boolean isRootException) throws IOException { StringBuilder sb = new StringBuilder().append(stackTracePrefix); if (!isRootException) { sb.append(CoreConstants.CAUSED_BY); } sb.append(tp.getClassName()).append(": ").append(tp.getMessage()); sw.write(sb.toString().getBytes()); sw.flush(); }
public void render(StringBuilder sbuf, ILoggingEvent event) { IThrowableProxy tp = event.getThrowableProxy(); sbuf.append("<tr><td class=\"Exception\" colspan=\"6\">"); while (tp != null) { render(sbuf, tp); tp = tp.getCause(); } sbuf.append("</td></tr>"); }