Java Code Examples for org.apache.logging.log4j.core.LogEvent#getThrown()
The following examples show how to use
org.apache.logging.log4j.core.LogEvent#getThrown() .
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: CatLog4j2ErrorAppender.java From hermes with Apache License 2.0 | 6 votes |
private void logError(LogEvent event) { try { Throwable throwable = event.getThrown(); if (throwable != null) { Object message = event.getMessage(); if (message != null) { Cat.logError(String.valueOf(message), throwable); } else { Cat.logError(throwable); } } } catch (Throwable e) { // ignore } }
Example 2
Source File: CatAppender4Log4j2.java From x-pipe with Apache License 2.0 | 6 votes |
private void logError(LogEvent event) { Throwable info = event.getThrown(); if (info != null) { Throwable exception = info; Object message = event.getMessage(); String extra = ExceptionUtils.extractExtraMessage(info); String logMessage = StringUtil.join(",", message, extra); if (!StringUtil.isEmpty(logMessage)) { Cat.logError(logMessage, exception); } else { Cat.logError(exception); } } }
Example 3
Source File: ExceptionResolver.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Override EventResolver createStackTraceObjectResolver(final EventResolverContext context) { return (final LogEvent logEvent, final JsonWriter jsonWriter) -> { final Throwable exception = logEvent.getThrown(); if (exception == null) { jsonWriter.writeNull(); } else { context.getStackTraceObjectResolver().resolve(exception, jsonWriter); } }; }
Example 4
Source File: ExceptionRootCauseResolver.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Override EventResolver createMessageResolver(final EventResolverContext context) { return (final LogEvent logEvent, final JsonWriter jsonWriter) -> { final Throwable exception = logEvent.getThrown(); if (exception == null) { jsonWriter.writeNull(); } else { final Throwable rootCause = Throwables.getRootCause(exception); final String rootCauseMessage = rootCause.getMessage(); jsonWriter.writeString(rootCauseMessage); } }; }
Example 5
Source File: ExceptionRootCauseResolver.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Override EventResolver createClassNameResolver() { return (final LogEvent logEvent, final JsonWriter jsonWriter) -> { final Throwable exception = logEvent.getThrown(); if (exception == null) { jsonWriter.writeNull(); } else { final Throwable rootCause = Throwables.getRootCause(exception); final String rootCauseClassName = rootCause.getClass().getCanonicalName(); jsonWriter.writeString(rootCauseClassName); } }; }
Example 6
Source File: SpectatorAppender.java From spectator with Apache License 2.0 | 5 votes |
@Override public void append(LogEvent event) { final LevelTag level = LevelTag.get(event.getLevel()); registry.counter(numMessages[level.ordinal()]).increment(); if (!ignoreExceptions() && event.getThrown() != null) { final String file = (event.getSource() == null) ? "unknown" : event.getSource().getFileName(); Id stackTraceId = numStackTraces[level.ordinal()] .withTag("exception", event.getThrown().getClass().getSimpleName()) .withTag("file", file); registry.counter(stackTraceId).increment(); } }
Example 7
Source File: RootThrowablePatternConverter.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public void format(final LogEvent event, final StringBuilder toAppendTo) { final ThrowableProxy proxy = event.getThrownProxy(); final Throwable throwable = event.getThrown(); if (throwable != null && options.anyLines()) { if (proxy == null) { super.format(event, toAppendTo); return; } final String trace = proxy.getCauseStackTraceAsString(options.getIgnorePackages(), options.getTextRenderer(), getSuffix(event), options.getSeparator()); final int len = toAppendTo.length(); if (len > 0 && !Character.isWhitespace(toAppendTo.charAt(len - 1))) { toAppendTo.append(' '); } if (!options.allLines() || !Strings.LINE_SEPARATOR.equals(options.getSeparator())) { final StringBuilder sb = new StringBuilder(); final String[] array = trace.split(Strings.LINE_SEPARATOR); final int limit = options.minLines(array.length) - 1; for (int i = 0; i <= limit; ++i) { sb.append(array[i]); if (i < limit) { sb.append(options.getSeparator()); } } toAppendTo.append(sb.toString()); } else { toAppendTo.append(trace); } } }
Example 8
Source File: ExceptionResolver.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Override EventResolver createMessageResolver(final EventResolverContext context) { return (final LogEvent logEvent, final JsonWriter jsonWriter) -> { final Throwable exception = logEvent.getThrown(); if (exception == null) { jsonWriter.writeNull(); } else { String exceptionMessage = exception.getMessage(); jsonWriter.writeString(exceptionMessage); } }; }
Example 9
Source File: ExceptionResolver.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Override EventResolver createStackTraceStringResolver(final EventResolverContext context) { StackTraceStringResolver stackTraceStringResolver = new StackTraceStringResolver(context); return (final LogEvent logEvent, final JsonWriter jsonWriter) -> { final Throwable exception = logEvent.getThrown(); if (exception == null) { jsonWriter.writeNull(); } else { stackTraceStringResolver.resolve(exception, jsonWriter); } }; }
Example 10
Source File: ExceptionRootCauseResolver.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Override EventResolver createStackTraceStringResolver(final EventResolverContext context) { final StackTraceStringResolver stackTraceStringResolver = new StackTraceStringResolver(context); return (final LogEvent logEvent, final JsonWriter jsonWriter) -> { final Throwable exception = logEvent.getThrown(); if (exception == null) { jsonWriter.writeNull(); } else { final Throwable rootCause = Throwables.getRootCause(exception); stackTraceStringResolver.resolve(rootCause, jsonWriter); } }; }
Example 11
Source File: MockLogAppender.java From crate with Apache License 2.0 | 4 votes |
@Override public boolean innerMatch(final LogEvent event) { return event.getThrown() != null && event.getThrown().getClass() == clazz && event.getThrown().getMessage().equals(exceptionMessage); }
Example 12
Source File: ExceptionResolver.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Override public boolean isResolvable(final LogEvent logEvent) { return stackTraceEnabled && logEvent.getThrown() != null; }
Example 13
Source File: GelfAppender.java From log4j2-gelf with Apache License 2.0 | 4 votes |
@Override public void append(LogEvent event) { final Layout<? extends Serializable> layout = getLayout(); final String formattedMessage; if (layout == null) { formattedMessage = event.getMessage().getFormattedMessage(); } else { formattedMessage = new String(layout.toByteArray(event), StandardCharsets.UTF_8); } final GelfMessageBuilder builder = new GelfMessageBuilder(formattedMessage, hostName) .timestamp(event.getTimeMillis() / 1000d) .level(GelfMessageLevel.fromNumericLevel(Severity.getSeverity(event.getLevel()).getCode())) .additionalField("loggerName", event.getLoggerName()) .additionalField("threadName", event.getThreadName()); final Marker marker = event.getMarker(); if (marker != null) { builder.additionalField("marker", marker.getName()); } if (includeThreadContext) { for (Map.Entry<String, String> entry : event.getContextMap().entrySet()) { builder.additionalField(entry.getKey(), entry.getValue()); } // Guard against https://issues.apache.org/jira/browse/LOG4J2-1530 final ThreadContext.ContextStack contextStack = event.getContextStack(); if (contextStack != null) { final List<String> contextStackItems = contextStack.asList(); if (contextStackItems != null && !contextStackItems.isEmpty()) { builder.additionalField("contextStack", contextStackItems.toString()); } } } if (includeSource) { final StackTraceElement source = event.getSource(); if (source != null) { builder.additionalField("sourceFileName", source.getFileName()); builder.additionalField("sourceMethodName", source.getMethodName()); builder.additionalField("sourceClassName", source.getClassName()); builder.additionalField("sourceLineNumber", source.getLineNumber()); } } @SuppressWarnings("all") final Throwable thrown = event.getThrown(); if (includeStackTrace && thrown != null) { String stackTrace; if (includeExceptionCause) { final StringWriter stringWriter = new StringWriter(); final PrintWriter printWriter = new PrintWriter(stringWriter); thrown.printStackTrace(printWriter); stackTrace = stringWriter.toString(); } else { stackTrace = getSimpleStacktraceAsString(thrown); } builder.additionalField("exceptionClass", thrown.getClass().getCanonicalName()); builder.additionalField("exceptionMessage", thrown.getMessage()); builder.additionalField("exceptionStackTrace", stackTrace); builder.fullMessage(formattedMessage); } if (!additionalFields.isEmpty()) { builder.additionalFields(additionalFields); } final GelfMessage gelfMessage = builder.build(); try { final boolean sent = client.trySend(gelfMessage); if (!sent) { LOG.debug("Couldn't send message: {}", gelfMessage); } } catch (Exception e) { throw new AppenderLoggingException("failed to write log event to GELF server: " + e.getMessage(), e); } }
Example 14
Source File: ExceptionRootCauseResolver.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Override public boolean isResolvable(final LogEvent logEvent) { return stackTraceEnabled && logEvent.getThrown() != null; }
Example 15
Source File: XPipeThrowablePatternConverter.java From x-pipe with Apache License 2.0 | 4 votes |
/** * {@inheritDoc} */ @Override public void format(final LogEvent event, final StringBuilder toAppendTo) { final ThrowableProxy proxy = event.getThrownProxy(); final Throwable throwable = event.getThrown(); //xpipe code if(throwable != null){ if(ExceptionUtils.isSocketIoException(event.getThrown()) || ExceptionUtils.xpipeExceptionLogMessage(throwable)){ toAppendTo.append("," + throwable.getClass() + ":" + throwable.getMessage()); return; } String extra = ExceptionUtils.extractExtraMessage(throwable); if(extra != null){ toAppendTo.append(String.format("\n[%s]", extra)); } } if ((throwable != null || proxy != null) && options.anyLines()) { if (proxy == null) { super.format(event, toAppendTo); return; } final String extStackTrace = proxy.getExtendedStackTraceAsString(options.getPackages()); final int len = toAppendTo.length(); if (len > 0 && !Character.isWhitespace(toAppendTo.charAt(len - 1))) { toAppendTo.append(' '); } if (!options.allLines() || !Constants.LINE_SEPARATOR.equals(options.getSeparator())) { final StringBuilder sb = new StringBuilder(); final String[] array = extStackTrace.split(Constants.LINE_SEPARATOR); final int limit = options.minLines(array.length) - 1; for (int i = 0; i <= limit; ++i) { sb.append(array[i]); if (i < limit) { sb.append(options.getSeparator()); } } toAppendTo.append(sb.toString()); } else { toAppendTo.append(extStackTrace); } } }
Example 16
Source File: JsonPatternLayout.java From cf-java-logging-support with Apache License 2.0 | 4 votes |
private PatternSelector getSelector(LogEvent event) { if (event.getThrownProxy() != null || event.getThrown() != null) { return execptionSelector; } return markerSelector; }
Example 17
Source File: LogTestRule.java From ArchUnit with Apache License 2.0 | 4 votes |
RecordedLogEvent(LogEvent event) { this(event.getLevel(), event.getMessage().getFormattedMessage(), event.getThrown()); }
Example 18
Source File: DeobfuscatingRewritePolicy.java From VanillaFix with MIT License | 4 votes |
@Override public LogEvent rewrite(LogEvent source) { if (source.getThrown() != null) StacktraceDeobfuscator.deobfuscateThrowable(source.getThrown()); return source; }
Example 19
Source File: Log4j2QiniuAppender.java From qiniu-logging-plugin with BSD 2-Clause "Simplified" License | 4 votes |
public void append(LogEvent logEvent) { Point point = new Point(); point.append("timestamp", logEvent.getTimeMillis()); point.append("level", logEvent.getLevel().toString()); point.append("logger", logEvent.getLoggerName()); if (logEvent.getMarker() != null) { point.append("marker", logEvent.getMarker().toString()); } else { point.append("marker", ""); } point.append("message", logEvent.getMessage().getFormattedMessage()); point.append("thread_name", logEvent.getThreadName()); point.append("thread_id", logEvent.getThreadId()); point.append("thread_priority", logEvent.getThreadPriority()); if (logEvent.getThrown() != null) { Throwable t = logEvent.getThrown().getCause(); if (t != null) { StringBuilder exceptionBuilder = new StringBuilder(); exceptionBuilder.append(t.getMessage()).append("\n"); for (StackTraceElement element : logEvent.getThrown().getStackTrace()) { exceptionBuilder.append(element.toString()).append("\n"); } point.append("exception", exceptionBuilder.toString()); } else { point.append("exception", logEvent.getThrown().getMessage()); } } else { point.append("exception", ""); } //lock this.rwLock.lock(); if (!batch.canAdd(point)) { final byte[] postBody = batch.toString().getBytes(Constants.UTF_8); try { this.executorService.execute(new Runnable() { public void run() { try { Response response = logPushSender.send(postBody); response.close(); } catch (QiniuException e) { //e.printStackTrace(); guard.write(postBody); } } }); } catch (RejectedExecutionException ex) { guard.write(postBody); } batch.clear(); } batch.add(point); this.rwLock.unlock(); }
Example 20
Source File: HtmlLayout.java From logging-log4j2 with Apache License 2.0 | 4 votes |
/** * Formats as a String. * * @param event The Logging Event. * @return A String containing the LogEvent as HTML. */ @Override public String toSerializable(final LogEvent event) { final StringBuilder sbuf = getStringBuilder(); sbuf.append(Strings.LINE_SEPARATOR).append("<tr>").append(Strings.LINE_SEPARATOR); sbuf.append("<td>"); sbuf.append(event.getTimeMillis() - jvmStartTime); sbuf.append("</td>").append(Strings.LINE_SEPARATOR); final String escapedThread = Transform.escapeHtmlTags(event.getThreadName()); sbuf.append("<td title=\"").append(escapedThread).append(" thread\">"); sbuf.append(escapedThread); sbuf.append("</td>").append(Strings.LINE_SEPARATOR); sbuf.append("<td title=\"Level\">"); if (event.getLevel().equals(Level.DEBUG)) { sbuf.append("<font color=\"#339933\">"); sbuf.append(Transform.escapeHtmlTags(String.valueOf(event.getLevel()))); sbuf.append("</font>"); } else if (event.getLevel().isMoreSpecificThan(Level.WARN)) { sbuf.append("<font color=\"#993300\"><strong>"); sbuf.append(Transform.escapeHtmlTags(String.valueOf(event.getLevel()))); sbuf.append("</strong></font>"); } else { sbuf.append(Transform.escapeHtmlTags(String.valueOf(event.getLevel()))); } sbuf.append("</td>").append(Strings.LINE_SEPARATOR); String escapedLogger = Transform.escapeHtmlTags(event.getLoggerName()); if (Strings.isEmpty(escapedLogger)) { escapedLogger = LoggerConfig.ROOT; } sbuf.append("<td title=\"").append(escapedLogger).append(" logger\">"); sbuf.append(escapedLogger); sbuf.append("</td>").append(Strings.LINE_SEPARATOR); if (locationInfo) { final StackTraceElement element = event.getSource(); sbuf.append("<td>"); sbuf.append(Transform.escapeHtmlTags(element.getFileName())); sbuf.append(':'); sbuf.append(element.getLineNumber()); sbuf.append("</td>").append(Strings.LINE_SEPARATOR); } sbuf.append("<td title=\"Message\">"); sbuf.append(Transform.escapeHtmlTags(event.getMessage().getFormattedMessage()).replaceAll(REGEXP, "<br />")); sbuf.append("</td>").append(Strings.LINE_SEPARATOR); sbuf.append("</tr>").append(Strings.LINE_SEPARATOR); if (event.getContextStack() != null && !event.getContextStack().isEmpty()) { sbuf.append("<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : ").append(fontSize); sbuf.append(";\" colspan=\"6\" "); sbuf.append("title=\"Nested Diagnostic Context\">"); sbuf.append("NDC: ").append(Transform.escapeHtmlTags(event.getContextStack().toString())); sbuf.append("</td></tr>").append(Strings.LINE_SEPARATOR); } if (event.getContextData() != null && !event.getContextData().isEmpty()) { sbuf.append("<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : ").append(fontSize); sbuf.append(";\" colspan=\"6\" "); sbuf.append("title=\"Mapped Diagnostic Context\">"); sbuf.append("MDC: ").append(Transform.escapeHtmlTags(event.getContextData().toMap().toString())); sbuf.append("</td></tr>").append(Strings.LINE_SEPARATOR); } final Throwable throwable = event.getThrown(); if (throwable != null) { sbuf.append("<tr><td bgcolor=\"#993300\" style=\"color:White; font-size : ").append(fontSize); sbuf.append(";\" colspan=\"6\">"); appendThrowableAsHtml(throwable, sbuf); sbuf.append("</td></tr>").append(Strings.LINE_SEPARATOR); } return sbuf.toString(); }