/** * {@inheritDoc} */ @Override public void format(final LogEvent event, final StringBuilder toAppendTo) { final ThrowableProxy proxy = event.getThrownProxy(); final Throwable throwable = event.getThrown(); if ((throwable != null || proxy != null) && options.anyLines()) { if (proxy == null) { super.format(event, toAppendTo); return; } final String extStackTrace = proxy.getExtendedStackTraceAsString(options.getIgnorePackages(), options.getTextRenderer(), getSuffix(event), options.getSeparator()); final int len = toAppendTo.length(); if (len > 0 && !Character.isWhitespace(toAppendTo.charAt(len - 1))) { toAppendTo.append(' '); } toAppendTo.append(extStackTrace); } }
@Test public void testDeserializedLogEventWithThrowableProxyButNoThrowable() { final ExtendedThrowablePatternConverter converter = ExtendedThrowablePatternConverter.newInstance(null, null); final Throwable originalThrowable = new Exception("something bad happened"); final ThrowableProxy throwableProxy = new ThrowableProxy(originalThrowable); final Throwable deserializedThrowable = null; final Log4jLogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("testLogger") // .setLoggerFqcn(this.getClass().getName()) // .setLevel(Level.DEBUG) // .setMessage(new SimpleMessage("")) // .setThrown(deserializedThrowable) // .setThrownProxy(throwableProxy) // .setTimeMillis(0).build(); final StringBuilder sb = new StringBuilder(); converter.format(event, sb); final String result = sb.toString(); assertTrue(result, result.contains(originalThrowable.getMessage())); assertTrue(result, result.contains(originalThrowable.getStackTrace()[0].getMethodName())); }
public void appendersCalled(LogEvent event) { ThrowableProxy throwableProxy = event.getThrownProxy(); String throwable = (throwableProxy != null) ? throwableProxy.getName() : null; LogEventTracker.LogLevel level = LogEventTracker.LogLevel.valueOf(event.getLevel().toString()); tracker.track(level, (throwableProxy != null), throwable); }
/** * {@inheritDoc} */ @Override public void format(final LogEvent event, final StringBuilder toAppendTo) { ThrowableProxy proxy = null; if (event instanceof Log4jLogEvent) { proxy = ((Log4jLogEvent) event).getThrownProxy(); } final Throwable throwable = event.getThrown(); if (throwable != null && options.anyLines()) { if (proxy == null) { super.format(event, toAppendTo); return; } final String trace = proxy.getExtendedStackTrace(options.getPackages()); final int len = toAppendTo.length(); if (len > 0 && !Character.isWhitespace(toAppendTo.charAt(len - 1))) { toAppendTo.append(" "); } if (!options.allLines() || !Constants.LINE_SEP.equals(options.getSeparator())) { final StringBuilder sb = new StringBuilder(); final String[] array = trace.split(Constants.LINE_SEP); 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); } } }
/** * {@inheritDoc} */ @Override public void format(final LogEvent event, final StringBuilder toAppendTo) { ThrowableProxy proxy = null; if (event instanceof Log4jLogEvent) { proxy = ((Log4jLogEvent) event).getThrownProxy(); } final Throwable throwable = event.getThrown(); if (throwable != null && options.anyLines()) { if (proxy == null) { super.format(event, toAppendTo); return; } final String trace = proxy.getRootCauseStackTrace(options.getPackages()); final int len = toAppendTo.length(); if (len > 0 && !Character.isWhitespace(toAppendTo.charAt(len - 1))) { toAppendTo.append(" "); } if (!options.allLines() || !Constants.LINE_SEP.equals(options.getSeparator())) { final StringBuilder sb = new StringBuilder(); final String[] array = trace.split(Constants.LINE_SEP); 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); } } }
/** * {@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); } } }
void setupModule(final SetupContext context, final boolean includeStacktrace, final boolean stacktraceAsString) { // JRE classes: we cannot edit those with Jackson annotations context.setMixInAnnotations(StackTraceElement.class, StackTraceElementMixIn.class); // Log4j API classes: we do not want to edit those with Jackson annotations because the API module should not depend on Jackson. context.setMixInAnnotations(Marker.class, MarkerMixIn.class); context.setMixInAnnotations(Level.class, LevelMixIn.class); context.setMixInAnnotations(Instant.class, InstantMixIn.class); context.setMixInAnnotations(LogEvent.class, LogEventWithContextListMixIn.class); // Log4j Core classes: we do not want to bring in Jackson at runtime if we do not have to. context.setMixInAnnotations(ExtendedStackTraceElement.class, ExtendedStackTraceElementMixIn.class); context.setMixInAnnotations(ThrowableProxy.class, includeStacktrace ? (stacktraceAsString ? ThrowableProxyWithStacktraceAsStringMixIn.class : ThrowableProxyMixIn.class ) : ThrowableProxyWithoutStacktraceMixIn.class); }
void setupModule(final SetupContext context, final boolean includeStacktrace, final boolean stacktraceAsString) { // JRE classes: we cannot edit those with Jackson annotations context.setMixInAnnotations(StackTraceElement.class, StackTraceElementMixIn.class); // Log4j API classes: we do not want to edit those with Jackson annotations because the API module should not depend on Jackson. context.setMixInAnnotations(Marker.class, MarkerMixIn.class); context.setMixInAnnotations(Level.class, LevelMixIn.class); context.setMixInAnnotations(Instant.class, InstantMixIn.class); context.setMixInAnnotations(LogEvent.class, LogEventJsonMixIn.class); // different ThreadContext handling // Log4j Core classes: we do not want to bring in Jackson at runtime if we do not have to. context.setMixInAnnotations(ExtendedStackTraceElement.class, ExtendedStackTraceElementMixIn.class); context.setMixInAnnotations(ThrowableProxy.class, includeStacktrace ? (stacktraceAsString ? ThrowableProxyWithStacktraceAsStringMixIn.class : ThrowableProxyMixIn.class ) : ThrowableProxyWithoutStacktraceMixIn.class); }
@Test public void testSerializationDeserialization() throws IOException, ClassNotFoundException { final RingBufferLogEvent evt = new RingBufferLogEvent(); final String loggerName = "logger.name"; final Marker marker = null; final String fqcn = "f.q.c.n"; final Level level = Level.TRACE; final Message data = new SimpleMessage("message"); final Throwable t = new InternalError("not a real error"); final ContextStack contextStack = null; final String threadName = "main"; final StackTraceElement location = null; evt.setValues(null, loggerName, marker, fqcn, level, data, t, (StringMap) evt.getContextData(), contextStack, -1, threadName, -1, location, new FixedPreciseClock(12345, 678), new DummyNanoClock(1)); ((StringMap) evt.getContextData()).putValue("key", "value"); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ObjectOutputStream out = new ObjectOutputStream(baos); out.writeObject(evt); final ObjectInputStream in = new FilteredObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); final RingBufferLogEvent other = (RingBufferLogEvent) in.readObject(); assertEquals(loggerName, other.getLoggerName()); assertEquals(marker, other.getMarker()); assertEquals(fqcn, other.getLoggerFqcn()); assertEquals(level, other.getLevel()); assertEquals(data, other.getMessage()); assertNull("null after serialization", other.getThrown()); assertEquals(new ThrowableProxy(t), other.getThrownProxy()); assertEquals(evt.getContextData(), other.getContextData()); assertEquals(contextStack, other.getContextStack()); assertEquals(threadName, other.getThreadName()); assertEquals(location, other.getSource()); assertEquals(12345, other.getTimeMillis()); assertEquals(678, other.getInstant().getNanoOfMillisecond()); }
/** * @return a log event that uses all the bells and whistles, features, nooks and crannies */ static Log4jLogEvent createLogEvent() { final Marker cMarker = MarkerManager.getMarker("Marker1"); final Marker pMarker1 = MarkerManager.getMarker("ParentMarker1"); final Marker pMarker2 = MarkerManager.getMarker("ParentMarker2"); final Marker gfMarker = MarkerManager.getMarker("GrandFatherMarker"); final Marker gmMarker = MarkerManager.getMarker("GrandMotherMarker"); cMarker.addParents(pMarker1); cMarker.addParents(pMarker2); pMarker1.addParents(gmMarker); pMarker1.addParents(gfMarker); final Exception sourceHelper = new Exception(); sourceHelper.fillInStackTrace(); final Exception cause = new NullPointerException("testNPEx"); sourceHelper.fillInStackTrace(); final StackTraceElement source = sourceHelper.getStackTrace()[0]; final IOException ioException = new IOException("testIOEx", cause); ioException.addSuppressed(new IndexOutOfBoundsException("I am suppressed exception 1")); ioException.addSuppressed(new IndexOutOfBoundsException("I am suppressed exception 2")); final ThrowableProxy throwableProxy = new ThrowableProxy(ioException); final Map<String, String> contextMap = new HashMap<>(); contextMap.put("MDC.A", "A_Value"); contextMap.put("MDC.B", "B_Value"); final DefaultThreadContextStack contextStack = new DefaultThreadContextStack(true); contextStack.clear(); contextStack.push("stack_msg1"); contextStack.add("stack_msg2"); final Log4jLogEvent expected = Log4jLogEvent.newBuilder() // .setLoggerName("a.B") // .setMarker(cMarker) // .setLoggerFqcn("f.q.c.n") // .setLevel(Level.DEBUG) // .setMessage(new SimpleMessage("Msg")) // .setThrown(ioException) // .setThrownProxy(throwableProxy) // .setContextMap(contextMap) // .setContextStack(contextStack) // .setThreadName("MyThreadName") // .setSource(source) // .setTimeMillis(1).build(); // validate event? return expected; }
private final String format(final LogEvent event) { final StringBuilder sb = getStringBuilder(); if (complete && eventCount > 0) { sb.append(", "); } sb.append('{'); // Internal Info json(sb, "layout.version", LAYOUT_VERSION); json(sb, "layout.start", layoutStartTime); json(sb, "layout.sequence", layoutSequence.incrementAndGet()); // Basic Info json(sb, "timestamp", event.getTimeMillis()); json(sb, "thread", event.getThreadName()); json(sb, "threadId", event.getThreadId()); json(sb, "level", event.getLevel().toString()); json(sb, "logger", event.getLoggerName()); // Caller info if (locationInfo) { final StackTraceElement source = event.getSource(); json(sb, "source"); sb.append('{'); json(sb, "class", source.getClassName()); json(sb, "method", source.getMethodName()); json(sb, "file", source.getFileName()); json(sb, "line", source.getLineNumber()); sb.setLength(sb.length() - 1); sb.append('}').append(','); } // Diagnostic Context if (properties) { if (!event.getContextStack().isEmpty()) { json(sb, "ndc", event.getContextStack().asList()); } if (!event.getContextData().isEmpty()) { json(sb, "mdc", event.getContextData().toMap()); } } // Additional Fields for (int i = 0; i < additionalFields.length; i++) { final KeyValuePair kv = additionalFields[i]; final String key = kv.getKey(); final String value = kv.getValue(); final String iv = interpolator.lookup(event, value); if (iv != null) { json(sb, (key != null) ? key : value, iv); } } // Message json(sb, "msg", event.getMessage().getFormattedMessage()); // Exceptions if (event.getThrownProxy() != null) { final ThrowableProxy throwableInfo = event.getThrownProxy(); final Throwable t = throwableInfo.getThrowable(); final String exClass = t.getClass().getCanonicalName(); if (exClass != null) { json(sb, "exception", exClass); } final String exMsg = t.getMessage(); if (exMsg != null) { json(sb, "cause", exMsg); } // TODO: Change pure string to complex list/maps of stacktraces? final String stackTrace = throwableInfo.getExtendedStackTraceAsString(""); if (stackTrace != null) { json(sb, "stacktrace", stackTrace); } } sb.setLength(sb.length() - 1); sb.append('}').append(eol); return sb.toString(); }
private Message processLogEvent(LogEvent logEvent) { final String formattedMessage = logEvent.getMessage().getFormattedMessage(); final DateTime timestamp = new DateTime(logEvent.getTimeMillis(), DateTimeZone.UTC); final Message message = new Message(formattedMessage, hostname, timestamp); final Level level = logEvent.getLevel(); message.addField(Message.FIELD_LEVEL, Severity.getSeverity(level).getCode()); message.addField("log4j_level", level.name()); message.addField("log4j_level_int", level.intLevel()); message.addField("node_id", nodeId); if (clusterId != null) { message.addField("cluster_id", clusterId); } message.addField("logger_name", logEvent.getLoggerName()); message.addField("thread_id", logEvent.getThreadId()); message.addField("thread_name", logEvent.getThreadName()); message.addField("thread_priority", logEvent.getThreadPriority()); message.addField("timestamp_nanos", logEvent.getNanoTime()); final Marker marker = logEvent.getMarker(); if (marker != null) { message.addField("marker", marker.getName()); } if (includeThreadContext) { logEvent.getContextData().forEach((k, v ) -> message.addField("context_" + k, v)); // Guard against https://issues.apache.org/jira/browse/LOG4J2-1530 final ThreadContext.ContextStack contextStack = logEvent.getContextStack(); if (contextStack != null) { final List<String> contextStackItems = contextStack.asList(); if (contextStackItems != null && !contextStackItems.isEmpty()) { message.addField("context_stack", contextStackItems); } } } if (includeSource) { final StackTraceElement source = logEvent.getSource(); if (source != null) { message.addField("source_file_name", source.getFileName()); message.addField("source_method_name", source.getMethodName()); message.addField("source_class_name", source.getClassName()); message.addField("source_line_number", source.getLineNumber()); } } final ThrowableProxy throwableProxy = logEvent.getThrownProxy(); if (includeStackTrace && throwableProxy != null) { final String stackTrace; if (includeExceptionCause) { stackTrace = throwableProxy.getExtendedStackTraceAsString(""); } else { stackTrace = throwableProxy.getCauseStackTraceAsString(""); } message.addField("exception_class", throwableProxy.getName()); message.addField("exception_message", throwableProxy.getMessage()); message.addField("exception_stack_trace", stackTrace); } return message; }
/** * {@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); } } }
/** * Returns the Throwable associated with the event, if any. * @return the Throwable. */ @Override public ThrowableProxy getThrownProxy() { return event.getThrownProxy(); }
@Override @Transient public ThrowableProxy getThrownProxy() { return this.getWrappedEvent().getThrownProxy(); }
@Override public ThrowableProxy getThrownProxy() { return null; }
@JsonProperty(JsonConstants.ELT_SUPPRESSED) @JacksonXmlElementWrapper(namespace = XmlConstants.XML_NAMESPACE, localName = XmlConstants.ELT_SUPPRESSED) @JacksonXmlProperty(namespace = XmlConstants.XML_NAMESPACE, localName = XmlConstants.ELT_SUPPRESSED_ITEM) public abstract ThrowableProxy[] getSuppressedProxies();
@JsonProperty(JsonConstants.ELT_THROWN) @JacksonXmlProperty(namespace = XmlConstants.XML_NAMESPACE, localName = XmlConstants.ELT_THROWN) @Override public abstract ThrowableProxy getThrownProxy();
/** * @return a log event that uses all the bells and whistles, features, nooks and crannies */ static Log4jLogEvent createLogEvent() { final Marker cMarker = MarkerManager.getMarker("Marker1"); final Marker pMarker1 = MarkerManager.getMarker("ParentMarker1"); final Marker pMarker2 = MarkerManager.getMarker("ParentMarker2"); final Marker gfMarker = MarkerManager.getMarker("GrandFatherMarker"); final Marker gmMarker = MarkerManager.getMarker("GrandMotherMarker"); cMarker.addParents(pMarker1); cMarker.addParents(pMarker2); pMarker1.addParents(gmMarker); pMarker1.addParents(gfMarker); final Exception sourceHelper = new Exception(); sourceHelper.fillInStackTrace(); final Exception cause = new NullPointerException("testNPEx"); sourceHelper.fillInStackTrace(); final StackTraceElement source = sourceHelper.getStackTrace()[0]; final IOException ioException = new IOException("testIOEx", cause); ioException.addSuppressed(new IndexOutOfBoundsException("I am suppressed exception 1")); ioException.addSuppressed(new IndexOutOfBoundsException("I am suppressed exception 2")); final ThrowableProxy throwableProxy = new ThrowableProxy(ioException); final StringMap contextData = ContextDataFactory.createContextData(); contextData.putValue("MDC.A", "A_Value"); contextData.putValue("MDC.B", "B_Value"); final DefaultThreadContextStack contextStack = new DefaultThreadContextStack(true); contextStack.clear(); contextStack.push("stack_msg1"); contextStack.add("stack_msg2"); final Log4jLogEvent expected = Log4jLogEvent.newBuilder() // .setLoggerName("a.B") // .setMarker(cMarker) // .setLoggerFqcn("f.q.c.n") // .setLevel(Level.DEBUG) // .setMessage(new SimpleMessage("Msg")) // .setThrown(ioException) // .setThrownProxy(throwableProxy) // .setContextData(contextData) // .setContextStack(contextStack) // .setThreadName("MyThreadName") // .setSource(source) // .setTimeMillis(1).build(); // validate event? return expected; }
/** * Gets the exception logged. Annotated with {@code @Convert(converter = ThrowableAttributeConverter.class)}. * * @return the exception logged. * @see ThrowableAttributeConverter */ @Override @Transient public ThrowableProxy getThrownProxy() { return this.getWrappedEvent().getThrownProxy(); }
/** * Gets throwable proxy associated with logging request. * * @return throwable, may be null. */ ThrowableProxy getThrownProxy();